Last active
November 9, 2021 04:54
-
-
Save linhx/ccd4ce33342064caacf7fe2a19ab5098 to your computer and use it in GitHub Desktop.
Winston formater
This file contains hidden or 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
| import * as winston from 'winston'; | |
| import safeStringify from 'fast-safe-stringify'; | |
| const { combine, timestamp, errors, printf } = winston.format; | |
| // tslint:disable-next-line:no-shadowed-variable | |
| const format = printf(({ timestamp, level, label, message, stack, ...others }: | |
| { timestamp: string, level: string, label: string, message: any, stack: any, others: { [key in symbol]: string } }) => { | |
| const namespace = label ? `(${label})` : ''; | |
| const errStack = stack ? `\n${stack}` : ''; | |
| let meta = ''; | |
| if (level === 'error') { | |
| const args = others[Symbol.for('splat')]; | |
| meta = args? `\n\t${args.map(safeStringify).join('\n\t')}` : ''; | |
| } | |
| return `[${timestamp}] ${level}: ${namespace} ${message} ${meta} ${errStack}`; | |
| }) | |
| const logger = winston.createLogger({ | |
| level: 'info', | |
| format: combine( | |
| timestamp(), | |
| errors({ stack: true }), | |
| format | |
| ), | |
| transports: [ | |
| new winston.transports.File({ filename: 'error.log', level: 'error' }), | |
| new winston.transports.File({ filename: 'combined.log' }) | |
| ] | |
| }); | |
| export default logger; |
Author
linhx
commented
Sep 30, 2020

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment