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?
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
Errorobject that was created on the line in question. Creating anErrorwithin 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 thestackproperty of theError(or themessageproperty in Opera), which has several problems:Errorand 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.