use winston.http to log morgan, and adjust development output

Signed-off-by: Raccoon <raccoon@hackmd.io>
This commit is contained in:
Raccoon 2021-06-12 14:55:33 +08:00
parent 8fb31bd018
commit 41f0e61049
No known key found for this signature in database
GPG Key ID: 06770355DC9ECD38
4 changed files with 15 additions and 10 deletions

View File

@ -75,8 +75,9 @@ app.use(expressPrometheusMetricsMiddleware({
})) }))
// logger // logger
app.use(morgan('combined', { const morganLogType = config.env === config.Environment.development ? 'dev' : 'combined'
stream: logger.stream app.use(morgan(morganLogType, {
stream: logger.morganLog,
})) }))
// socket io // socket io
@ -338,6 +339,7 @@ function handleTermSignals() {
process.exit(1) process.exit(1)
}, 5000) }, 5000)
} }
process.on('SIGINT', handleTermSignals) process.on('SIGINT', handleTermSignals)
process.on('SIGTERM', handleTermSignals) process.on('SIGTERM', handleTermSignals)
process.on('SIGQUIT', handleTermSignals) process.on('SIGQUIT', handleTermSignals)

View File

@ -5,7 +5,7 @@ const defaultConfig = {
urlPath: '', urlPath: '',
host: '0.0.0.0', host: '0.0.0.0',
port: 3000, port: 3000,
loglevel: 'info', loglevel: 'http',
urlAddPort: false, urlAddPort: false,
allowOrigin: ['localhost'], allowOrigin: ['localhost'],
useSSL: false, useSSL: false,

View File

@ -46,7 +46,7 @@ merge(config, require('./environment'))
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
merge(config, require('./dockerSecret')) 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) logger.setLevel(config.loglevel)
} else { } else {
logger.error('Selected loglevel %s doesn\'t exist, using default level \'debug\'. Available options: debug, verbose, info, warn, error', config.loglevel) 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.defaultNotePath = path.resolve(appRootPath, config.defaultNotePath)
config.docsPath = path.resolve(appRootPath, config.docsPath) config.docsPath = path.resolve(appRootPath, config.docsPath)
config.uploadsPath = path.resolve(appRootPath, config.uploadsPath) config.uploadsPath = path.resolve(appRootPath, config.uploadsPath)
config.env = env
// make config readonly // make config readonly
config = deepFreeze(config) as any config = deepFreeze(config) as any

View File

@ -1,7 +1,10 @@
import {createLogger, format, transports, Logger} from "winston"; import {createLogger, format, transports, Logger} from "winston";
type CodiMDLogger = Logger & { type CodiMDLogger = Logger & {
stream: any morganLog?: {
write: (message: string) => void
}
setLevel?: (string) => void setLevel?: (string) => void
} }
@ -23,7 +26,6 @@ if (process.env.NODE_ENV === 'development' || !process.env.NODE_ENV) {
} }
const logger: CodiMDLogger = createLogger({ const logger: CodiMDLogger = createLogger({
level: 'debug',
format: defaultFormatter, format: defaultFormatter,
transports: [ transports: [
new transports.Console({ new transports.Console({
@ -33,9 +35,10 @@ const logger: CodiMDLogger = createLogger({
exitOnError: false exitOnError: false
}) })
logger.stream = { // for morgan used
write: function (message, encoding) { logger.morganLog = {
logger.info(message) write: buffer => {
logger.http(buffer.trim())
} }
} }