Skip to content

Commit

Permalink
feat(api): add common logging types & default impls
Browse files Browse the repository at this point in the history
- add ILogger interface, LogLevel enum
- add NULL_LOGGER & ConsoleLogger
  • Loading branch information
postspectacular committed Apr 24, 2019
1 parent ec0c15e commit 4578604
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ export type TypedArray =
*/
export type Watch<T> = (id: string, oldState: T, newState: T) => void;

export const enum LogLevel {
FINE,
DEBUG,
INFO,
WARN,
SEVERE,
NONE
}

/**
* @param K key type
* @param V value type
Expand Down Expand Up @@ -566,6 +575,14 @@ export interface ILength {
readonly length: number;
}

export interface ILogger {
fine(...args: any[]): void;
debug(...args: any[]): void;
info(...args: any[]): void;
warn(...args: any[]): void;
severe(...args: any[]): void;
}

/**
* Generic interface for types supporting metadata. Implementations MUST
* exclude metadata from any comparisons, equality checks & hashing.
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./api";

export * from "./assert";
export * from "./logger";
export * from "./mixin";

export * from "./decorators/configurable";
Expand Down
43 changes: 43 additions & 0 deletions packages/api/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ILogger, LogLevel } from "./api";

export const NULL_LOGGER: ILogger = {
fine() {},
debug() {},
info() {},
warn() {},
severe() {}
};

export class ConsoleLogger implements ILogger {
id: string;
level: LogLevel;

constructor(id: string, level = LogLevel.FINE) {
this.id = id;
this.level = level;
}

fine(...args: any[]): void {
this.level <= LogLevel.FINE && this.log("FINE", args);
}

debug(...args: any[]): void {
this.level <= LogLevel.DEBUG && this.log("DEBUG", args);
}

info(...args: any[]): void {
this.level <= LogLevel.INFO && this.log("INFO", args);
}

warn(...args: any[]): void {
this.level <= LogLevel.WARN && this.log("WARN", args);
}

severe(...args: any[]): void {
this.level <= LogLevel.SEVERE && this.log("SEVERE", args);
}

protected log(level: string, args: any[]) {
console.info(`[${level}][${this.id}]`, ...args);
}
}

0 comments on commit 4578604

Please sign in to comment.