js日志组件~~
1 function Logger(level) {
2 if (!(this instanceof Logger)) {
3 return new Logger();
4 }
5 var ERROR = 1;
6 var INFO = 2;
7 var DEBUG = 3;
8
9 var logLevel = null;
10
11 var levels = {
12 "debug": 3,
13 "info": 2,
14 "error": 1,
15 "none": 0,
16 }
17
18 if (level === undefined) {
19 logLevel = ERROR;
20 } else {
21 logLevel = level;
22 }
23
24 this.getLevelName = function (level) {
25
26 for (var name in levels) {
27 if (levels[name] == level) {
28 return name;
29 }
30 }
31 }
32
33 this.setLevel = function (newLevel) {
34
35 logLevel = newLevel;
36 console.log("setting log level to: " + this.getLevelName(newLevel));
37 }
38
39 var processMessage = function (msg, level) {
40 if (level <= logLevel) {
41 console.log(msg);
42 }
43 }
44
45 this.error = function (msg) {
46 processMessage(msg, ERROR);
47 };
48 this.info = function (msg) {
49 processMessage(msg, INFO);
50 };
51 this.debug = function (msg) {
52 processMessage(msg, DEBUG);
53 };
54
55 var that = this;
56 }
-----------------------------------------------------------
58 var logger = Logger();
59 logger.error("haha");