forked from s992/chromelogger-cf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromelogger.cfc
352 lines (225 loc) · 8.86 KB
/
chromelogger.cfc
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
component {
public chromelogger function init( autoWriteHeader = true, convertObjects = true ) {
variables.version = "0.1";
variables.headerName = "X-ChromeLogger-Data";
variables.path = getMetaData( this ).path;
variables.autoWriteHeader = arguments.autoWriteHeader;
variables.convertObjects = arguments.convertObjects;
variables.backtraces = [];
variables.processed = [];
variables.logObject = {
"version" = variables.version
, "columns" = [ "log", "backtrace", "type" ]
, "rows" = []
};
return this;
}
public void function log() {
var args = argsToArray( args = arguments );
arrayPrepend( args, "log" );
_log( args );
}
public void function warn() {
var args = argsToArray( args = arguments );
arrayPrepend( args, "warn" );
_log( args );
}
public void function error() {
var args = argsToArray( args = arguments );
arrayPrepend( args, "error" );
_log( args );
}
public void function group() {
var args = argsToArray( args = arguments );
arrayPrepend( args, "group" );
_log( args );
}
public void function groupCollapsed() {
var args = argsToArray( args = arguments );
arrayPrepend( args, "groupCollapsed" );
_log( args );
}
public void function groupEnd() {
var args = argsToArray( args = arguments );
arrayPrepend( args, "groupEnd" );
_log( args );
}
private void function _log( required array args ) {
// chromelogger treats "log" and "" the same, so save a few bytes in the header by passing
// an empty string if the severity is "log."
var severity = arguments.args[ 1 ] == "log" ? "" : arguments.args[ 1 ];
var backtrace = getBacktrace();
var convertedArgs = [];
// pop off the severity now that we have it in a var.
arrayDeleteAt( arguments.args, 1 );
// no need to continue if we're not logging anything, unless we're ending a group.
if( !arrayLen( arguments.args ) && severity != "groupEnd" ) {
return;
}
if( arrayFindNoCase( variables.backtraces, backtrace ) ) {
// kill the backtrace if we've already processed a log with the same backtrace
// i.e. logging within a loop
backtrace = "";
} else {
// otherwise, stick it in our saved backtraces so we can kill the next one.
arrayAppend( variables.backtraces, backtrace );
}
for( var arg in args ) {
arrayAppend( convertedArgs, convert( arg ) );
}
addRow( convertedArgs, backtrace, severity );
}
private void function addRow( required array logs, required string backtrace, required string severity ) {
// backtrace is not useful in grouped logging, so kill it.
if( listFindNoCase( "group,groupEnd,groupCollapsed", arguments.severity ) ) {
arguments.backtrace = "";
}
arrayAppend( getLogObject().rows, [
arguments.logs
, arguments.backtrace == "" ? javaCast( "null", 0 ) : arguments.backtrace
, arguments.severity
]);
if( variables.autoWriteHeader ) {
writeHeader();
}
}
public void function writeHeader() {
if( arrayLen( getLogObject().rows ) ) {
getPageContext().getResponse().setHeader( variables.headerName, encode( getLogObject() ) );
}
}
private string function encode( required struct data ) {
return toBase64( serializeJSON( arguments.data ) );
}
// this is mostly here for unit testing purposes.
public struct function getLogObject() {
return variables.logObject;
}
public void function reset() {
getLogObject().rows = [];
}
/**
* Recurses over an object and its properties to create a struct representation of the object. This is
* more useful than just calling serializeJSON on an object (especially when used with ORM entities, where
* serializeJSON is completely broken). There's plenty of hackery here, so beware.
*/
private any function convert( required any object ) {
if( !variables.convertObjects ) {
return arguments.object;
}
var md = getMetaData( arguments.object );
// was getting some weirdness where methods were being added to the log if they were part of
// hibernate entities, which in turn causes a stack overflow when we try to serialize. discarding
// methods from the start fixes that.
if( isMethod( arguments.object ) ) {
return md.name;
}
// no need to continue if we aren't working with an object or if the passed object
// is an exception. exceptions return true in an isObject call, but they have a different
// underlying base class and don't work the same as other CFCs. better to just not screw
// with the exception and log it as is.
if( !isObject( arguments.object ) || isException( arguments.object ) ) {
return arguments.object;
}
var obj = {};
var props = {};
var prop = "";
var propval = "";
var comparison = {};
var isCollection = false;
var isStructCollection = false;
// save this object so that we don't get into an infinite loop if objects are referencing each other.
saveProcessedObject( arguments.object );
// ___class_name is a special keyword for chromelogger that allows it to print the object name all fancy-like
obj[ "___class_name" ] = md.name;
// rather than calling evaluate() on getters, we're going to inject a getter method into the object and
// use that while looping the properties. i'm not sure which is better, but i'm not really happy with either.
arguments.object[ "__chromecf_injected_getter" ] = __chromecf_injected_getter;
do {
props = structKeyExists( md, "properties" ) ? md.properties : [];
for( var i = 1; i <= arrayLen( props ); i++ ) {
prop = props[ i ];
if( structKeyExists( arguments.object, "get" & prop.name ) ) {
propval = arguments.object.__chromecf_injected_getter( prop.name );
propval = isNull( propval ) ? "" : propval;
isCollection = ( isStruct( propval ) && !isObject( propval ) ) || isArray( propval );
if( isCollection ) {
obj[ prop.name ] = [];
isStructCollection = isStruct( propval ) && !isObject( propval );
for( var item in propval ) {
item = isStructCollection ? propval[ item ] : item;
if( isRecursion( arguments.object, item ) ) {
item = getRecursionString( item );
}
arrayAppend( obj[ prop.name ], convert( item ) );
}
} else {
if( isRecursion( arguments.object, propval ) ) {
propval = getRecursionString( propval );
}
obj[ prop.name ] = convert( propval );
}
}
}
if( structKeyExists( md, "extends" ) ) {
md = md.extends;
}
} while( structKeyExists( md, "extends" ) );
// let's get rid of our injected method before someone starts asking questions..
structDelete( arguments.object, "__chromecf_injected_getter" );
return obj;
}
private void function saveProcessedObject( required any object ) {
arrayAppend( variables.processed, arguments.object );
}
private boolean function isRecursion( required any object1, required any object2 ) {
var comparison = {
obj1 = { obj = arguments.object1 }
, obj2 = { obj = arguments.object2}
};
return comparison.obj1.equals( comparison.obj2 ) || arrayFind( variables.processed, arguments.object2 );
}
private string function getRecursionString( required any object ) {
return "recursion - parent object [ #getMetaData( arguments.object ).name# ]";
}
private array function argsToArray( required struct args ) {
var argsLength = structCount( arguments.args );
var arr = [];
for( var i = 1; i <= argsLength; i++ ) {
arrayAppend( arr, arguments.args[ i ] );
}
return arr;
}
private string function getBacktrace() {
var stacktrace = createObject( "java", "java.lang.Thread" ).currentThread().getStackTrace();
var tracelength = arrayLen( stacktrace );
var item = "";
var backtrace = "";
for( var i = 1; i <= tracelength; i++ ) {
item = stacktrace[ i ];
// grab the first item in the stack trace that is a cfc/cfm file and is not the chromelogger cfc
if( reFindNoCase( "\.cf[cm]$", item.getFileName() ) && item.getFileName() != variables.path ) {
backtrace = item.getFileName() & " : " & item.getLineNumber();
break;
}
}
return backtrace;
}
private boolean function isException( required any object ) {
// all exceptions (i think?) ultimately extend java.lang.Throwable
return isInstanceOf( arguments.object, "java.lang.Throwable" );
}
private boolean function isMethod( required any object ) {
// query metadata is different from every other data type because coldfusion is awesome, or something like that.
// a query certainly isn't a method, so we don't need to jump through hoops with the MD to figure that out.
return !isQuery( arguments.object ) && structKeyExists( getMetaData( arguments.object ), "parameters" );
}
/**
* This method is injected into any logged objects in lieu of using evaluate(). As noted in
* the convert() method's comments, I'm not sure how I feel about this.
*/
private any function __chromecf_injected_getter( required string property ) {
var getter = this[ "get" & arguments.property ];
return getter();
}
}