Created
April 6, 2016 08:57
-
-
Save leegee/81eef4b1d854a6fcf5f5e63a5f33847c to your computer and use it in GitHub Desktop.
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
/* eslint-env node, es6 */ | |
'use strict'; | |
/* @param {object|string} _logger - an instance of Log4js/object with a `log` method. If a string, should be a category (see Log4js.getLogger(category)). | |
@example | |
logger = require('logger').getLogger( gutil ); | |
*/ | |
const log4js = require('log4js'); | |
/** Calls log4js.configure on the supplied path, | |
* but adding to the category filter's exclude list | |
* the CSV potentially stored in the env var LOG_EXC | |
* Expects something like: | |
log4js.configure({ | |
appenders: [ | |
{ | |
type: 'categoryFilter', | |
exclude: ['results'], | |
layout: { | |
type: 'pattern', | |
pattern: '%[%-10c %-5p%] %m' | |
}, | |
appender: { | |
type: 'console' | |
} | |
} | |
], | |
replaceConsole: true | |
}); | |
...where config can be a path | |
*/ | |
module.exports.configure = function (pathOrConfig) { | |
if (process.env.LOG_EXC) { | |
let exc = process.env.LOG_EXC.replace(/\s+/, ''); | |
if (exc.length > 0 ) { | |
if (typeof pathOrConfig === 'string') { | |
let configJson = require('fs').readFileSync( pathOrConfig ); | |
pathOrConfig = JSON.parse( configJson ); | |
} | |
pathOrConfig.appenders.forEach( (appender) => { | |
if (appender.type === 'categoryFilter') { | |
appender.exclude = appender.exclude || []; | |
appender.exclude.push( exc ); | |
} | |
}); | |
} | |
} | |
log4js.configure( pathOrConfig ); | |
}; | |
/* @param {string|object} _logger Log4js category, or Log4js object; or object with .log method (ie gutil */ | |
module.exports.getLogger = function (_logger) { | |
var logger; | |
if (_logger) { | |
if (typeof _logger === 'string') { | |
logger = log4js.getLogger(_logger); | |
} else { | |
logger = _logger; | |
// If not an instanceof log4js, fake it | |
if (!logger.hasOwnProperty('setLevel')) { | |
'trace,debug,warn,info,error,fatal'.split(',').forEach((i) => { | |
logger[i] = logger.log; | |
}); | |
} | |
} | |
} else { | |
logger = log4js.getLogger(_logger); | |
} | |
if (logger.hasOwnProperty('setLevel')) { | |
var logLevel = process.env.LOG_LEVEL || 'TRACE'; | |
logger.setLevel(logLevel.toUpperCase()); | |
} | |
return logger; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment