]> git.r.bdr.sh - rbdr/cologne/blob - lib/loggers/console.js
c8e3036bc8497ca44d8a5abaf592bdbe348b4c22
[rbdr/cologne] / lib / loggers / console.js
1 (function (global) {
2 var Module, Class;
3
4 // Load up dependencies
5 if (typeof require === 'function') {
6 var Ne = require('neon');
7 Module = Ne.Module;
8 Class = Ne.Class;
9 } else {
10 Module = global.Module;
11 Class = global.Class;
12 }
13
14 var Cobalt = {};
15 Module(Cobalt, 'Logger')({});
16
17 Cobalt.Logger.JsConsole = Class(Cobalt.Logger, 'JsConsole')({
18 prototype : {
19 console : null,
20 formatterOpts : {},
21
22 init : function (config) {
23 var logger = this,
24 property;
25
26 if (config) {
27 for (property in config) {
28 logger[property] = config[property];
29 }
30 }
31
32 if (!logger.console) {
33 logger.console = console;
34 }
35 },
36
37 log : function () {
38 var i, message = [], severity;
39
40 for (i = 0; i < arguments.length; i++) {
41 // We're not formatting objects for now.
42
43 if (!arguments[i].__skipConsole && !arguments[i].message.__skipConsole) {
44 if (typeof arguments[i].message === 'object') {
45 message.push(arguments[i].message);
46 } else {
47 message.push(this.format(arguments[i]));
48 }
49 if (!severity) {
50 severity = arguments[i]._level
51 }
52 }
53 }
54
55 switch (severity){
56 case 0:
57 case 1:
58 case 2:
59 case 3:
60 this.console.error.apply(this.console, message);
61 break;
62 case 4:
63 this.console.warn.apply(this.console, message);
64 break;
65 case 5:
66 case 6:
67 this.console.info.apply(this.console, message);
68 break;
69 case 7:
70 default:
71 this.console.log.apply(this.console, message);
72 break;
73 }
74 },
75
76 format : function (logObject) {
77 // Usually what you want to do here is format. Preferably using
78 // someone inside Cobalt.Formatter
79 if (this.formatter) {
80 return this.formatter.format(logObject, this.formatterOpts);
81 }
82
83 return logObject.message;
84 }
85 }
86 });
87
88 if (Cobalt.Logger.JsConsole.__objectSpy) {
89 Cobalt.Logger.JsConsole.__objectSpy.destroy();
90 }
91
92 if (typeof require === 'function') {
93 global.JsConsole = Cobalt.Logger.JsConsole;
94 } else {
95 global.Cobalt.Logger.JsConsole = Cobalt.Logger.JsConsole;
96 }
97 }(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self)));