-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (88 loc) · 2.63 KB
/
index.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
/**
* Loglog Dev tools
* Loglog transport for chrome dev tools
*/
;(function(){
'use strict';
var defaults = {
levelMethod: function( level ){
switch ( level ){
case 'error': return 'error';
case 'warn': return 'warn';
default: return 'log';
}
}
, levelColor: function( level ){
switch ( level ){
case 'warn': return 'orange';
case 'error': return 'red';
case 'debug': return 'green';
default: return 'blue';
}
}
// If true, will not show parents for each log entry
, disableParentPrefix: true
};
var loglogDevTools = function( options ){
options = options || {};
for ( var key in defaults ){
if ( !(key in options ) ){
options[ key ] = defaults[ key ];
}
}
var groups = [];
return function( entry ){
var args = [];
var components = entry.parents.map( function( component, i ){
return {
name: component
, cid: entry.parentOriginCids[ i ]
};
});
var componentPrefix;
if ( entry.component ){
components.push({ name: entry.component, cid: entry.originCid });
componentPrefix = (
options.disableParentPrefix ? [] : entry.parents
).concat( entry.component ).join('.')
args.push( '%c[' + componentPrefix + ']' );
args.push( 'color: ' + options.levelColor( entry.level ) );
}
args.push( entry.message );
if ( Object.keys( entry.data ).length > 0 ) {
args.push( entry.data );
}
// Find where components differs from groups
var differsAt = -1;
for ( var i = 0; i < components.length; i++ ){
if ( components[i].cid !== groups[i] ){
differsAt = i;
break;
}
}
if ( components.length < groups.length )
if ( differsAt === -1 ){
differsAt = components.length - 1;
}
if ( components.length === 0 && groups.length > 0 ){
differsAt = 0;
}
if ( differsAt > -1 ){
while ( groups.length > differsAt ){
groups.pop();
console.groupEnd();
}
for ( i = differsAt; i < components.length; i++ ){
groups.push( components[i].cid );
console.group( components[i].name );
}
}
console[
options.levelMethod( entry.level )
].apply( console, args );
};
};
if ( typeof module !== "undefined" ) module.exports = loglogDevTools;
else if ( typeof define !== "undefined" ) define( 'loglog-dev-tools', function(){ return loglogDevTools; } );
else window.loglogDevTools = loglogDevTools;
})();