pimterry / loglevel

:ledger: Minimal lightweight logging for JavaScript, adding reliable log level methods to wrap any available console.log methods

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Show filename and line of logging code

pslzr opened this issue · comments

commented

when using methodFactory and modifying logging functions, the source lines are always the same (the rawMethod calling line.!!)

Is there a way to show the REAL original source code line?

var originalFactory = log.methodFactory;
log.methodFactory = function (methodName, logLevel, loggerName) {
var rawMethod = originalFactory(methodName, logLevel, loggerName);
return function (...args) {
var date = new Date();
rawMethod(dateTime(date),...args); // shows this LINE always !! ugh...
}
}

Using Edge or Chrome in Console output.. always shows that LINE..

14:35:12 bla1... log.js:130
14:36:12 bla2... log.js:130
14:37:09 bla3... log.js:130

That's because of your implementation: you're wrapping the raw method in your own function, so that is included in the source line. There's more detail in the 2nd paragraph of the plugin docs: https://github.com/pimterry/loglevel#writing-plugins.

The only way to avoid this is to call rawMethod without adding a function wrapper. For some use cases (e.g. prefixing a fixed string) you can do this by using bind instead, e.g. return rawMethod.bind(console, 'prefix') will return a log method that logs 'prefix' before every line.

For other cases though, where you want to add dynamic behaviour, what you're asking for impossible. In general you can't wrap console logging in a function but ask the browser to ignore that function.

As a workaround, you might be able to hide the lines you don't care about by configuring your browser developer tools to blackbox the script you don't want included, see https://antongunnarsson.com/devtools-blackbox/ for some details. I'm not sure if that will work in all cases though, and it depends on your browser's config - you can't build it into your logging code.

This is a limitation of browsers and JavaScript in general, not something to do with loglevel, so I'm going to close this for now.

commented

Thank you for your help. Best regards..