-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use winston-embeded format for logger
- Loading branch information
Showing
3 changed files
with
23 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,58 @@ | ||
import * as winston from 'winston'; | ||
import * as DailyRotateFile from 'winston-daily-rotate-file'; | ||
import chalk from 'chalk'; | ||
import { config } from 'config'; | ||
|
||
function pad(input: number | string, width: number, z = '0') { | ||
const n = typeof input === 'number' ? input.toString() : input; | ||
return n.padStart(width, z); | ||
} | ||
|
||
function getDateString() { | ||
const d = new Date(); | ||
return `${pad(d.getMonth() + 1, 2)}-${pad(d.getDate(), 2)} ${pad( | ||
d.getHours(), | ||
2 | ||
)}:${pad(d.getMinutes(), 2)}`; | ||
} | ||
|
||
function getLogsSubdir() { | ||
const d = new Date(); | ||
return `${d.getFullYear()}-${pad(d.getMonth() + 1, 2)}-${pad( | ||
d.getDate(), | ||
2 | ||
)}`; | ||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart( | ||
2, | ||
'0' | ||
)}-${String(d.getDate()).padStart(2, '0')}`; | ||
} | ||
|
||
function createLogger(name: string) { | ||
const print = winston.format.printf((info) => { | ||
let level; | ||
|
||
if (!config.USE_LOG_FILE) { | ||
// Do not colorize when writing to file | ||
if (info.level === 'error') { | ||
level = chalk.red(info.level.toUpperCase()); | ||
} else if (info.level === 'warn') { | ||
level = chalk.yellow(info.level.toUpperCase()); | ||
} else { | ||
level = chalk.green(info.level.toUpperCase()); | ||
} | ||
} | ||
const formats = [winston.format.errors({ stack: true })]; | ||
|
||
const message = info.stack | ||
? `${info.message}\n${info.stack}` | ||
: info.message; | ||
|
||
const log = `${getDateString()} [${ | ||
level ? level : info.level | ||
} - ${name}]: ${message}`; | ||
if (!config.USE_LOG_FILE) { | ||
formats.push(winston.format.colorize()); | ||
} | ||
|
||
return log; | ||
}); | ||
formats.push( | ||
winston.format.timestamp(), | ||
winston.format.printf((info) => { | ||
return `${info.timestamp} [${info.level} - ${name}]: ${ | ||
info.stack || info.message | ||
}`; | ||
}) | ||
); | ||
|
||
const logger = winston.createLogger({ | ||
level: 'silly', | ||
format: winston.format.combine( | ||
winston.format.errors({ stack: true, stackTraces: true }), | ||
), | ||
format: winston.format.combine(...formats), | ||
defaultMeta: { service: 'user-service' } | ||
}); | ||
|
||
if (config.USE_LOG_FILE) { | ||
// | ||
// - Write to all logs with level `info` and below to `combined.log` | ||
// - Write all logs error (and below) to `error.log`. | ||
// | ||
logger.add( | ||
new DailyRotateFile({ | ||
level: 'error', | ||
filename: `logs/${getLogsSubdir()}/${name}_error.log`, | ||
zippedArchive: true | ||
}) | ||
); | ||
|
||
logger.add( | ||
new DailyRotateFile({ | ||
filename: `logs/${getLogsSubdir()}/${name}_combined.log`, | ||
zippedArchive: true | ||
}) | ||
); | ||
} | ||
|
||
if (!config.USE_LOG_FILE) { | ||
} else { | ||
logger.add(new winston.transports.Console()); | ||
} | ||
|
||
return logger; | ||
} | ||
|
||
export const executorLogger = createLogger('Executor'); | ||
export const outputLogger = createLogger('Output'); | ||
export const batchLogger = createLogger('Batch'); | ||
export const challengerLogger = createLogger('Challenger'); | ||
export const outputLogger = createLogger('Output'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters