mperdeck / jsnlog.js

Tiny JavaScript logging library, simple and well documented. Lots of options to filter logging data.
js.jsnlog.com
Other
130 stars 43 forks source link

Correct line number for ConsoleAppender. #9

Closed thraxst closed 10 years ago

thraxst commented 10 years ago

Is there anyway to get the ConsoleAppender to show the correct file/line where log was called? Currently, It shows up as the line in jsnlog.js

mperdeck commented 10 years ago

I guess what you're really looking for is an easy way to get the line number of the currently executing JavaScript statement. You could then log that line number along with the other log information.

At the moment only Firefox allows you to do that. You create a new Error object, and then get its lineNumber property, which by default is set to the current line:

var line = new Error().lineNumber

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/prototype

When logging, you would use that like so:

JL("jsLogger").fatal({ msg: "log message from js", line: new Error().lineNumber });

This is not in the standard though. Chrome and IE do not let you easily get the line number. With these browsers, you still get the log message (no exception), but the line is not filled in.

An alternative to relying on line numbers would be to name your loggers to show their location, such as:

var abc = function(x) {
    JL("abc.1").fatal({ x: x });
    ...
    JL("abc.2").fatal({ y: y });
    ...
}