Skip to content

Commit

Permalink
Upgrade Log and logging methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ymonb1291 committed Oct 14, 2020
1 parent 15acb61 commit 0d0f4ff
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
25 changes: 13 additions & 12 deletions log.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@ import type { KeyValuePair } from "./utils.ts";

export interface LogMetaData extends KeyValuePair {
_v: number;
}
};

export interface Bindings extends KeyValuePair {
bindings?: KeyValuePair;
}
};

export interface BaseLog extends Bindings {
level: number | string;
name?: string;
time?: number | string;
}
};

export interface LogPayload extends KeyValuePair {
payload?: KeyValuePair;
}
};

interface LogMessage extends LogPayload {
message?: string;
message?: string | number | boolean;
};

interface LogError extends LogPayload {
errorName: string;
message: string;
stack?: string;
type: "error";
error: {
message: string;
name: string;
stack?: string;
}
};

export type LogBody = Partial<LogMessage> & Partial<LogError>;

export interface LogWithMessage extends LogMetaData, BaseLog, LogMessage, LogPayload {}
export interface LogWithError extends LogMetaData, BaseLog, LogError, LogPayload {}
export interface Log extends LogMetaData, BaseLog, LogBody, LogPayload {}
export interface LogWithMessage extends LogMetaData, BaseLog, LogMessage, LogPayload {};
export interface LogWithError extends LogMetaData, BaseLog, LogError, LogPayload {};
export interface Log extends LogMetaData, BaseLog, LogBody, LogPayload {};
17 changes: 10 additions & 7 deletions logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export abstract class Logger {
/** Handle the logging flow */
protected logger(level: Level, ...data: unknown[]): this {
// Stop flow if loggind disabled or level too low
if(level < this.configuration.level || !this.configuration.enabled || !data.length) return this;
if(level < this.configuration.level || !this.configuration.enabled) return this;

// Initialize, format and print Log
let log: Log = this.build(level, ...data);
Expand Down Expand Up @@ -102,22 +102,25 @@ export abstract class Logger {
/** Build a LogBody object with filtered payload */
private buildBody(baseLog: BaseLog, ...data: unknown[]): LogBody {
// Extracts the message from data
const message: string | void = typeof data[0] === "string" ? data[0]: void 0;
const message: string | number | boolean | void =
typeof data[0] === "string" || typeof data[0] === "number" || typeof data[0] === "boolean"
? data[0]
: void 0;
// Extracts the error from data
const error: Error | void = data[0] instanceof Error ? data[0]: void 0;
// Extracts the additional data
const rawPayload: KeyValuePair[] = (data as KeyValuePair[]).slice(message || error ? 1 : 0, data.length);


// Init LogBody with message or error
let logBody: LogBody = {};
if(error) {
logBody = {
type: "error",
errorName: error.name,
message: error.message,
error: {
message: error.message,
name: error.name,
}
};
if(error.stack) logBody.stack = error.stack;
if(error.stack && logBody.error) logBody.error.stack = error.stack;
} else if(message) {
logBody = {message}
}
Expand Down
49 changes: 30 additions & 19 deletions papyrus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,56 @@ export class Papyrus extends Logger {
}

// Log using level debug
public debug(message: string): this
public debug(message: string, ...data: KeyValuePair[]): this
public debug(...data: KeyValuePair[]): this
public debug(): this;
public debug(message: string | number | boolean): this;
public debug(message: string | number | boolean, ...data: KeyValuePair[]): this;
public debug(...data: KeyValuePair[]): this;
public debug(error: Error): this;
public debug(error: Error, ...data: KeyValuePair[]): this;
public debug(...data: unknown[]): this {
return this.logger(Level.debug, ...data);
}

// Log using level error
public error(message: string): this
public error(message: string, ...data: KeyValuePair[]): this
public error(...data: KeyValuePair[]): this
public error(error: Error): this
public error(error: Error, ...data: KeyValuePair[]): this
public error(): this;
public error(message: string | number | boolean): this;
public error(message: string | number | boolean, ...data: KeyValuePair[]): this;
public error(...data: KeyValuePair[]): this;
public error(error: Error): this;
public error(error: Error, ...data: KeyValuePair[]): this;
public error(...data: unknown[]): this {
return this.logger(Level.error, ...data);
}

// Log using level info
public info(message: string): this
public info(message: string, ...data: KeyValuePair[]): this
public info(...data: KeyValuePair[]): this
public info(): this;
public info(message: string | number | boolean): this;
public info(message: string | number | boolean, ...data: KeyValuePair[]): this;
public info(...data: KeyValuePair[]): this;
public info(error: Error): this;
public info(error: Error, ...data: KeyValuePair[]): this;
public info(...data: unknown[]): this {
return this.logger(Level.info, ...data);
}

// Log using level trace
public trace(message: string): this
public trace(message: string, ...data: KeyValuePair[]): this
public trace(...data: KeyValuePair[]): this
public trace(): this;
public trace(message: string | number | boolean): this;
public trace(message: string | number | boolean, ...data: KeyValuePair[]): this;
public trace(...data: KeyValuePair[]): this;
public trace(error: Error): this;
public trace(error: Error, ...data: KeyValuePair[]): this;
public trace(...data: unknown[]): this {
return this.logger(Level.trace, ...data);
}

// Log using level warn
public warn(message: string): this
public warn(message: string, ...data: KeyValuePair[]): this
public warn(...data: KeyValuePair[]): this
public warn(error: Error): this
public warn(error: Error, ...data: KeyValuePair[]): this
public warn(): this;
public warn(message: string | number | boolean): this;
public warn(message: string | number | boolean, ...data: KeyValuePair[]): this;
public warn(...data: KeyValuePair[]): this;
public warn(error: Error): this;
public warn(error: Error, ...data: KeyValuePair[]): this;
public warn(...data: unknown[]): this {
return this.logger(Level.warn, ...data);
}
Expand Down

0 comments on commit 0d0f4ff

Please sign in to comment.