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
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)

View File

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

View File

@ -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

View File

@ -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())
}
}