]> git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.js
Makes file logger append by default
[rbdr/cologne] / lib / cobalt.js
1 // Load up dependencies
2 if (typeof require === 'function') {
3 require('neon');
4 var colors = require('colors');
5 var Microtime = require('microtime');
6 }
7
8 Cobalt = Module("Cobalt");
9 Module(Cobalt, 'Logger')({});
10 Module(Cobalt, 'Formatter')({});
11
12 // Load up loggers + formatters
13 if (typeof require === 'function') {
14 // Formatters
15 require('./formatters/token.js');
16 require('./formatters/simple.js');
17
18 // Loggers
19 require('./loggers/console.js');
20 require('./loggers/socket.js');
21 require('./loggers/file.js');
22 }
23
24 Cobalt.now = function () {
25 if (typeof performance !== 'undefined' && performance.timing) {
26 return performance.timing.navigationStart + performance.now();
27 }
28
29 if (typeof Microtime !== 'undefined') {
30 return Microtime.nowDouble() * 1000;
31 }
32
33 return Date.now();
34 }
35
36 // Stringify with circular dereference.
37 Cobalt.stringify = function (object) {
38 var cache = [], stringified;
39 stringified = JSON.stringify(object, function (key, value) {
40 if (typeof value === 'object' && value !== null) {
41 if (cache.indexOf(value) !== -1) {
42 return "[Circular]";
43 }
44 cache.push(value);
45 }
46 return value;
47 });
48 cache = null;
49
50 return stringified;
51 }
52
53 Class(Cobalt, 'Console')({
54 prototype : {
55 from : "Generic Cobalt Logger",
56 version : "0.1.0",
57 currentIndent : 0,
58 indentSize : 2,
59 loggers : [],
60 separatorLength : 120,
61 currentColor : "black",
62
63 // Initialize instance of cobalt console
64 // and extend configuration.
65 init : function (config) {
66 var co = this,
67 property;
68
69 if (config) {
70 for (property in config) {
71 co[property] = config[property];
72 }
73 }
74 },
75
76 addLogger : function (logger) {
77 this.loggers.push(logger);
78 },
79
80 removeLogger : function (logger) {
81 var index;
82
83 index = this.loggers.indexOf(logger);
84 this.loggers.splice(index, 1);
85 },
86
87 // Builds a Cobalt Log Object
88 buildLog : function (item, level) {
89 var co = this, oldItem, logObject = {};
90
91 if (typeof item === "undefined" || item === null || !item._cobaltLog) {
92 logObject.message = item;
93 logObject._cobaltLog = true;
94 logObject._from = co.from;
95 logObject._level = level || 6;
96 logObject._levelString = co._levelString(logObject._level);
97 logObject._version = co.version;
98 logObject._timestamp = co.now();
99 logObject._indentLevel = co.currentIndent;
100 logObject._color = co.currentColor;
101 logObject._separator = false;
102 return logObject;
103 }
104
105 if (item._cobaltLog) {
106 item._level = level || item._level || 6;
107 item._levelString = co._levelString(item._level);
108 }
109
110 return item;
111 },
112
113 extendLog : function (extendingObject) {
114 var co = this, logObject,
115 property;
116
117 logObject = co.buildLog(undefined, 6);
118 extendingObject = extendingObject || {};
119
120 for (property in extendingObject) {
121 if (extendingObject.hasOwnProperty(property)) {
122 logObject[property] = extendingObject[property];
123 }
124 }
125
126 return logObject;
127 },
128
129 buildSeparator : function (type) {
130 var co = this;
131 return {
132 _cobaltLog : true,
133 _separator : true,
134 _version : co.version,
135 _timestamp : co.now(),
136 _separatorType : type,
137 _indentLevel : co.currentIndent,
138 _color : co.currentColor
139 }
140 },
141
142 _log : function (severity) {
143 var co = this,
144 logString,
145 logObjectArray = [],
146 i, j;
147
148 for (i = 1; i < arguments.length; i++) {
149 if (typeof arguments[i] === 'undefined') {
150 logObjectArray.push(co.buildLog("undefined", severity));
151 } else if (arguments[i] === null) {
152 logObjectArray.push(co.buildLog("null", severity));
153 } else {
154 logObjectArray.push(co.buildLog(arguments[i], severity));
155 }
156 }
157
158 for (j = 0; j < co.loggers.length; j++) {
159 co.loggers[j].log.apply(co.loggers[j], logObjectArray);
160 }
161 },
162
163 log : function () {
164 this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments)));
165 },
166
167 debug : function () {
168 this._log.apply(this, [7].concat(Array.prototype.slice.call(arguments)));
169 },
170
171 info : function () {
172 this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments)));
173 },
174
175 notice : function () {
176 this._log.apply(this, [5].concat(Array.prototype.slice.call(arguments)));
177 },
178
179 warn : function () {
180 this._log.apply(this, [4].concat(Array.prototype.slice.call(arguments)));
181 },
182
183 error : function () {
184 this._log.apply(this, [3].concat(Array.prototype.slice.call(arguments)));
185 },
186
187 dir : function () {
188 },
189
190 time : function () {
191 },
192
193 timeEnd : function () {
194 },
195
196 groupCollapsed : function () {
197 },
198
199 groupEnd : function () {
200 },
201
202 separator : function (type) {
203 var co = this;
204
205 co._log(7, co.buildSeparator(type));
206 },
207
208 space : function (lines) {
209 var co = this,
210 i;
211
212 if (typeof lines === "undefined") {
213 lines = 1;
214 }
215
216 for (i = 0; i < lines; i++) {
217 co.log(' ');
218 }
219
220 return co;
221 },
222
223 indent : function (callback) {
224 var co = this;
225
226 if (typeof callback === "function") {
227 co.currentIndent = co.currentIndent + co.indentSize;
228 callback();
229 co.currentIndent = co.currentIndent - co.indentSize;
230 } else {
231 co.currentIndent = co.currentIndent + co.indentSize;
232 }
233
234 return co;
235 },
236
237 outdent : function (callback) {
238 var co = this;
239
240 if (typeof callback === "function") {
241 co.currentIndent = co.currentIndent - co.indentSize;
242 if (co.currentIndent < 0) {
243 co.currentIndent = 0;
244 }
245
246 callback();
247
248 co.currentIndent = co.currentIndent + co.indentSize;
249 } else {
250 co.currentIndent = co.currentIndent - co.indentSize;
251 if (co.currentIndent < 0) {
252 co.currentIndent = 0;
253 }
254 }
255
256 return co;
257 },
258
259 color : function (color, callback) {
260 var co = this,
261 oldColor = co.currentColor;
262
263 if (typeof callback === "function") {
264 co.currentColor = color;
265 callback();
266 co.currentColor = oldColor;
267 } else {
268 co.currentColor = color;
269 }
270
271 return co;
272 },
273
274 // Returns the current time in microseconds.
275 now : function () {
276 if (typeof performance !== 'undefined' && performance.timing) {
277 return performance.timing.navigationStart + performance.now();
278 }
279
280 if (typeof Microtime !== 'undefined') {
281 return Microtime.nowDouble() * 1000;
282 }
283
284 return Date.now();
285 },
286
287 _levelString : function (level) {
288 switch(level) {
289 case 0:
290 return "PANIC";
291 break;
292 case 1:
293 return "ALERT"
294 break;
295 case 2:
296 return "CRIT"
297 break;
298 case 3:
299 return "ERROR"
300 break;
301 case 4:
302 return "WARN"
303 break;
304 case 5:
305 return "NOTICE"
306 break;
307 case 6:
308 return "INFO"
309 break;
310 case 7:
311 return "DEBUG"
312 break;
313 }
314 }
315 }
316 });
317
318 if (Cobalt.Console.__objectSpy) {
319 Cobalt.Console.__objectSpy.destroy();
320 }