Skip to content

Instantly share code, notes, and snippets.

@mmoehrlein
Created February 17, 2019 19:35
Show Gist options
  • Select an option

  • Save mmoehrlein/78e449393955226b95ee3ea69b06b58a to your computer and use it in GitHub Desktop.

Select an option

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
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;
const logger = require('./logger');
const configuration = require('./configuration');
function initAll() {
logger.init();
configuration.init();
}
module.exports = {
initAll,
logger,
configuration
};
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