source: trip-planner-front/node_modules/node-forge/lib/log.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 9.0 KB
Line 
1/**
2 * Cross-browser support for logging in a web application.
3 *
4 * @author David I. Lehn <dlehn@digitalbazaar.com>
5 *
6 * Copyright (c) 2008-2013 Digital Bazaar, Inc.
7 */
8var forge = require('./forge');
9require('./util');
10
11/* LOG API */
12module.exports = forge.log = forge.log || {};
13
14/**
15 * Application logging system.
16 *
17 * Each logger level available as it's own function of the form:
18 * forge.log.level(category, args...)
19 * The category is an arbitrary string, and the args are the same as
20 * Firebug's console.log API. By default the call will be output as:
21 * 'LEVEL [category] <args[0]>, args[1], ...'
22 * This enables proper % formatting via the first argument.
23 * Each category is enabled by default but can be enabled or disabled with
24 * the setCategoryEnabled() function.
25 */
26// list of known levels
27forge.log.levels = [
28 'none', 'error', 'warning', 'info', 'debug', 'verbose', 'max'];
29// info on the levels indexed by name:
30// index: level index
31// name: uppercased display name
32var sLevelInfo = {};
33// list of loggers
34var sLoggers = [];
35/**
36 * Standard console logger. If no console support is enabled this will
37 * remain null. Check before using.
38 */
39var sConsoleLogger = null;
40
41// logger flags
42/**
43 * Lock the level at the current value. Used in cases where user config may
44 * set the level such that only critical messages are seen but more verbose
45 * messages are needed for debugging or other purposes.
46 */
47forge.log.LEVEL_LOCKED = (1 << 1);
48/**
49 * Always call log function. By default, the logging system will check the
50 * message level against logger.level before calling the log function. This
51 * flag allows the function to do its own check.
52 */
53forge.log.NO_LEVEL_CHECK = (1 << 2);
54/**
55 * Perform message interpolation with the passed arguments. "%" style
56 * fields in log messages will be replaced by arguments as needed. Some
57 * loggers, such as Firebug, may do this automatically. The original log
58 * message will be available as 'message' and the interpolated version will
59 * be available as 'fullMessage'.
60 */
61forge.log.INTERPOLATE = (1 << 3);
62
63// setup each log level
64for(var i = 0; i < forge.log.levels.length; ++i) {
65 var level = forge.log.levels[i];
66 sLevelInfo[level] = {
67 index: i,
68 name: level.toUpperCase()
69 };
70}
71
72/**
73 * Message logger. Will dispatch a message to registered loggers as needed.
74 *
75 * @param message message object
76 */
77forge.log.logMessage = function(message) {
78 var messageLevelIndex = sLevelInfo[message.level].index;
79 for(var i = 0; i < sLoggers.length; ++i) {
80 var logger = sLoggers[i];
81 if(logger.flags & forge.log.NO_LEVEL_CHECK) {
82 logger.f(message);
83 } else {
84 // get logger level
85 var loggerLevelIndex = sLevelInfo[logger.level].index;
86 // check level
87 if(messageLevelIndex <= loggerLevelIndex) {
88 // message critical enough, call logger
89 logger.f(logger, message);
90 }
91 }
92 }
93};
94
95/**
96 * Sets the 'standard' key on a message object to:
97 * "LEVEL [category] " + message
98 *
99 * @param message a message log object
100 */
101forge.log.prepareStandard = function(message) {
102 if(!('standard' in message)) {
103 message.standard =
104 sLevelInfo[message.level].name +
105 //' ' + +message.timestamp +
106 ' [' + message.category + '] ' +
107 message.message;
108 }
109};
110
111/**
112 * Sets the 'full' key on a message object to the original message
113 * interpolated via % formatting with the message arguments.
114 *
115 * @param message a message log object.
116 */
117forge.log.prepareFull = function(message) {
118 if(!('full' in message)) {
119 // copy args and insert message at the front
120 var args = [message.message];
121 args = args.concat([] || message['arguments']);
122 // format the message
123 message.full = forge.util.format.apply(this, args);
124 }
125};
126
127/**
128 * Applies both preparseStandard() and prepareFull() to a message object and
129 * store result in 'standardFull'.
130 *
131 * @param message a message log object.
132 */
133forge.log.prepareStandardFull = function(message) {
134 if(!('standardFull' in message)) {
135 // FIXME implement 'standardFull' logging
136 forge.log.prepareStandard(message);
137 message.standardFull = message.standard;
138 }
139};
140
141// create log level functions
142if(true) {
143 // levels for which we want functions
144 var levels = ['error', 'warning', 'info', 'debug', 'verbose'];
145 for(var i = 0; i < levels.length; ++i) {
146 // wrap in a function to ensure proper level var is passed
147 (function(level) {
148 // create function for this level
149 forge.log[level] = function(category, message/*, args...*/) {
150 // convert arguments to real array, remove category and message
151 var args = Array.prototype.slice.call(arguments).slice(2);
152 // create message object
153 // Note: interpolation and standard formatting is done lazily
154 var msg = {
155 timestamp: new Date(),
156 level: level,
157 category: category,
158 message: message,
159 'arguments': args
160 /*standard*/
161 /*full*/
162 /*fullMessage*/
163 };
164 // process this message
165 forge.log.logMessage(msg);
166 };
167 })(levels[i]);
168 }
169}
170
171/**
172 * Creates a new logger with specified custom logging function.
173 *
174 * The logging function has a signature of:
175 * function(logger, message)
176 * logger: current logger
177 * message: object:
178 * level: level id
179 * category: category
180 * message: string message
181 * arguments: Array of extra arguments
182 * fullMessage: interpolated message and arguments if INTERPOLATE flag set
183 *
184 * @param logFunction a logging function which takes a log message object
185 * as a parameter.
186 *
187 * @return a logger object.
188 */
189forge.log.makeLogger = function(logFunction) {
190 var logger = {
191 flags: 0,
192 f: logFunction
193 };
194 forge.log.setLevel(logger, 'none');
195 return logger;
196};
197
198/**
199 * Sets the current log level on a logger.
200 *
201 * @param logger the target logger.
202 * @param level the new maximum log level as a string.
203 *
204 * @return true if set, false if not.
205 */
206forge.log.setLevel = function(logger, level) {
207 var rval = false;
208 if(logger && !(logger.flags & forge.log.LEVEL_LOCKED)) {
209 for(var i = 0; i < forge.log.levels.length; ++i) {
210 var aValidLevel = forge.log.levels[i];
211 if(level == aValidLevel) {
212 // set level
213 logger.level = level;
214 rval = true;
215 break;
216 }
217 }
218 }
219
220 return rval;
221};
222
223/**
224 * Locks the log level at its current value.
225 *
226 * @param logger the target logger.
227 * @param lock boolean lock value, default to true.
228 */
229forge.log.lock = function(logger, lock) {
230 if(typeof lock === 'undefined' || lock) {
231 logger.flags |= forge.log.LEVEL_LOCKED;
232 } else {
233 logger.flags &= ~forge.log.LEVEL_LOCKED;
234 }
235};
236
237/**
238 * Adds a logger.
239 *
240 * @param logger the logger object.
241 */
242forge.log.addLogger = function(logger) {
243 sLoggers.push(logger);
244};
245
246// setup the console logger if possible, else create fake console.log
247if(typeof(console) !== 'undefined' && 'log' in console) {
248 var logger;
249 if(console.error && console.warn && console.info && console.debug) {
250 // looks like Firebug-style logging is available
251 // level handlers map
252 var levelHandlers = {
253 error: console.error,
254 warning: console.warn,
255 info: console.info,
256 debug: console.debug,
257 verbose: console.debug
258 };
259 var f = function(logger, message) {
260 forge.log.prepareStandard(message);
261 var handler = levelHandlers[message.level];
262 // prepend standard message and concat args
263 var args = [message.standard];
264 args = args.concat(message['arguments'].slice());
265 // apply to low-level console function
266 handler.apply(console, args);
267 };
268 logger = forge.log.makeLogger(f);
269 } else {
270 // only appear to have basic console.log
271 var f = function(logger, message) {
272 forge.log.prepareStandardFull(message);
273 console.log(message.standardFull);
274 };
275 logger = forge.log.makeLogger(f);
276 }
277 forge.log.setLevel(logger, 'debug');
278 forge.log.addLogger(logger);
279 sConsoleLogger = logger;
280} else {
281 // define fake console.log to avoid potential script errors on
282 // browsers that do not have console logging
283 console = {
284 log: function() {}
285 };
286}
287
288/*
289 * Check for logging control query vars.
290 *
291 * console.level=<level-name>
292 * Set's the console log level by name. Useful to override defaults and
293 * allow more verbose logging before a user config is loaded.
294 *
295 * console.lock=<true|false>
296 * Lock the console log level at whatever level it is set at. This is run
297 * after console.level is processed. Useful to force a level of verbosity
298 * that could otherwise be limited by a user config.
299 */
300if(sConsoleLogger !== null) {
301 var query = forge.util.getQueryVariables();
302 if('console.level' in query) {
303 // set with last value
304 forge.log.setLevel(
305 sConsoleLogger, query['console.level'].slice(-1)[0]);
306 }
307 if('console.lock' in query) {
308 // set with last value
309 var lock = query['console.lock'].slice(-1)[0];
310 if(lock == 'true') {
311 forge.log.lock(sConsoleLogger);
312 }
313 }
314}
315
316// provide public access to console logger
317forge.log.consoleLogger = sConsoleLogger;
Note: See TracBrowser for help on using the repository browser.