Created
February 17, 2019 19:35
-
-
Save mmoehrlein/78e449393955226b95ee3ea69b06b58a to your computer and use it in GitHub Desktop.
a few helpers that setup a logger and config the same in different projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const nconf = require('nconf'); | |
| /* eslint import/no-unresolved: [2, { ignore: ['dotenv.*'] }] */ | |
| function init() { | |
| if ((process.env.ENVIRONMENT || process.env.NODE_ENV || '').toLowerCase().trim() !== 'production') { | |
| let dotenv; | |
| try { | |
| dotenv = require('dotenv-safe'); | |
| } catch (error) { | |
| try { | |
| dotenv = require('dotenv'); | |
| } catch (error) { | |
| dotenv = false; | |
| } | |
| } | |
| if (dotenv) { | |
| try { | |
| const dotenvexpand = require('dotenv-expand'); | |
| dotenvexpand(dotenv.config()); | |
| } catch (error) { | |
| dotenv.config(); | |
| } | |
| } | |
| } | |
| nconf.argv() | |
| .env(); | |
| } | |
| module.exports.init = init; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const logger = require('./logger'); | |
| const configuration = require('./configuration'); | |
| function initAll() { | |
| logger.init(); | |
| configuration.init(); | |
| } | |
| module.exports = { | |
| initAll, | |
| logger, | |
| configuration | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {format, loggers, transports} = require('winston'); | |
| let label = ''; | |
| function init() { | |
| const myform = format.printf(info => { | |
| return `${info.timestamp}${info.label ? ' [' + info.label + ']' : info.label} ${info.level}: ${info.message}`; | |
| }); | |
| const processInfo = format(info => { | |
| if (info.label) { | |
| label = info.label; | |
| } else { | |
| info.label = label; | |
| } | |
| return info; | |
| })(); | |
| const environment = process.env.ENVIRONMENT || process.env.NODE_ENV || ''; | |
| loggers.add('standard', { | |
| level: environment.toLowerCase().trim() === 'production' ? 'info' : 'debug', | |
| format: format.combine( | |
| format.timestamp({format: 'YYYY-MM-DDTHH:mm:ssZZ:'}), | |
| processInfo, | |
| myform | |
| ), | |
| transports: [ | |
| new transports.Console() | |
| ] | |
| }); | |
| } | |
| module.exports.init = init; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment