Skip to content

Commit

Permalink
Remove _v from Log
Browse files Browse the repository at this point in the history
  • Loading branch information
ymonb1291 committed Oct 17, 2020
1 parent 66e9dff commit 06ef8b6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion formatter.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Log } from "./log.interface.ts";

export interface Formatter {
format: (data: string | Log) => string | Log;
format: (data: string | Log, _v: number) => string | Log;
}
10 changes: 3 additions & 7 deletions log.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { KeyValuePair } from "./utils.ts";

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

export interface Bindings extends KeyValuePair {
bindings?: KeyValuePair;
};
Expand Down Expand Up @@ -32,6 +28,6 @@ interface LogError extends LogPayload {

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 BaseLog, LogMessage, LogPayload {};
export interface LogWithError extends BaseLog, LogError, LogPayload {};
export interface Log extends BaseLog, LogBody, LogPayload {};
18 changes: 6 additions & 12 deletions logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
Bindings,
Log,
LogBody,
LogMetaData,
LogPayload
} from "./log.interface.ts";
import type { Papyrus } from "./papyrus.ts";
Expand All @@ -21,7 +20,7 @@ import type { Transport, TransportOptions } from "./transport.interface.ts";
export abstract class Logger {
protected readonly configuration: Configuration;

private readonly logMetaData: LogMetaData = {_v: LOG_VERSION};
private readonly _v: number = LOG_VERSION;

constructor(options?: string | PapyrusOptions, parent?: Papyrus) {
const opts = (!options || typeof options === "string") ? {name: options} : options;
Expand All @@ -44,10 +43,7 @@ export abstract class Logger {
const baseLog: BaseLog = this.buildBaseLog(level);
const logBody: LogBody = this.buildBody(baseLog, ...data);

// The log is created with metadata in the prototype
// -> metadata can be accessed but are not visible when printing the log.
return Object.assign(
Object.create(this.logMetaData),
baseLog,
logBody,
);
Expand Down Expand Up @@ -94,8 +90,7 @@ export abstract class Logger {
const bindings: Bindings = filterKeys(
this.configuration.mergeBindings,
this.configuration.bindings,
baseLog,
this.logMetaData
baseLog
);
return this.configuration.mergeBindings ? bindings : {bindings};
}
Expand Down Expand Up @@ -150,16 +145,15 @@ export abstract class Logger {
const payload: KeyValuePair = filterKeys(
this.configuration.mergePayload,
Object.assign({}, ...rawPayload),
baseLog,
this.logMetaData
baseLog
);
return this.configuration.mergePayload ? payload : {payload};
}

/** Call a formatter, then convert to JSON */
private format(log: Log): string {
const formattedLog: string | Log = this.configuration.formatter
? this.configuration.formatter.format(log)
? this.configuration.formatter.format(log, this._v)
: log;

return typeof formattedLog !== "string"
Expand All @@ -173,10 +167,10 @@ export abstract class Logger {
const formatter: Formatter | void = transportOptions.formatter;

let formattedLog: string | Log = formatter
? formatter.format(log)
? formatter.format(log, this._v)
: log;

transport.log(typeof formattedLog === "string" ? formattedLog : JSON.stringify(formattedLog));
transport.log(typeof formattedLog === "string" ? formattedLog : JSON.stringify(formattedLog), this._v);
}

/** Send the log to the transports */
Expand Down
2 changes: 1 addition & 1 deletion transport.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export interface TransportOptions {
}

export interface Transport {
log: (data: string) => void;
log: (data: string, _v: number) => void;
}

0 comments on commit 06ef8b6

Please sign in to comment.