Reputation: 265
I've built a custom logging utility which displays a Log Message and the DateTime. I would like to add the line number in the source code which called the function.
Is there a way to determine which line of the HTML source a particular javascript function was fired?
Upvotes: 3
Views: 1119
Reputation: 324707
Having written a logging library (log4javascript) myself, I've considered this same problem and here are my thoughts:
The problem is that in order to get the information you want, you need an Error
object that was created on the line in question. Creating an Error
within your logging utility will only directly give you the filename and line number for the particular line in your logging utility code rather than for the line of code that made the logging call. The only way round this I can think of is parsing the stack
property of the Error
(or the message
property in Opera), which has several problems:
Error
and parsing its stack trace for every log call will add a significant performance overhead.For the purposes of log4javascript, I decided it wasn't worth implementing, but for your own needs you may decide it's worthwhile.
Upvotes: 7