Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 1x 1x 1x 1x | import { Format } from 'logform';
import { addColors, createLogger, format, Logger, transports } from 'winston';
import { IBaseLogger, ILogInput } from '../shared/interface';
import { getCommonData } from '../shared/utils';
import { DEFAULT_OPTIONS, DEFAULT_REDACT_OPTIONS } from './const';
import { IWinstonLoggerOptions } from './interface';
export class WinstonLogger implements IBaseLogger {
logger: Logger;
options: IWinstonLoggerOptions;
constructor(options?: IWinstonLoggerOptions) {
Iif (options?.colorEnabled)
addColors({
info: 'cyan',
warn: 'yellow',
error: 'red',
});
const FORMAT: Format[] = [
format.timestamp({ format: 'DD/MM/YYYY, h:mm:ss A' }),
format.label({ label: '[Server]' }),
];
Iif (options?.redact?.enabled ?? DEFAULT_REDACT_OPTIONS.enabled)
FORMAT.push(
format((info) => {
const censorKeys = options?.redact?.paths || DEFAULT_REDACT_OPTIONS.paths;
censorKeys.forEach((key) => {
Iif (info.data && Object.keys(info.data).includes(key))
info.data[key] = options?.redact?.censor ?? DEFAULT_REDACT_OPTIONS.censor;
});
return info;
})(),
);
if (options?.prettyEnabled)
FORMAT.push(
format.printf(({ label, timestamp, level, message, data }) => {
const textDisplay = [
`${label} - ${timestamp}, ${level.toUpperCase()} ${message}`,
data ? JSON.stringify(data) : undefined,
]
.filter(Boolean)
.join(' | ');
return options?.colorEnabled
? format.colorize().colorize(level, textDisplay)
: textDisplay;
}),
);
else FORMAT.push(format.json());
this.logger = createLogger({
level: options?.level ?? DEFAULT_OPTIONS.level,
silent: !(options?.enabled ?? DEFAULT_OPTIONS.enabled),
format: format.combine(...FORMAT),
transports: [new transports.Console()],
});
}
info(message: string, data?: ILogInput) {
this.logger.info(message, getCommonData({ data }));
}
debug(message: string, data?: ILogInput) {
this.logger.debug(message, getCommonData({ data }));
}
warn(message: string, data?: ILogInput) {
this.logger.warn(message, getCommonData({ data }));
}
error(message: string, data?: ILogInput) {
this.logger.error(message, getCommonData({ data }));
}
fatal(message: string, data?: ILogInput) {
this.logger.crit(message, getCommonData({ data }));
}
}
|