-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logger.js
102 lines (81 loc) · 2.56 KB
/
Logger.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
/**
* App Logger module
*
* @author Max Maximov <[email protected]>
* @author Andrew Tereshko <[email protected]>
* @version 0.3
*/
define( function () {
'use strict';
// Check if yam "namespace" exists
if( !window.yam ) window.yam = {};
// prepend local console link
var _stub = function(){},
_ie = navigator.userAgent.indexOf('MSIE') != -1,
_console = window.console ? window.console : { log: _stub, debug: _stub, info: _stub, warn: _stub, error: _stub };
/**
* @class yam.Logger
*
* @type {Object}
*/
yam.Logger = {
/**
* Log levels
*/
NONE : 0,
ERROR : 1,
WARN : 2,
INFO : 3,
DEBUG : 4,
LOG : 5,
/**
* Current log level
*/
level : 1
};
// Short link
var Logger = yam.Logger;
Logger.log = function () {
if( Logger.level >= Logger.LOG ) Logger._echo( arguments, 'log' );
}
Logger.debug = function () {
if( Logger.level >= Logger.DEBUG ) Logger._echo( arguments, 'debug' );
}
Logger.info = function () {
if( Logger.level >= Logger.INFO ) Logger._echo( arguments, 'info' );
}
Logger.warn = function () {
if( Logger.level >= Logger.WARN ) Logger._echo( arguments, 'warn' );
}
Logger.error = function () {
if( Logger.level >= Logger.ERROR ) Logger._echo( arguments, 'error' );
}
/**
* @deprecated ( Very odd method )
*/
Logger.tLog = function () {
if ( (Logger.level >= Logger.LOG || Logger.timing) && Logger.ie ){
_console["log"].apply( _console, Logger._parseArgs(arguments));
}
}
Logger._parseArgs = function () {
var args = Array.prototype.slice.call( arguments[0]),
isClass = typeof args[0] == 'object' && args[0].constructor.fullName;//@todo - replace this by interface check
return ([ isClass ? '[' + args[0].constructor.fullName + ']' : args[0]].concat(args.slice(1)));
}
Logger._echo = function (args, type) {
var args = Logger._parseArgs(args);
/*@cc_on _ie = true; @*/
if (Logger.debug && window.console && window.console[type]) {
if ( _ie) {
console[type](args.join(' '));
} else {
console[type].apply(console, args);
}
}
}
//backward compatibility
if( !window.app ) window.app = {};
window.app.Logger = yam.Logger;
return Logger;
});