From 67bc95f580604e9b693373d8781012ae9320a42d Mon Sep 17 00:00:00 2001 From: Raccoon Date: Sat, 12 Jun 2021 14:55:33 +0800 Subject: [PATCH] use winston.http to log morgan, and adjust development output --- lib/app.ts | 6 ++++-- lib/config/default.ts | 2 +- lib/config/index.ts | 4 ++-- lib/logger.ts | 13 ++++++++----- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/app.ts b/lib/app.ts index 8ac6836e6e..602c797d5b 100644 --- a/lib/app.ts +++ b/lib/app.ts @@ -75,8 +75,9 @@ app.use(expressPrometheusMetricsMiddleware({ })) // logger -app.use(morgan('combined', { - stream: logger.stream +const morganLogType = config.env === config.Environment.development ? 'dev' : 'combined' +app.use(morgan(morganLogType, { + stream: logger.morganLog, })) // socket io @@ -338,6 +339,7 @@ function handleTermSignals() { process.exit(1) }, 5000) } + process.on('SIGINT', handleTermSignals) process.on('SIGTERM', handleTermSignals) process.on('SIGQUIT', handleTermSignals) diff --git a/lib/config/default.ts b/lib/config/default.ts index ab292e17c1..8f3de324c1 100644 --- a/lib/config/default.ts +++ b/lib/config/default.ts @@ -5,7 +5,7 @@ const defaultConfig = { urlPath: '', host: '0.0.0.0', port: 3000, - loglevel: 'info', + loglevel: 'http', urlAddPort: false, allowOrigin: ['localhost'], useSSL: false, diff --git a/lib/config/index.ts b/lib/config/index.ts index c12533b8a8..2489a5717c 100644 --- a/lib/config/index.ts +++ b/lib/config/index.ts @@ -46,7 +46,7 @@ merge(config, require('./environment')) // eslint-disable-next-line @typescript-eslint/no-var-requires merge(config, require('./dockerSecret')) -if (['debug', 'verbose', 'info', 'warn', 'error'].includes(config.loglevel)) { +if (['debug', 'verbose', 'http', 'info', 'warn', 'error'].includes(config.loglevel)) { logger.setLevel(config.loglevel) } else { logger.error('Selected loglevel %s doesn\'t exist, using default level \'debug\'. Available options: debug, verbose, info, warn, error', config.loglevel) @@ -213,7 +213,7 @@ config.tmpPath = path.resolve(appRootPath, config.tmpPath) config.defaultNotePath = path.resolve(appRootPath, config.defaultNotePath) config.docsPath = path.resolve(appRootPath, config.docsPath) config.uploadsPath = path.resolve(appRootPath, config.uploadsPath) - +config.env = env // make config readonly config = deepFreeze(config) as any diff --git a/lib/logger.ts b/lib/logger.ts index fd636e4bb8..22e3df22ac 100644 --- a/lib/logger.ts +++ b/lib/logger.ts @@ -1,7 +1,10 @@ import {createLogger, format, transports, Logger} from "winston"; + type CodiMDLogger = Logger & { - stream: any + morganLog?: { + write: (message: string) => void + } setLevel?: (string) => void } @@ -23,7 +26,6 @@ if (process.env.NODE_ENV === 'development' || !process.env.NODE_ENV) { } const logger: CodiMDLogger = createLogger({ - level: 'debug', format: defaultFormatter, transports: [ new transports.Console({ @@ -33,9 +35,10 @@ const logger: CodiMDLogger = createLogger({ exitOnError: false }) -logger.stream = { - write: function (message, encoding) { - logger.info(message) +// for morgan used +logger.morganLog = { + write: buffer => { + logger.http(buffer.trim()) } }