Skip to content

Commit

Permalink
refactor stringifyErrorReplacer
Browse files Browse the repository at this point in the history
  • Loading branch information
serdiukov-o-nordwhale committed Jul 14, 2020
1 parent fe2a4be commit 96b6f5c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
24 changes: 14 additions & 10 deletions src/imports/logger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// libraries
import winston from 'winston'
import { omit, isPlainObject } from 'lodash'
import { omit, isPlainObject, isError, mapKeys } from 'lodash'
import Crypto from 'crypto'
import { SPLAT } from 'triple-beam'

// configs
import ErrorsTransport from './loggerUtils/ErrorsTransport'
import stringifyErrorReplacer from './loggerUtils/stringifyErrorReplacer'
import conf from '../server/server.config'

const { format } = winston
Expand Down Expand Up @@ -36,24 +35,29 @@ const levelConfigs = {
}
}

const formatLogValue = value => {
if (!isError(value)) {
return value
}

const { name, message, stack } = value

return `${name}: ${message}\n${stack}`
}

const logger = winston.createLogger({
levels: levelConfigs.levels,
level: logLevel,
format: combine(
timestamp(),
format.errors({ stack: true }),
printf(({ level, timestamp, from, userId, ...rest }) => {
const context = rest[SPLAT]
const logPayload = mapKeys(rest, (_, key) => (key === SPLAT ? 'context' : key))
const stringifiedPayload = JSON.stringify(logPayload, (_, logValue) => formatLogValue(logValue))

return colorizer.colorize(
level,
`${timestamp} - ${level}${from ? ` (FROM ${from} ${userId || ''})` : ''}: ${JSON.stringify(
{
context,
...rest
},
stringifyErrorReplacer
)}`
`${timestamp} - ${level}${from ? ` (FROM ${from} ${userId || ''})` : ''}: ${stringifiedPayload}`
)
})
),
Expand Down
13 changes: 0 additions & 13 deletions src/imports/loggerUtils/stringifyErrorReplacer.js

This file was deleted.

0 comments on commit 96b6f5c

Please sign in to comment.