In short, no, not in the current version of log4javascript. However, in Firefox and Opera you can obtain a stack trace by creating an error object and passing it into any of the logging methods of Logger (log.debug(), etc). Firefox has a handy "stack" property of error objects, which log4javascript can be configured to display when displaying any error using
log4javascript.setShowStackTraces(true);
Also, in Opera, every error object contains a stack trace in the error message, so every error displayed by log4javascript in Opera already contains a stack trace.
For example, the following will give you a stack trace in Firefox and Opera:
var log = log4javascript.getDefaultLogger();
log4javascript.setShowStackTraces(true);
log.debug("Here follows a lovely stack trace", new Error("Stack trace"));
This admittedly does nothing for you in Internet Explorer or Safari. I did do some work on this a few months ago and had a working implementation, but decided not to release it as there were a number of problems:
1. Generating stack traces relies on the "caller" property of functions and the "callee" property of the arguments object. These are not universally supported across even recent browsers (the details now escape me), and are not included in any standard I know of.
2. The best you can hope to obtain at each level of the stack is to obtain the function name, which if, like me, you use a lot of anonymous functions, is entirely unhelpful (though you could use that as an argument in favour of using named functions). It's not possible to obtain the line number or file name. The only other information you can obtain is the body of the calling function, which could be occasionally useful but not desirable to be displayed in general.
In summary, a combination of patchy browser support and poor quality of output led me to drop the feature. However, you're not the first person to ask so I will take another look.
Tim
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have looked again and still consider it not worthwhile implementing a poor stack trace feature, so this will not be featuring in future versions of log4javascript unless anyone wants to persuade me otherwise.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
thanks for this great tool!
Is there any possibility to add the current function name (by reflection)
or the stack trace for each log?
thanks
Marcel
Marcel,
In short, no, not in the current version of log4javascript. However, in Firefox and Opera you can obtain a stack trace by creating an error object and passing it into any of the logging methods of Logger (log.debug(), etc). Firefox has a handy "stack" property of error objects, which log4javascript can be configured to display when displaying any error using
log4javascript.setShowStackTraces(true);
Also, in Opera, every error object contains a stack trace in the error message, so every error displayed by log4javascript in Opera already contains a stack trace.
For example, the following will give you a stack trace in Firefox and Opera:
var log = log4javascript.getDefaultLogger();
log4javascript.setShowStackTraces(true);
log.debug("Here follows a lovely stack trace", new Error("Stack trace"));
This admittedly does nothing for you in Internet Explorer or Safari. I did do some work on this a few months ago and had a working implementation, but decided not to release it as there were a number of problems:
1. Generating stack traces relies on the "caller" property of functions and the "callee" property of the arguments object. These are not universally supported across even recent browsers (the details now escape me), and are not included in any standard I know of.
2. The best you can hope to obtain at each level of the stack is to obtain the function name, which if, like me, you use a lot of anonymous functions, is entirely unhelpful (though you could use that as an argument in favour of using named functions). It's not possible to obtain the line number or file name. The only other information you can obtain is the body of the calling function, which could be occasionally useful but not desirable to be displayed in general.
In summary, a combination of patchy browser support and poor quality of output led me to drop the feature. However, you're not the first person to ask so I will take another look.
Tim
I have looked again and still consider it not worthwhile implementing a poor stack trace feature, so this will not be featuring in future versions of log4javascript unless anyone wants to persuade me otherwise.