Created
May 22, 2014 06:21
-
-
Save miguelmota/1868673cc004dfce5a69 to your computer and use it in GitHub Desktop.
Node.js Winston logger wrapper to display filename
This file contains 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
var log = require('./lib/logger')(module); | |
log.info('foo'); |
This file contains 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
var winston = require('winston'); | |
var getLogger = function(module) { | |
var path = module.filename.split('/').slice(-2).join('/'); | |
return new winston.Logger({ | |
transports: [ | |
new winston.transports.Console({ | |
colorize: true, | |
level: 'debug', | |
label: path | |
}), | |
new (winston.transports.File)({filename: 'debug.log', silent: false}) | |
] | |
}); | |
}; | |
module.exports = getLogger; |
You can also omit printing the char
(for me at least it does not contain information I would want or need in my graylog-logs) with an instant anonymous function call and javacsript object deconstruction:
// [...]
newArguments[1].msg_location = (({ method, file, line }) => ({ method, file, line }))(getCurrentLine({ frames: 3 }));
// [...]
But this is of course up to you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I found a somewhat good solution/middleway to just being able to print informative stuff with winston, without having to manually specify, where the message came from.
Please test it out, following these steps:
server.js
:utils/Test.js
):node server.js
to see the result. Please let me know, if there is anything off.