sails: Overwriting sails logger does not work as expected
This is for Sails v0.10.5
I wanted to overwrite the default Captain’s Log logger in my Sails application to an instance of Winston from an internal library my company uses in our node.js apps.
I was unable to find anything specifically about overwriting the logger in the official Sails docs pertaining to logging (let me know if I missed something): http://sailsjs.org/#!/documentation/concepts/Logging http://sailsjs.org/#!/documentation/reference/sails.config/sails.config.log.html http://sailsjs.org/#!/documentation/anatomy/myApp/config/log.js.html
However, the following Stack Overflow answer claims that I can use the “log.custom” config property with an instance of Winston to overwrite the default logger: http://stackoverflow.com/questions/25211183/sails-js-0-10-x-log-to-file
This correlates with the official Captain’s Log documentation which states that a “custom” property with an instance of Winston or another logger can be passed to the Captain’s Log constructor. Reference: https://github.com/balderdashy/captains-log/blob/master/README.md#configuring-a-custom-logger
And so I tried the following in my app.js:
var logger = myLib.logger;
logger.log('test'); // works
logger.debug('test'); // works
sails.lift({
log: {
custom: logger
}
}, function(err, sails) {
sails.log('testing');
});
But got the following error:
error: uncaughtException: Unsupported logger override!
(has no `.log()` or `.debug()` method.) date=Thu Feb 26 2015 15:10:38 GMT-0700 (MST), pid=25243, uid=0, gid=0, cwd=/home/felix/Development/work/repiscore/repiscore-web-app, execPath=/usr/bin/nodejs, version=v0.10.29, argv=[node, /home/felix/Development/work/repiscore/repiscore-web-app/app], rss=27459584, heapTotal=18635008, heapUsed=9721784, loadavg=[1.13427734375, 0.8935546875, 0.97607421875], uptime=217524.947277618, trace=[column=10, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/captains-log/index.js, function=new CaptainsLog, line=53, method=null, native=false, column=25, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/lib/app/configuration/load.js, function=Array.async.auto.logger [as 1], line=94, method=async.auto.logger [as 1], native=false, column=38, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js, function=null, line=459, method=null, native=false, column=null, file=null, function=Array.forEach, line=null, method=forEach, native=true, column=24, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js, function=_each, line=32, method=null, native=false, column=9, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js, function=Object.async.auto, line=430, method=async.auto, native=false, column=11, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/lib/app/configuration/load.js, function=Configuration.loadConfig, line=35, method=loadConfig, native=false, column=21, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/lodash/dist/lodash.js, function=Array.bound [as 0], line=729, method=bound [as 0], native=false, column=38, file=/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js, function=null, line=459, method=null, native=false, column=null, file=null, function=Array.forEach, line=null, method=forEach, native=true], stack=[Error: Unsupported logger override!, (has no `.log()` or `.debug()` method.), at new CaptainsLog (/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/captains-log/index.js:53:10), at Array.async.auto.logger [as 1] (/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/lib/app/configuration/load.js:94:25), at /home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js:459:38, at Array.forEach (native), at _each (/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js:32:24), at Object.async.auto (/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js:430:9), at Configuration.loadConfig (/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/lib/app/configuration/load.js:35:11), at Array.bound [as 0] (/home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21), at /home/felix/Development/work/repiscore/repiscore-web-app/node_modules/sails/node_modules/async/lib/async.js:459:38, at Array.forEach (native)]
The error seems to suggest that Sails tried to load a logger from the “custom” property, but the instance I passed did not have “info()” and “debug()” methods, so it’s not a valid logger, even though I had called those methods on the logger right before passing it to the Sails config.
What’s strange is that if I passed a custom logger with just those methods, Sails had no problem with it and worked as expected:
sails.lift({
log: {
custom: {
info: function(text) {
console.log('INFO >> ' + text);
},
debug: function(text) {
console.log('DEBUG >> ' + text);
}
}
}
}, function(err, sails) {
sails.log('testing');
});
This might mean the problem isn’t with the “config.log.custom” property, but rather with my specific instance of Winston or the fact that I passed in an instance of Winston.
On a hunch, I decided set my Winston logger to “config.log”, and this appears to be working great (respects the logging options and transports on my Winston logger).
var logger = myLib.logger;
logger.log('test');
logger.debug('test');
sails.lift({
log: logger
}, function(err, sails) {
sails.log('test');
});
However I am confused about whether this is a supported feature or just a hack I stumbled upon that might get removed in future versions of Sails.
Since overwriting the logger isn’t really covered in Sail’s documentation, though it is mentioned in the docs of Captain’s Log, which is a Sails sub-project, I would have expected that setting the “custom” property of “config.log” would be the proper way of overwriting the logger.
If “config.log.custom” is supposed to be a proper way of setting a custom Winston logger, then there might be a bug in v0.10.5 that prevents that from working, at least in some instances (since it gave me the above error when I passed a valid instance of Winston to it).
Also if setting “config.log” is not a hack and is supposed to be supported, it should probably be mentioned in the docs along with “config.log.custom”.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 15 (2 by maintainers)
@gotmikhail @bberry6 Have you tried setting
"inspect":falsein the log config? It voids the argument to string-serialization incaptains-log/lib/write.js.