diff --git a/src/interface.ts b/src/interface.ts index 69a8c91..f6d5621 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -156,6 +156,10 @@ export interface LoggerOptions { * disable symlink for json logger */ disableJSONSymlink?: boolean; + /** + * disable ansi escape sequences for console transport + */ + disableConsoleColors?: boolean; /** * Maximum file size for file, error and json logger * Maximum size of the file after which it will rotate. diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 759f319..6481b50 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -1,4 +1,4 @@ -import { format, transports } from 'winston'; +import { transports, format as winstonFormat } from 'winston'; import { DailyRotateFileTransport } from '../transport/rotate'; import { ChildLoggerOptions, @@ -175,30 +175,37 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { enableConsole(): void { if (!this.consoleTransport) { + let format; + if ( + process.env.MIDWAY_LOGGER_DISABLE_COLORS !== 'true' || + this.loggerOptions.disableConsoleColors + ) { + format = this.getDefaultPrint(); + } else { + format = winstonFormat.combine( + this.getDefaultPrint(), + winstonFormat.colorize({ + all: true, + colors: { + none: 'reset', + error: 'red', + trace: 'reset', + warn: 'yellow', + info: 'reset', + verbose: 'reset', + debug: 'blue', + silly: 'reset', + all: 'reset', + }, + }) + ); + } + this.consoleTransport = new transports.Console({ level: formatLevel( this.loggerOptions.consoleLevel || this.loggerOptions.level || 'silly' ), - format: - process.env.MIDWAY_LOGGER_DISABLE_COLORS !== 'true' - ? format.combine( - this.getDefaultPrint(), - format.colorize({ - all: true, - colors: { - none: 'reset', - error: 'red', - trace: 'reset', - warn: 'yellow', - info: 'reset', - verbose: 'reset', - debug: 'blue', - silly: 'reset', - all: 'reset', - }, - }) - ) - : this.getDefaultPrint(), + format, }); } this.add(this.consoleTransport); @@ -287,11 +294,11 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { enableJSON(): void { if (!this.jsonTransport) { this.jsonTransport = new DailyRotateFileTransport({ - format: format.combine( + format: winstonFormat.combine( customJSON({ jsonFormat: this.loggerOptions.jsonFormat, }), - format.json() + winstonFormat.json() ), dirname: this.loggerOptions.jsonDir || this.loggerOptions.dir, filename: this.loggerOptions.jsonLogName, @@ -377,11 +384,11 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { } protected getDefaultFormat() { - return format.combine( - format.timestamp({ + return winstonFormat.combine( + winstonFormat.timestamp({ format: 'YYYY-MM-DD HH:mm:ss,SSS', }), - format.splat(), + winstonFormat.splat(), displayCommonMessage({ target: this, }), @@ -390,7 +397,7 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { } private getDefaultPrint() { - return format.printf(info => { + return winstonFormat.printf(info => { if (info.ignoreFormat) { return info.message; } @@ -439,6 +446,7 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { debug(...args) { this.log('debug', ...args); } + info(...args) { this.log('info', ...args); }