log4javascript打印log4javascript js文件的第一行。
日志消息..。log4javascript_uncompressed.js:1881
如何打印在日志消息中放置实际日志的行no?示例:日志消息..。app.js:15
发布于 2015-01-26 12:34:00
AFAIK,log4javascript中的log4javascript不为文件名/行号实现说明符。有几个用户对此提出了特性请求。
下面是您如何自己实现它的示例:https://code.google.com/p/aost/source/browse/trunk/tools/firefox-plugin/trump/chrome/content/logger.js?r=858
正如提姆所指出的,上面的链接有用于log4js的代码。
编辑:因此,根据它的提示,下面是与log4javascript一起工作的代码:
注意:这只适用于火狐!
/**
Throw a fake exception to retrieve the stack trace and analyze it
to find the line number from which this function was called
*/
var getLineNumber = function(layout, loggingReference) {
try {(0)()} catch (e) {
/* Split the stack trace */
output = e.stack.replace(/^.*?\n/,'').replace(/(?:\n@:0)?\s+$/m,'').replace(/^\(/gm,'{anon}(').split("\n");
/* The last trace in the array is the filename/line number of the line that logged this */
log_location = output.pop().split(':');
/* Extract the line number from this trace */
line = log_location[log_location.length - 2]
return line;
}
}
logger = log4javascript.getLogger("main");
/* Configure the logger object: add a pop-up appender */
var logAppender = new log4javascript.PopUpAppender();
/* Set a PatternAppender with a custom field */
var popUpLayout = new log4javascript.PatternLayout("[Line#%f] %m");
/* Use the method getLineNumber() as the value for the 0th custom field */
popUpLayout.setCustomField('line', getLineNumber);
logAppender.setLayout(popUpLayout);
logger.addAppender(logAppender);
/* A test log */
logger.info("This is a test")上面的脚本产生以下输出:
Line#31这是个测试
https://stackoverflow.com/questions/28107611
复制相似问题