| Total Complexity | 2 |
| Complexity/F | 0 |
| Lines of Code | 40 |
| Function Count | 0 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |