How can i write a console log wrapper that:
- Keeping the recorded line number and file name of the log statement intact
- Provides access to all log severity methods (error, log, debug, ...) and shows them in the console as they where logged
- does provide some fallback (for example calls the log method when the browser does not support error)
- can be switched off in a central location, so I can switch off logging for production
- does handle the case that no console exists, and does not throw errors
Since logging in Java Script is so inconsistent, there must be some solution. Implementing it myself is a little bit tedious, but there seems to be no good library.
I currently found this logger that provides all the features, but it does mess up the line numbers. http://benalman.com/projects/javascript-debug-console-log/
Google Chrome will soon have a feature that will be of interest to this thread.
You can enable it now by:
Restart and enjoy :)
References:
Tests from chrom devtools
devtools Issues thread
devtools code review
Crossposting from related question (A proper wrapper for console.log with correct line number?) but with updated solution to address multiple methods.
I liked @fredrik's answer, so I rolled it up with another answer which splits the Webkit stacktrace, and merged it with @PaulIrish's safe console.log wrapper. "Standardizes" the
filename:line
to a "special object" so it stands out and looks mostly the same in FF and Chrome.Testing in fiddle: http://jsfiddle.net/drzaus/pWe6W/9/
I found a solution (requires jquery) somehwere on the Web but it does not work in most browsers. I changed it and it works in Firefox (Mac, Linux. Android), Chrome (Mac, Linux. Android) and Safari and other Android webkit browsers.
Just write the following code to a file called e.g. debug.js and include it after the inclusion of 'jquery.js' in the <head> section of your webpage and it will work after the page has loaded (document.ready). I still have to find out to allow debugging before everything is loaded (e.g. only the <head>...</head> ). The webpage has to be called with ?d=1 in the URL and when using Safari ?d=1s as I cannot make a distinction between Safari and another Webkit browser in the user agent and Safari has a different behavior in line number and file name handling than other Webkit browsers.
The function p_r(expression) logs to the window of the id #js_debug and to the console (if opened) with the file name and line number.
I would also recommend log4javascript and explain how you can still keep the information about the printed filename and line, at least in Chrome.
I am not talking about changing the filename and line printed by Chrome but you can get to the information you are interested in and append it to the log statement. My solution has been a quick hack but I think with a little more work you can get nicely formatted log statements. It probably has also a heavy performance-impact, but since you won't leave your logs activated in production this shouldn't be too much of a problem.
The Concept
In Chrome you can create an Error object which provides a stack property that shows you your current stack location and somewhere in the stack string you find the file and line number of your calling script.
For a log4javascript call the stack trace might look something like this:
And the file and line that made the log4javascript call and that i am interested in is
The Solution
I am guessing that the stack depth from the script your interested in to where the actual
console
call happens is always the same. So now you simply have to find out where theBrowserConsoleAppender
makes itswindow.console
access and add the line you are interested in to the formatted string. I did the following changes tolog4javascript_uncompressed.js
(version 1.4.2 line 1913):Now instead of
I get
It sure isn't a nice solution :), but I get what I need.
With a little more knowledge of the framework I suppose one could change the PatternLayout in a way that you can define how to print the file name/location and line number.
edit Instead of my prior solution I made some modifications to the PatternLayout.prototype.format function, so now I can use the additional option %l to define where and how I want to output the calling file and its line. I published my changes and a usage example as a Gist.
I answered this question here, but in short see the codepen for full implementation. However, this does everything you want, cross browser, no errors, correct line numbers, all available console methods, global and local control:
And use it like this:
To keep it simple, I've the below wrapper for console methods:
Also, for better logs in IE and older browsers, please read: Detailed console logging