Skip to content

Commit

Permalink
use winston.http to log morgan, and adjust development output
Browse files Browse the repository at this point in the history
  • Loading branch information
a60814billy committed Jun 12, 2021
1 parent c27a734 commit 67bc95f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -338,6 +339,7 @@ function handleTermSignals() {
process.exit(1)
}, 5000)
}

process.on('SIGINT', handleTermSignals)
process.on('SIGTERM', handleTermSignals)
process.on('SIGQUIT', handleTermSignals)
2 changes: 1 addition & 1 deletion lib/config/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const defaultConfig = {
urlPath: '',
host: '0.0.0.0',
port: 3000,
loglevel: 'info',
loglevel: 'http',
urlAddPort: false,
allowOrigin: ['localhost'],
useSSL: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
13 changes: 8 additions & 5 deletions lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {createLogger, format, transports, Logger} from "winston";


type CodiMDLogger = Logger & {
stream: any
morganLog?: {
write: (message: string) => void
}
setLevel?: (string) => void
}

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

Expand Down

0 comments on commit 67bc95f

Please sign in to comment.