Skip to content

Instantly share code, notes, and snippets.

@SeanCannon
Created March 18, 2019 21:55
Show Gist options
  • Save SeanCannon/25f91473277ee1d944f5159d3f5f3983 to your computer and use it in GitHub Desktop.
Save SeanCannon/25f91473277ee1d944f5159d3f5f3983 to your computer and use it in GitHub Desktop.
const R = require('ramda'), // 0.26.1
winston = require('winston'); // 2.4.x
const winstonConf = {
transports : [
{
transportType : 'console'
}
],
strategies : {
console : winston.transports.Console
}
};
const getTransportInstances = R.curry((winstonConf, transportConfig) => {
const transportConstructor = winstonConf.strategies[transportConfig.transportType];
return new (transportConstructor)(transportConfig);
});
const logger = new (winston.Logger)({
transports : winstonConf.transports.map(getTransportInstances(winstonConf))
});
const appendMetaToEveryLog = (logType, meta) => {
return R.partialRight(R.bind(logger[logType], logger), [{ meta }]);
};
const decorateInfoLogForPerformanceMetrics = meta => R.compose(
R.partial(appendMetaToEveryLog('info', meta)),
R.append(R.__, []),
R.concat('PERFORMANCE TIMER ')
);
module.exports = meta => ({
error : appendMetaToEveryLog('error', meta),
info : appendMetaToEveryLog('info', meta),
debug : appendMetaToEveryLog('debug', meta),
warn : appendMetaToEveryLog('warn', meta),
perfStart : decorateInfoLogForPerformanceMetrics(meta)('START'),
perfEndSuccess : decorateInfoLogForPerformanceMetrics(meta)('END (SUCCESS)'),
perfEndFail : decorateInfoLogForPerformanceMetrics(meta)('END (FAIL)')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment