-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.js
More file actions
108 lines (88 loc) · 3.14 KB
/
log.js
File metadata and controls
108 lines (88 loc) · 3.14 KB
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
(function ( name, global, definition ) {
'use strict';
if( typeof module === 'object' && typeof require === 'function' ) {
module.exports = definition( name, global ); /* commonJS */
} else if( typeof define === 'function' && typeof define.amd === 'object' ) {
define( definition ); /* AMD */
} else {
global[ name ] = definition( name, global );
}
} ( 'Log', ( typeof window !== 'undefined' && window ) || this, function definition( name, global ) {
'use strict';
// logger object that will be returned as the API
var logger = {},
// regular expression to extract file, function and line number from stack trace
//var findDetailsRegularExpression = /^\s*at\s*([\w<>\.]*)\s*\((.*?):(\d*):(\d*)\)$/; extracts line number and column
findDetailsRegularExpression = /^\s*at\s*([\w<>\.]*)\s*(.*)$/,
// all available log level
allLogLevel = [
'NONE',
'ERROR',
'WARN',
'INFO',
'DEBUG',
'TRACE',
'DATA',
'DEVELOP'
],
// initial log level used
actualLogLevel = 3, //INFO
// log method to consume logging messages
actualConsumerFunction = console.log.bind( console),
// creates the api to log messages
currentLevel = null;
var allLogLevelLength = allLogLevel.length;
for ( currentLevel = 0; currentLevel < allLogLevelLength; currentLevel += 1 ) {
if( allLogLevel[ currentLevel ] === 'NONE' ) { continue; }
logger[ allLogLevel[ currentLevel ].toLowerCase() ] = function ( currentLevel ) {
return function () {
if ( actualLogLevel < currentLevel ) { return; }
log.apply( this, arguments );
};
} ( currentLevel );
}
// set consumer of the log messages
function to( consumer ) {
actualConsumerFunction = consumer;
}
// the function that logs using an error
function log() {
var stackInfo = ( new Error() ).stack.split( '\n' )[ 3 ];
var matches = findDetailsRegularExpression.exec( stackInfo );
var callingFunction = null;
var callingOccured = null;
var logMessage = null;
if( matches === null ) {
console.log( stackInfo );
throw new Error( 'InvalidStackError', 'Stacktrace is invalid' );
} else {
callingFunction = matches[ 1 ];
callingOccured = matches[ 2 ];
logMessage = new Date().toISOString() + ': ' +
Array.prototype.join.call( arguments, ' ') + ' ' +
callingOccured + ' in ' + callingFunction;
actualConsumerFunction.call( this, logMessage );
}
}
// set the actual log level or returns it
function level( newLevel ) {
if( newLevel ) {
actualLogLevel = allLogLevel.indexOf( newLevel );
} else {
return allLogLevel[ actualLogLevel ];
}
}
// API
return {
to: to,
//all: allLogLevel,
level: level,
error: logger.error,
warn: logger.warn,
info: logger.info,
debug: logger.debug,
trace: logger.trace,
data: logger.data,
develop: logger.develop
};
} ) );