Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add console ansi options #66 #67

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
62 changes: 35 additions & 27 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { format, transports } from 'winston';
import { transports, format as winstonFormat } from 'winston';
import { DailyRotateFileTransport } from '../transport/rotate';
import {
ChildLoggerOptions,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}),
Expand All @@ -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;
}
Expand Down Expand Up @@ -439,6 +446,7 @@ export class MidwayBaseLogger extends WinstonLogger implements IMidwayLogger {
debug(...args) {
this.log('debug', ...args);
}

info(...args) {
this.log('info', ...args);
}
Expand Down