From 9989706edbb5e6b3efe82d41de036bc873f63c3a Mon Sep 17 00:00:00 2001 From: zhangchuang Date: Tue, 2 Aug 2022 16:22:39 +0800 Subject: [PATCH 1/2] feat: add console ansi options #66 --- src/interface.ts | 4 +++ src/logger/logger.ts | 59 ++++++++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 27 deletions(-) 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 ebbd922..57a2c27 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -1,4 +1,4 @@ -import { transports, format } from 'winston'; +import { transports, format as winstonFormat } from 'winston'; import { DailyRotateFileTransport } from '../transport/rotate'; import { LoggerLevel, @@ -165,30 +165,34 @@ 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); @@ -273,11 +277,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, @@ -362,11 +366,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, }), @@ -375,7 +379,7 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { } private getDefaultPrint() { - return format.printf(info => { + return winstonFormat.printf(info => { if (info.ignoreFormat) { return info.message; } @@ -424,6 +428,7 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { debug(...args) { this.log('debug', ...args); } + info(...args) { this.log('info', ...args); } From da1873a357b787d5fdda723d086dfe4e39b75d72 Mon Sep 17 00:00:00 2001 From: zhangchuang Date: Tue, 2 Aug 2022 18:51:32 +0800 Subject: [PATCH 2/2] fix lint --- src/logger/logger.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 57a2c27..40d4f8f 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -166,8 +166,11 @@ 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() + if ( + process.env.MIDWAY_LOGGER_DISABLE_COLORS !== 'true' || + this.loggerOptions.disableConsoleColors + ) { + format = this.getDefaultPrint(); } else { format = winstonFormat.combine( this.getDefaultPrint(), @@ -185,7 +188,7 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger { all: 'reset', }, }) - ) + ); } this.consoleTransport = new transports.Console({