@arashi/logger
Advanced tools
Comparing version 0.0.0-dev.1 to 0.0.0-dev.2
@@ -7,3 +7,7 @@ /// <reference types="node" /> | ||
private _logger; | ||
constructor(name?: string, logLevel?: LogLevel); | ||
private _logLocation; | ||
private _serviceName; | ||
constructor(serviceName?: string, logLevel?: LogLevel, logLocation?: string); | ||
setLogLevel(level: LogLevel): void; | ||
getLogLevel(): LogLevel; | ||
addFilter(reg: RegExp): void; | ||
@@ -16,10 +20,13 @@ removeFilter(reg: RegExp): void; | ||
protected _shouldFilter(message: any): boolean; | ||
log(level: LogLevel, message: any): void; | ||
trace(message: any): void; | ||
debug(message: any): void; | ||
info(message: any): void; | ||
warn(message: any): void; | ||
error(message: any): void; | ||
deprecate(alternative?: string, methodOverride?: string): void; | ||
deprecateParameterType(argumentLocation: number, deprecatedType: string, alternative?: string): void; | ||
private _parseMessage; | ||
log(level: LogLevel, component: string, message: any, metadata?: Record<any, any>): void; | ||
trace(component: string, message: any, metadata?: Record<any, any>): void; | ||
silly(component: string, message: any, metadata?: Record<any, any>): void; | ||
debug(component: string, message: any, metadata?: Record<any, any>): void; | ||
verbose(component: string, message: any, metadata?: Record<any, any>): void; | ||
info(component: string, message: any, metadata?: Record<any, any>): void; | ||
warn(component: string, message: any, metadata?: Record<any, any>): void; | ||
error(component: string, message: any, metadata?: Record<any, any>): void; | ||
deprecate(component: string, alternative?: string, methodOverride?: string): void; | ||
deprecateParameterType(component: string, argumentLocation: number, deprecatedType: string, alternative?: string): void; | ||
private _getDeprecatedMethodMessage; | ||
@@ -26,0 +33,0 @@ private _getDeprecatedAlternativeMessage; |
@@ -9,6 +9,27 @@ "use strict"; | ||
const LogEvent_1 = require("./LogEvent"); | ||
const Path = require("path"); | ||
const F_RESET = '\x1b[0m'; | ||
const F_DIM = '\x1b[2m'; | ||
const F_FG_BLUE = '\x1b[34m'; | ||
const F_FG_CYAN = '\x1b[36m'; | ||
class Logger extends events_1.EventEmitter { | ||
constructor(name = '', logLevel = LogLevel_1.LogLevel.INFO) { | ||
constructor(serviceName = 'Generic', logLevel = LogLevel_1.LogLevel.INFO, logLocation) { | ||
super(); | ||
if (!logLocation) { | ||
logLocation = process.cwd(); | ||
} | ||
this._logLocation = Path.resolve(logLocation); | ||
this._filters = this._getDefaultLogFilters(); | ||
this._serviceName = serviceName; | ||
let format = Winston.format((info, opts) => { | ||
const MESSAGE = Symbol.for('message'); | ||
info[MESSAGE] = `${F_DIM}${info.timestamp}${F_RESET} - [${F_FG_BLUE}${info.service}${F_RESET}][${F_FG_CYAN}${info.component}${F_RESET}]: ${info.level}: ${info.message}`; | ||
return info; | ||
})(); | ||
let consoleTransport = new Winston.transports.Console({ | ||
format: Winston.format.combine(Winston.format.colorize(), format, Winston.format.errors({ stack: true })) | ||
}); | ||
consoleTransport.on('logged', (log) => { | ||
this.emit(LogEvent_1.LogEvent.LOG, log.message); | ||
}); | ||
this._logger = Winston.createLogger({ | ||
@@ -18,16 +39,49 @@ level: logLevel, | ||
format: 'YYYY-MM-DD HH:mm:ss' | ||
}), Winston.format.errors({ stack: true }), Winston.format.splat(), Winston.format.json()), | ||
defaultMeta: { service: name }, | ||
}), Winston.format.errors({ stack: true })), | ||
defaultMeta: { | ||
service: serviceName | ||
}, | ||
transports: [ | ||
new Winston.transports.Console({ | ||
format: Winston.format.combine(Winston.format.colorize(), Winston.format.simple()) | ||
consoleTransport, | ||
new Winston.transports.File({ | ||
filename: Path.resolve(this._logLocation, `${serviceName}.json.log`), | ||
level: logLevel, | ||
format: Winston.format.combine(Winston.format.json(), Winston.format.errors({ stack: true }), Winston.format((info, opts) => { | ||
const MESSAGE = Symbol.for('message'); | ||
info[MESSAGE] = utils.inspect({ | ||
level: info.level, | ||
message: info.message, | ||
timestamp: info.timestamp, | ||
service: info.service, | ||
component: info.component, | ||
meta: info.meta | ||
}, { | ||
depth: Infinity, | ||
colors: false, | ||
maxArrayLength: Infinity, | ||
showProxy: true, | ||
breakLength: Infinity | ||
}); | ||
return info; | ||
})()) | ||
}), | ||
new Winston.transports.File({ | ||
filename: Path.resolve(this._logLocation, `${serviceName}.log`), | ||
level: logLevel, | ||
format: Winston.format.combine(Winston.format.simple(), Winston.format.errors({ stack: true }), format) | ||
}), | ||
new Winston.transports.File({ | ||
filename: Path.resolve(this._logLocation, `${serviceName}.errors.log`), | ||
level: LogLevel_1.LogLevel.WARN, | ||
format: Winston.format.combine(Winston.format.simple(), Winston.format.errors({ stack: true }), format) | ||
}) | ||
] | ||
}); | ||
this._logger.stream({ | ||
start: -1 | ||
}).on('log', (log) => { | ||
this.emit(LogEvent_1.LogEvent.LOG, log); | ||
}); | ||
} | ||
setLogLevel(level) { | ||
this._logger.level = level; | ||
} | ||
getLogLevel() { | ||
return this._logger.level; | ||
} | ||
addFilter(reg) { | ||
@@ -74,26 +128,47 @@ this._filters.push(reg); | ||
} | ||
log(level, message) { | ||
if (this._shouldFilter(message)) { | ||
this._logger.log(level, utils.inspect(message, { | ||
_parseMessage(message) { | ||
if (typeof message === 'string') { | ||
return message; | ||
} | ||
else { | ||
return utils.inspect(message, { | ||
depth: Infinity, | ||
compact: true | ||
})); | ||
}); | ||
} | ||
} | ||
trace(message) { | ||
this.log(LogLevel_1.LogLevel.SILLY, message); | ||
log(level, component, message, metadata) { | ||
if (!metadata) { | ||
metadata = {}; | ||
} | ||
if (this._shouldFilter(message)) { | ||
this._logger.log(level, this._parseMessage(message), { | ||
service: this._serviceName, | ||
component: component, | ||
meta: metadata | ||
}); | ||
} | ||
} | ||
debug(message) { | ||
this.log(LogLevel_1.LogLevel.DEBUG, message); | ||
trace(component, message, metadata) { | ||
this.silly(component, message, metadata); | ||
} | ||
info(message) { | ||
this.log(LogLevel_1.LogLevel.INFO, message); | ||
silly(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.SILLY, component, message, metadata); | ||
} | ||
warn(message) { | ||
this.log(LogLevel_1.LogLevel.WARN, message); | ||
debug(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.DEBUG, component, message, metadata); | ||
} | ||
error(message) { | ||
this.log(LogLevel_1.LogLevel.ERROR, message); | ||
verbose(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.VERBOSE, component, message, metadata); | ||
} | ||
deprecate(alternative, methodOverride) { | ||
info(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.INFO, component, message, metadata); | ||
} | ||
warn(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.WARN, component, message, metadata); | ||
} | ||
error(component, message, metadata) { | ||
this.log(LogLevel_1.LogLevel.ERROR, component, message, metadata); | ||
} | ||
deprecate(component, alternative, methodOverride) { | ||
let e = new Error(); | ||
@@ -110,7 +185,7 @@ let args = []; | ||
} | ||
args.push('\n\n'); | ||
args.push('\n'); | ||
args.push(e.stack); | ||
this.log(args, LogLevel_1.LogLevel.WARN); | ||
this.log(LogLevel_1.LogLevel.WARN, component, args.join('\n')); | ||
} | ||
deprecateParameterType(argumentLocation, deprecatedType, alternative) { | ||
deprecateParameterType(component, argumentLocation, deprecatedType, alternative) { | ||
let e = new Error(); | ||
@@ -122,5 +197,5 @@ let args = []; | ||
} | ||
args.push('\n\n'); | ||
args.push('\n'); | ||
args.push(e.stack); | ||
this.log(args, LogLevel_1.LogLevel.WARN); | ||
this.log(LogLevel_1.LogLevel.WARN, component, args.join('\n')); | ||
} | ||
@@ -127,0 +202,0 @@ _getDeprecatedMethodMessage(e) { |
{ | ||
"name": "@arashi/logger", | ||
"version": "0.0.0-dev.1", | ||
"version": "0.0.0-dev.2", | ||
"description": "Logger", | ||
@@ -5,0 +5,0 @@ "main": "./lib/api.js", |
@@ -21,12 +21,46 @@ // Copyright (C) 2020 Norman Breau | ||
import {LogEvent} from './LogEvent'; | ||
import * as Path from 'path'; | ||
const F_RESET: string = '\x1b[0m'; | ||
const F_DIM: string = '\x1b[2m'; | ||
const F_FG_BLUE: string = '\x1b[34m'; | ||
const F_FG_CYAN: string = '\x1b[36m'; | ||
export class Logger extends EventEmitter { | ||
private _filters: Array<RegExp>; | ||
private _logger: Winston.Logger; | ||
private _logLocation: string; | ||
private _serviceName: string; | ||
public constructor(name: string = '', logLevel: LogLevel = LogLevel.INFO) { | ||
public constructor(serviceName: string = 'Generic', logLevel: LogLevel = LogLevel.INFO, logLocation?: string) { | ||
super(); | ||
if (!logLocation) { | ||
logLocation = process.cwd(); | ||
} | ||
this._logLocation = Path.resolve(logLocation); | ||
this._filters = this._getDefaultLogFilters(); | ||
this._serviceName = serviceName; | ||
let format: Winston.Logform.Format = Winston.format((info: Winston.Logform.TransformableInfo, opts?: any): Winston.Logform.TransformableInfo => { | ||
// Typescript for some reason doesn't allow using symbols as indexes. | ||
const MESSAGE: any = Symbol.for('message'); | ||
info[MESSAGE] = `${F_DIM}${info.timestamp}${F_RESET} - [${F_FG_BLUE}${info.service}${F_RESET}][${F_FG_CYAN}${info.component}${F_RESET}]: ${info.level}: ${info.message}` | ||
return info; | ||
})(); | ||
let consoleTransport: Winston.transports.ConsoleTransportInstance = new Winston.transports.Console({ | ||
format: Winston.format.combine( | ||
Winston.format.colorize(), | ||
format, | ||
Winston.format.errors({ stack: true }) | ||
) | ||
}); | ||
consoleTransport.on('logged', (log: Winston.LogEntry) => { | ||
this.emit(LogEvent.LOG, log.message); | ||
}); | ||
this._logger = Winston.createLogger({ | ||
@@ -38,24 +72,65 @@ level: logLevel, | ||
}), | ||
Winston.format.errors({ stack: true }), | ||
Winston.format.splat(), | ||
Winston.format.json() | ||
Winston.format.errors({ stack: true }) | ||
), | ||
defaultMeta: { service: name }, | ||
defaultMeta: { | ||
service: serviceName | ||
}, | ||
transports: [ | ||
new Winston.transports.Console({ | ||
consoleTransport, | ||
new Winston.transports.File({ | ||
filename: Path.resolve(this._logLocation, `${serviceName}.json.log`), | ||
level: logLevel, | ||
format: Winston.format.combine( | ||
Winston.format.colorize(), | ||
Winston.format.simple() | ||
Winston.format.json(), | ||
Winston.format.errors({ stack: true }), | ||
Winston.format((info: Winston.Logform.TransformableInfo, opts?: any): Winston.Logform.TransformableInfo => { | ||
const MESSAGE: any = Symbol.for('message'); | ||
info[MESSAGE] = utils.inspect({ | ||
level: info.level, | ||
message: info.message, | ||
timestamp: info.timestamp, | ||
service: info.service, | ||
component: info.component, | ||
meta: info.meta | ||
}, { | ||
depth: Infinity, | ||
colors: false, | ||
maxArrayLength: Infinity, | ||
showProxy: true, | ||
breakLength: Infinity | ||
}); | ||
return info; | ||
})() | ||
) | ||
}), | ||
new Winston.transports.File({ | ||
filename: Path.resolve(this._logLocation, `${serviceName}.log`), | ||
level: logLevel, | ||
format: Winston.format.combine( | ||
Winston.format.simple(), | ||
Winston.format.errors({ stack: true }), | ||
format | ||
) | ||
}), | ||
new Winston.transports.File({ | ||
filename: Path.resolve(this._logLocation, `${serviceName}.errors.log`), | ||
level: LogLevel.WARN, | ||
format: Winston.format.combine( | ||
Winston.format.simple(), | ||
Winston.format.errors({ stack: true }), | ||
format | ||
) | ||
}) | ||
] | ||
}); | ||
} | ||
this._logger.stream({ | ||
start: -1 | ||
}).on('log', (log: any) => { | ||
this.emit(LogEvent.LOG, log); | ||
}); | ||
public setLogLevel(level: LogLevel): void { | ||
this._logger.level = level; | ||
} | ||
public getLogLevel(): LogLevel { | ||
return <LogLevel> this._logger.level; | ||
} | ||
public addFilter(reg: RegExp): void { | ||
@@ -113,8 +188,11 @@ this._filters.push(reg); | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public log(level: LogLevel, message: any): void { | ||
if (this._shouldFilter(message)) { | ||
this._logger.log(level, utils.inspect(message, { | ||
private _parseMessage(message: any): string { | ||
if (typeof message === 'string') { | ||
return message; | ||
} | ||
else { | ||
return utils.inspect(message, { | ||
depth: Infinity, | ||
compact: true | ||
})); | ||
}); | ||
} | ||
@@ -124,27 +202,57 @@ } | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public trace(message: any): void { | ||
this.log(LogLevel.SILLY, message); | ||
public log(level: LogLevel, component: string, message: any, metadata?: Record<any, any>): void { | ||
if (!metadata) { | ||
metadata = {}; | ||
} | ||
if (this._shouldFilter(message)) { | ||
this._logger.log(level, this._parseMessage(message), { | ||
service: this._serviceName, | ||
component: component, | ||
meta: metadata | ||
}); | ||
} | ||
} | ||
/** | ||
* Alias for `silly` | ||
* @param message | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public debug(message: any): void { | ||
this.log(LogLevel.DEBUG, message); | ||
public trace(component: string, message: any, metadata?: Record<any, any>): void { | ||
this.silly(component, message, metadata); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public info(message: any): void { | ||
this.log(LogLevel.INFO, message); | ||
public silly(component: string, message: any, metadata?: Record<any, any>): void { | ||
this.log(LogLevel.SILLY, component, message, metadata); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public warn(message: any): void { | ||
this.log(LogLevel.WARN, message); | ||
public debug(component: string, message: any, metadata?: Record<any, any>): void { | ||
this.log(LogLevel.DEBUG, component, message, metadata); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public error(message: any): void { | ||
this.log(LogLevel.ERROR, message); | ||
public verbose(component: string, message: any, metadata?: Record<any, any>): void { | ||
this.log(LogLevel.VERBOSE, component, message, metadata); | ||
} | ||
public deprecate(alternative?: string, methodOverride?: string): void { | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public info(component: string, message: any, metadata?: Record<any, any>): void { | ||
this.log(LogLevel.INFO, component, message, metadata); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public warn(component: string, message: any, metadata?: Record<any, any>): void { | ||
this.log(LogLevel.WARN, component, message, metadata); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
public error(component: string, message: any, metadata?: Record<any, any>): void { | ||
this.log(LogLevel.ERROR, component, message, metadata); | ||
} | ||
public deprecate(component: string, alternative?: string, methodOverride?: string): void { | ||
let e: Error = new Error(); | ||
@@ -164,8 +272,8 @@ let args: any = []; | ||
args.push('\n\n'); | ||
args.push('\n'); | ||
args.push(e.stack); | ||
this.log(args, LogLevel.WARN); | ||
this.log(LogLevel.WARN, component, args.join('\n')); | ||
} | ||
public deprecateParameterType(argumentLocation: number, deprecatedType: string, alternative?: string): void { | ||
public deprecateParameterType(component: string, argumentLocation: number, deprecatedType: string, alternative?: string): void { | ||
let e: Error = new Error(); | ||
@@ -180,5 +288,5 @@ let args: any = []; | ||
args.push('\n\n'); | ||
args.push('\n'); | ||
args.push(e.stack); | ||
this.log(args, LogLevel.WARN); | ||
this.log(LogLevel.WARN, component, args.join('\n')); | ||
} | ||
@@ -185,0 +293,0 @@ |
Sorry, the diff of this file is not supported yet
70500
26
618