1.在页面中引入js文件
官网:http://log4javascript.org/index.html
```javascript
<script src="http://cdn.bootcss.com/log4javascript/1.4.3/log4javascript.min.js"></script>
```
2.
初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<script type=
"text/javascript"
>
//启用javascript 日志功能
var
logger = log4javascript.getLogger(
"main"
);
log4javascript.setEnabled(
false
);
var
appender =
new
log4javascript.InPageAppender();
appender.layout.pattern='%d{yyyy-MM-dd HH:mm:ss,SSS} [%c] %-5p - %m{1}%n';
appender.visible=
false
;
logger.addAppender(appender);
logger.debug(
"This is a debugging message from the log4javascript in-page page"
);
document.onkeydown =
function
(evt) {
var
VK_F9=120;
//兼容IE和Firefox获得keyBoardEvent对象
var
evt = (evt) ? evt : ((window.event) ? window.event :
""
);
//兼容IE和Firefox获得keyBoardEvent对象的键值
var
key = evt.keyCode?evt.keyCode:evt.which;
if
(evt.ctrlKey && evt.altKey && key == VK_F9){
if
(appender.visible){
log4javascript.setEnabled(
false
);
logger.info(
"隐藏记录日志."
);
appender.hide();
appender.visible=
false
;
}
else
{
log4javascript.setEnabled(
true
);
logger.info(
"显示记录日志."
);
appender.show();
appender.visible=
true
;
}
}
};
</script>
|
3.总结
现在你就可以在页面上通过 Ctrl+Alt+F9 来开始/显示js日志 或结束/隐藏js日志
效果图: