Created
August 26, 2018 04:59
-
-
Save keidrun/acbf313f2eac4719a8d5ed0d0eeecdeb to your computer and use it in GitHub Desktop.
Log4js examples
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 log4js = require('log4js'); | |
| const logger = require('./logger').access; | |
| module.exports = function(options) { | |
| options = options || {}; | |
| options.level = options.level || 'auto'; | |
| return log4js.connectLogger(logger, options); | |
| }; |
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 log4js = require('log4js'); | |
| const levels = require('log4js/lib/levels').levels; | |
| const config = require('../../config/log4js.config'); | |
| let console, system, application, access; | |
| log4js.configure(config); | |
| console = log4js.getLogger(); | |
| system = log4js.getLogger('system'); | |
| const ApplicationLogger = function() { | |
| this.logger = log4js.getLogger('application'); | |
| }; | |
| let proto = ApplicationLogger.prototype; | |
| for(let level of levels) { | |
| level = level.levelStr.toLowerCase(); | |
| proto[level] = (function(level) { | |
| return function(key, message) { | |
| const logger = this.logger; | |
| logger.addContext('key', key); | |
| logger[level](message); | |
| }; | |
| })(level); | |
| } | |
| application = new ApplicationLogger(); | |
| access = log4js.getLogger('access'); | |
| module.exports = { | |
| console, | |
| system, | |
| application, | |
| access | |
| }; |
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').system; | |
| module.exports = function(options) { | |
| return function(err, req, res, next) { | |
| logger.error(err.message); | |
| next(err); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment