config/components/logger.js   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 40
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
wmc 2
c 1
b 0
f 0
nc 2
mnd 1
bc 2
fnc 0
dl 0
loc 40
rs 10
bpm 0
cpm 0
noi 0
1
// @see https://www.npmjs.com/package/winston
2
'use strict'
3
4
import joi from 'joi'
5
import winston from 'winston'
6
7
const envVarsSchema = joi.object({
8
  LOGGER_LEVEL: joi.string()
9
    .allow(['error', 'warn', 'info', 'verbose', 'debug', 'silly'])
10
    .default('info'),
11
  LOGGER_ENABLED: joi.boolean()
12
    .truthy('TRUE')
13
    .truthy('true')
14
    .falsy('FALSE')
15
    .falsy('false')
16
    .default(true)
17
}).unknown()
18
  .required()
19
20
const { error, value: envVars } = joi.validate(process.env, envVarsSchema)
21
if (error) {
22
  throw new Error(`Config validation error: ${error.message}`)
23
}
24
25
const config = {
26
  logger: {
27
    level: envVars.LOGGER_LEVEL,
28
    enabled: envVars.LOGGER_ENABLED
29
  }
30
}
31
32
const file = process.env.NODE_ENV || 'environment'
33
34
winston.level = config.logger.level
35
winston.add(winston.transports.File, { name: 'main', filename: `log/${file}.log` })
36
37
if (!config.logger.enabled) {
38
  winston.remove(winston.transports.Console)
39
}
40
41
module.exports = config
42