I have wrapped the console API to provide granular logging levels as well as few other sugar features.
This works fine, the only problem is that firebug (or whatever other console) will always report the line number the log came from as the line the console API itself is invoked.
How would you suggest I make the console log the line number at which I call my wrapper function?
I would prefer a cross browser solution but failing that a firebug plugin could be a good start.
fyi I call my loging function like so:
db.log(db.LogLevel.WARN, "Blah Blah Blah");
So this recently came up again so I decided to revisit it.
Now I'm older and wiser it's clear to me a much better solution then what I was trying to do is to call the console functions as they are but selectively replace them with dummy functions when the level is turned down. This gives me fine grained logging and accurate line number reporting. A few features have been lost from my previous solution but I think this is an acceptable compromise.
Here's a partial snip of my new logging lib which shows the main solution
You can see the full thing here: https://github.com/antiBaconMachine/abm-log
Typically using the debug() or error() instead of log() functions will cause line numbers to be displayed. I believe the Google Chrome console works similarly. (firebug reference)
Here are two ways to wrap logging without losing context. The first is a little bit ugly from the caller's side. The second is only usable if you don't need the details of what was logged.
See the JSFiddle for a demo: http://jsfiddle.net/epQ95/1/
Interesting problem... I may have a hack for you. I can't test this right now, but I think it might work.
We know that a regular function call won't work, so I started thinking about #defines in C and macros in various other languages. Unfortunately, javascript doesn't have this, but perhaps an
eval
hack will work. I'm expecting thateval
will run the code as if it came from the same line - if not, bleh, ignore the rest of this answer.My method works like this:
eval
(yes, ew)console.log
in it and a custom message.It should look something like this:
You should be able to call it like this now: