1 | /**
|
2 | * Copyright 2014 Skytap Inc.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | **/
|
16 |
|
17 | var _ = require('underscore'),
|
18 | path = require('path');
|
19 |
|
20 | /**
|
21 | * Module to handle application logging.
|
22 | **/
|
23 | var Logger = {
|
24 |
|
25 | levels : [
|
26 | 'debug',
|
27 | 'info',
|
28 | 'warn',
|
29 | 'error'
|
30 | ],
|
31 |
|
32 | loggers : [],
|
33 |
|
34 | //////////////////////////////////////////////////////////////////////////
|
35 | // Public methods ///////////////////////////////////////////////////////
|
36 | ////////////////////////////////////////////////////////////////////////
|
37 |
|
38 | /**
|
39 | * Logger.initialize()
|
40 | *
|
41 | * Initialize the logger.
|
42 | **/
|
43 | initialize : function (options) {
|
44 | this._options = options;
|
45 |
|
46 | if (options.loggers && _.isArray(options.loggers) && options.loggers.length > 0) {
|
47 | this.loggers = this._loadLoggers(options.loggers);
|
48 | }
|
49 |
|
50 | this._addLevelHandlers();
|
51 | },
|
52 |
|
53 | /**
|
54 | * Logger.profile(name, start)
|
55 | * - name (String)
|
56 | * - start (Integer): Start time in milliseconds
|
57 | *
|
58 | * Output a debug log of the number of milliseconds it took to complete a task.
|
59 | **/
|
60 | profile : function (name, start, end, contextId) {
|
61 | if (typeof end === 'undefined') {
|
62 | end = Date.now();
|
63 | }
|
64 | this._log('debug', 'Performance: ' + name + ' took ' + (end - start) + 'ms', null, contextId);
|
65 | },
|
66 |
|
67 | debug: function(){
|
68 | },
|
69 |
|
70 | error: function(){
|
71 | },
|
72 |
|
73 | //////////////////////////////////////////////////////////////////////////
|
74 | // Pseudo-private methods ///////////////////////////////////////////////
|
75 | ////////////////////////////////////////////////////////////////////////
|
76 |
|
77 | /**
|
78 | * Logger._addLevelHandlers()
|
79 | **/
|
80 | _addLevelHandlers : function () {
|
81 | var self = this;
|
82 |
|
83 | this.levels.forEach(function (level) {
|
84 | self[level] = function () {
|
85 | var args = Array.prototype.slice.call(arguments);
|
86 | args.unshift(level)
|
87 | self._log.apply(self, args);
|
88 | };
|
89 | });
|
90 | },
|
91 |
|
92 | /**
|
93 | * Logger._loadLoggers(loggers) -> Array
|
94 | * - loggers (Array): Array of logger names
|
95 | **/
|
96 | _loadLoggers : function (loggers) {
|
97 | var self = this,
|
98 | results = [];
|
99 |
|
100 | loggers.forEach(function processLogger (logger) {
|
101 | results.push(self._loadLogger(logger));
|
102 | });
|
103 |
|
104 | return results;
|
105 | },
|
106 |
|
107 | /**
|
108 | * Logger._loadLogger(file) -> Object
|
109 | * - file (Mixed): Either the name of a logger or a logger class
|
110 | **/
|
111 | _loadLogger : function (file) {
|
112 | var loggerPath,
|
113 | Logger;
|
114 |
|
115 | if (typeof file === 'function') {
|
116 | Logger = file;
|
117 | } else {
|
118 | loggerPath = path.join(this._options.basePath, 'lib', 'loggers'),
|
119 | Logger = require(path.join(loggerPath, file + '_logger'));
|
120 | }
|
121 |
|
122 | return new Logger(this.levels);
|
123 | },
|
124 |
|
125 | /**
|
126 | * Logger._log()
|
127 | *
|
128 | * Log a message using the specified loggers.
|
129 | **/
|
130 | _log : function () {
|
131 | var args = arguments;
|
132 | this.loggers.forEach(function logMessage (logger) {
|
133 | logger.log.apply(logger, args);
|
134 | });
|
135 | }
|
136 | };
|
137 |
|
138 | module.exports = Logger;
|