forked from bazaarvoice/bv-ui-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (152 loc) · 4.9 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
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
/*jshint expr:true */
/*global console:false */
/**
* @fileOverview Logger provides a wrapper around the console object. It should be used for creating
* messages that will be helpful for developers. Logger methods can safely be kept in the
* code -- that is, you don't need to remove them like you need to remove console.log statements.
*
* Logger should be used sparingly outside of framework files. When you use it, be thoughtful
* about the level of logging you choose. Most messages should be at the "debug" or "log" level, so
* developers can easily silence them. Messages at the info level will generally not be silenced
* during development, and messages at the warn and error level will almost never be silenced.
*
* Logger also provides the ability to make assertions. Assertions are useful when other
* developers may use your code in unpredictable ways. You can write an assertion so a fellow
* developer will know at a glance what your code expects, and so that they will get a useful
* error message if they violate those expectations.
*
* @type {Object}
*/
var global = require('../global');
var ie = require('../ie')
var cookie = require('../cookie');
var logLevel = 4; // Default to OFF.
/**
* Set the logLevel to whatever the _bvui_debug cookie has stored.
*/
var cookieObj = {};
var debugCookie = cookie.read('_bvui_debug');
if (debugCookie) {
var val;
var values = debugCookie.split(',');
for (var i = 0; i < values.length; i++) {
val = values[i].split(':');
cookieObj[ val[0] ] = val[1];
}
}
if (cookieObj.logLevel) {
logLevel = cookieObj.logLevel;
}
var Logger = {
// Assign these constants so we don't have to remember them.
DEBUG: -1, // very noisy
LOG: 0,
INFO: 1, // recommended
WARN: 2,
ERROR: 3,
OFF: 4, // don't log anything
getLogLevel: function () {
return logLevel;
},
/**
* Only sets the log level if there is no cookie or if passed a force param.
*
* @param {[type]} level logLevel
* @param {[type]} force arg to force setting level to the level arg
*/
setLogLevel: function (level, force) {
logLevel = (!cookieObj.logLevel || force) ?
(level|| Logger.LOG) :
cookieObj.logLevel;
logger('log', [ 'Log level set to', logLevel ]);
},
debug: function () {
logLevel <= Logger.DEBUG && logger('log', arguments);
},
log: function () {
logLevel <= Logger.LOG && logger('log', arguments);
},
info: function () {
logLevel <= Logger.INFO && logger('info', arguments);
},
warn: function () {
logLevel <= Logger.WARN && logger('warn', arguments);
},
error: function () {
logLevel <= Logger.ERROR && logger('error', arguments);
},
count: function () {
logLevel < Logger.INFO && logger('count', arguments);
},
time: function () {
logLevel < Logger.WARN && logger('time', arguments);
},
timeEnd: function () {
logLevel < Logger.WARN && logger('timeEnd', arguments);
},
group: function (maxLevel) {
var args = Array.prototype.slice.call(arguments, 1);
if (logLevel <= maxLevel) {
logger('group', args);
logger('time', args);
}
},
groupEnd: function (maxLevel) {
var args = Array.prototype.slice.call(arguments, 1);
if (logLevel <= maxLevel) {
logger('groupEnd', args);
logger('timeEnd', args);
}
},
/**
* Assert that an expression is true, and throw the provided message if it is not.
*
* @param {Expression} assertion
* @param {String} message
*/
assert: function (assertion, message) {
if (logLevel === Logger.OFF) {
return;
}
// If we want to assert something computationally expensive, we can pass a function
// as the sole argument to Logger.assert. The function should return an array with
// two items: the assertion, and the message.
if (typeof assertion === 'function') {
Logger.assert.apply(null, assertion());
return;
}
if (!assertion) {
throw new Error('Assertion failed: ' + message);
}
}
};
function logger (level, args) {
if (logLevel === Logger.OFF) {
return;
}
if (!global.console) {
return;
}
// If we want to log something that's computationally expensive, we can pass a
// function to a Logger method as its sole argument. The function should
// return the arguments to be logged; the function will only ever be executed
// in the development environment.
args = [].slice.call(args);
if (typeof args[0] === 'function') {
args = args[0]();
}
if (ie) {
global.console.log(args);
return;
}
try {
(global.console[level] || global.console.log).apply(global.console, args);
}
catch (e) {
// If we are unable to call, log then we cannot do anything but silently
// fail. In reality, you will either see your logs or you wont. And if you
// don't see the logs, there are only two cases: 1 - your logLevel is
// incorrect or 2 - logging is failing.
}
}
module.exports = Logger;