Skip to content

Commit

Permalink
Merge pull request #37 from danBamikiya/db 🔀
Browse files Browse the repository at this point in the history
Feat: add custom log levels 🔊
  • Loading branch information
danBamikiya authored Aug 18, 2021
2 parents 5c613a0 + f7464e1 commit bfaef28
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
22 changes: 20 additions & 2 deletions server/src/config/logger.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import winston from 'winston'
import Sentry from 'winston-transport-sentry-node'
import { SENTRY_DSN } from './common'
import logLevel from '../utils/logLevel'
import { logLevel, logLevels } from '../utils/logLevel'

winston.addColors(logLevels.colors)

const logger = winston.createLogger({
levels: logLevels.levels,
level: logLevel(),
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.json(),
winston.format.colorize()
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.printf(info => {
return (
Object.keys(info)
.reverse()
.reduce((acc, key, i) => {
if (typeof key === 'string') {
if (i > 0) acc += ', '
acc += `"${key}": "${info[key]}"`
}

return acc
}, '{ ') + ' }'
)
})
),
transports: [
new winston.transports.Console({ handleExceptions: true }),
Expand Down
7 changes: 4 additions & 3 deletions server/src/middlewares/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import winston from 'winston'
import expressWinston from 'express-winston'
import Sentry from 'winston-transport-sentry-node'
import { SENTRY_DSN } from '../config/common'
import logLevel from '../utils/logLevel'
import { logLevel } from '../utils/logLevel'

const handleLogging = (router: Router) =>
router.use(
expressWinston.logger({
msg: 'HTTP {{req.method}} {{req.url}}',
format: winston.format.combine(
winston.format.json(),
winston.format.colorize({ all: true }),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
winston.format.json(),
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.printf(
info => `${info.timestamp} ${info.level}: ${info.message}`
)
Expand Down
33 changes: 32 additions & 1 deletion server/src/utils/logLevel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
/**
* @desc
* Only log levels less than or equal to this level.
*/
const logLevel = () => {
const env = process.env.NODE_ENV || 'development'
return env === 'development' ? 'debug' : 'warn'
}

export default logLevel
/**
* @desc
* Levels (and colors) representing log priorities.
*/
const logLevels = {
levels: {
emerg: 0,
alert: 1,
crit: 2,
error: 3,
warn: 4,
notice: 5,
info: 6,
debug: 7
},
colors: {
emerg: 'bold red',
alert: 'bold yellow',
crit: 'bold red',
error: 'bold red',
warn: 'bold red',
notice: 'yellow',
info: 'green',
debug: 'blue'
}
}

export { logLevel, logLevels }

0 comments on commit bfaef28

Please sign in to comment.