-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-log.js
228 lines (189 loc) · 8.39 KB
/
js-log.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2020 by André Kreienbring
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
const loggers = []; //stores all created loggers
const loggerProxies = []; //stores all created logger proxies
let lastClassLogger = -1; //stores the last ClassLogger that was called
let lastFunctionLogger = -1; //stores the last FunctionLogger that was called
let defaultEnabled = true; // used by the disableAll and enableAll methods. Used as default for new loggers
/**
* A handler that intercepts calls to the Logger class and is responsible for the grouping of the output.
*/
const handler = {
get: function(target, propKey, receiver) {
//target is the Proxy. Get the wrapped logger object from the target.
const logger = target.logger;
//log, info... intercept the call to a function of the logger
if(typeof target.logger[propKey] == "function"){
if(!logger.isEnabled){
//if the logger is disabled: return an empty function.
return function(){};
};
//Consolegrouping: based on the index values of the loggers.
if(logger.parentLoggerIndex == -1){
//a ClassLogger wants to output something
//Control the grouping of the classLogger
if(lastClassLogger == -1){
console.group(logger.loggerName);
lastClassLogger = logger.index;
//alert("classlogger is adding group because non exists " + logger.loggerName);
}else{
if(lastClassLogger != logger.index){
console.groupEnd();
//alert("classlogger is ending the previous group and adding a group because classlogger changed " + logger.loggerName);
if(lastFunctionLogger != -1){
console.groupEnd();
lastFunctionLogger = -1;
//alert("classlogger is ending a group because the last log came from a functionLogger of a diffrent class.");
};
console.group(logger.loggerName);
lastClassLogger = logger.index;
}else{
if(lastFunctionLogger != -1){
if (lastFunctionLogger != logger.index){
console.groupEnd();
lastFunctionLogger = -1;
//alert("classlogger is ending the previous group because this is no function logger " + logger.loggerName);
};
};
};
};
}else{
//a FunctionLogger wants to output something
//Control the grouping of the functionLogger
if(lastFunctionLogger == -1){
console.group(logger.loggerName);
lastFunctionLogger = logger.index;
//alert("functionlogger is adding group because non exists " + logger.loggerName);
}else{
if(lastFunctionLogger != logger.index){
console.groupEnd();
console.group(logger.loggerName);
lastFunctionLogger = logger.index;
//alert("functionlogger is ending the previous group and adding group because functionlogger changed " + logger.loggerName);
}else{
/* if(lastClassLogger != logger.parentLoggerIndex){
console.groupEnd();
//alert("functionlogger is ending group because parent classlogger changed. RESETTING lastFunctionLogger " + logger.loggerName);
lastFunctionLogger = -1;
};
*/ };
};
};
//(re-)binding is not neccesary, because we did on when we created the logger!
//var binding = "console." + propKey + ".bind(window.console, '" + prefix + "')";
//return eval(binding);
//return target.logger[propKey];
};
return target.logger[propKey];
}, //get handler
};
/**
* A Javascript Logging class that intercepts calls to the console with the purpose of grouping the output.
* @param loggerName The Name of the logger when constructed. Should be the name of a class or a function.
*/
class Logger{
constructor(loggerName){
this.loggerName = loggerName;
this.index; //the index of the logger in loggers[]
this.isEnabled = defaultEnabled;
this.parentLoggerIndex = -1; //indicates that this is a function logger if >= 0. Important for the grouping.
};
/**
* Disables the given logger.
* @param loggerName The Name of the logger that will be disabled.
*/
static disable(loggerName){
const logger = loggers.find( logger => logger.loggerName === loggerName);
if(!logger){
throw "A Logger with the name " + loggerName + " does not exist";
};
logger.isEnabled = false;
};
/**
* Enables the given logger.
* @param loggerName The Name of the logger that will be enabled.
*/
static enable(loggerName){
const logger = loggers.find( logger => logger.loggerName === loggerName);
if(!logger){
throw "A Logger with the name " + loggerName + " does not exist";
};
logger.isEnabled = true;
};
/**
* Disables all existing loggers. AND sets the default for the 'isEnabled' property of new loggers to false
*/
static disableAll(){
loggers.forEach(logger => logger.isEnabled = false);
defaultEnabled = false;
};
/**
* Enables all existing loggers. AND sets the default for the 'isEnabled' property of new loggers to true
*/
static enableAll(){
loggers.forEach(logger => logger.isEnabled = true);
defaultEnabled = true;
};
/**
* Creates a new ClassLogger.
* @param classLoggerName The Name of the logger
* @return An instance of Logger
*/
static getClassLogger(classLoggerName){
const classLogger = new Logger(classLoggerName);
//bind the returned function to the console. IMPORTANT: This preserves the context of the caller!
classLogger.log = console.log.bind(console, classLoggerName + ": ");
classLogger.debug = console.log.bind(console, classLoggerName + ": ");
classLogger.info = console.log.bind(console, classLoggerName + ": ");
classLogger.warn = console.log.bind(console, classLoggerName + ": ");
classLogger.error = console.log.bind(console, classLoggerName + ": ");
classLogger.index = loggers.length;
loggers.push (classLogger);
//create and return a Proxy. This is necessary to intercept calls to the logger.
const classLoggerProxy = new Proxy({logger:classLogger}, handler);
loggerProxies.push(classLoggerProxy);
return classLoggerProxy;
};
/**
* Creates a new FunctionLogger or returns an existing one. Outputs of a FunctionLogger are grouped underneath the outputs of a parent ClassLogger
* @param classLoggerName The Name of the parent ClassLogger
* @param functionLoggerName The Name of the FunctionLogger
* @return An instance of Logger
*/
static getFunctionLogger(classLoggerName, functionLoggerName){
const classLogger = loggers.find( classLogger => classLogger.loggerName === classLoggerName);
if(!classLogger){
throw "ClassLogger " + classLoggerName + " does not exist";
};
let functionLogger = loggers.find( functionLogger => functionLogger.loggerName === functionLoggerName);
if (!functionLogger){
functionLogger = new Logger(functionLoggerName);
//bind the returned function to the console. IMPORTANT: This preserves the context of the caller!
functionLogger.log = console.log.bind(console, classLoggerName + "->" + functionLoggerName + ": ");
functionLogger.debug = console.log.bind(console, classLoggerName + "->" + functionLoggerName + ": ");
functionLogger.info = console.log.bind(console, classLoggerName + "->" + functionLoggerName + ": ");
functionLogger.warn = console.log.bind(console, classLoggerName + "->" + functionLoggerName + ": ");
functionLogger.error = console.log.bind(console, classLoggerName + "->" + functionLoggerName + ": ");
functionLogger.index = loggers.length;
functionLogger.parentLoggerIndex = classLogger.index;
loggers.push(functionLogger);
const functionLoggerProxy = new Proxy({logger:functionLogger}, handler);
loggerProxies.push(functionLoggerProxy);
return functionLoggerProxy;
}else{
return loggerProxies[functionLogger.index];
};
};
}; //Class Logger
export {Logger};