From f7464e1c460f296c9531798b66bbc81131c1613d Mon Sep 17 00:00:00 2001 From: Dan Bamikiya Date: Wed, 18 Aug 2021 15:27:41 +0000 Subject: [PATCH] feat: add custom log levels :loud_sound: * colorize log levels and messages :art: * add default message to log middleware :speech_balloon: --- server/src/config/logger.ts | 22 +++++++++++++++++++-- server/src/middlewares/logging.ts | 7 ++++--- server/src/utils/logLevel.ts | 33 ++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/server/src/config/logger.ts b/server/src/config/logger.ts index 2bbfb24..645bfe8 100644 --- a/server/src/config/logger.ts +++ b/server/src/config/logger.ts @@ -1,13 +1,31 @@ import winston from 'winston' import Sentry from 'winston-transport-sentry-node' import { SENTRY_DSN } from './common' -import logLevel from '../utils/logLevel' +import { logLevel, logLevels } from '../utils/logLevel' + +winston.addColors(logLevels.colors) const logger = winston.createLogger({ + levels: logLevels.levels, level: logLevel(), format: winston.format.combine( + winston.format.colorize({ all: true }), winston.format.json(), - winston.format.colorize() + winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), + winston.format.printf(info => { + return ( + Object.keys(info) + .reverse() + .reduce((acc, key, i) => { + if (typeof key === 'string') { + if (i > 0) acc += ', ' + acc += `"${key}": "${info[key]}"` + } + + return acc + }, '{ ') + ' }' + ) + }) ), transports: [ new winston.transports.Console({ handleExceptions: true }), diff --git a/server/src/middlewares/logging.ts b/server/src/middlewares/logging.ts index 74b8ea3..b08feb7 100644 --- a/server/src/middlewares/logging.ts +++ b/server/src/middlewares/logging.ts @@ -3,15 +3,16 @@ import winston from 'winston' import expressWinston from 'express-winston' import Sentry from 'winston-transport-sentry-node' import { SENTRY_DSN } from '../config/common' -import logLevel from '../utils/logLevel' +import { logLevel } from '../utils/logLevel' const handleLogging = (router: Router) => router.use( expressWinston.logger({ + msg: 'HTTP {{req.method}} {{req.url}}', format: winston.format.combine( - winston.format.json(), winston.format.colorize({ all: true }), - winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }), + winston.format.json(), + winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), winston.format.printf( info => `${info.timestamp} ${info.level}: ${info.message}` ) diff --git a/server/src/utils/logLevel.ts b/server/src/utils/logLevel.ts index 0418d6b..aa4f4ba 100644 --- a/server/src/utils/logLevel.ts +++ b/server/src/utils/logLevel.ts @@ -1,6 +1,37 @@ +/** + * @desc + * Only log levels less than or equal to this level. + */ const logLevel = () => { const env = process.env.NODE_ENV || 'development' return env === 'development' ? 'debug' : 'warn' } -export default logLevel +/** + * @desc + * Levels (and colors) representing log priorities. + */ +const logLevels = { + levels: { + emerg: 0, + alert: 1, + crit: 2, + error: 3, + warn: 4, + notice: 5, + info: 6, + debug: 7 + }, + colors: { + emerg: 'bold red', + alert: 'bold yellow', + crit: 'bold red', + error: 'bold red', + warn: 'bold red', + notice: 'yellow', + info: 'green', + debug: 'blue' + } +} + +export { logLevel, logLevels }