pimterry / loglevel

:ledger: Minimal lightweight logging for JavaScript, adding reliable log level methods to wrap any available console.log methods
MIT License
2.62k stars 157 forks source link

Logger does not output its name? #90

Closed ericntd-legacy closed 8 years ago

ericntd-legacy commented 8 years ago

I was expecting something like:

myapp - this is a log

when I did:

var log = require('loglevel');
var logger = log.getLogger('myapp');
logger.log('this is a log');

Basically, my application is supposed to work in both browser and Node JS environment. While your library is perfect in browser environment, I need the file name or the logger name to be output for the sake of the Node JS environment too, can this be done?

pimterry commented 8 years ago

Yep, that's by design. Sadly, as soon as you start doing anything dynamic (like changing log output) at the moment when the log method is called, you need to add a wrapper layer around the built-in console methods, and if you do that you don't get perfect stack traces any more. Every message appears to get from your logging wrapper instead, not directly from the log.error() line in your code.

You can do this anyway if you like though, it's just not bundled automatically because of that issue. There's a loglevel-message-prefix plugin you can add that can do things like this, see https://github.com/NatLibFi/loglevel-message-prefix. Note that I'm not the maintainer of that, if you've got questions or problems with using that you'll have to ask over there.

ericntd-legacy commented 8 years ago

yeah, the perfect stacktrace is something I'm always after, sad that we can't have everything, perfect stacktrace and filename/ logger name.

I actually tried to write a wrapper around console.log myself so I understand what you're saying :smile: