-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (86 loc) · 3.32 KB
/
index.js
File metadata and controls
99 lines (86 loc) · 3.32 KB
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
'use strict';
var addInterfaceMethods = require( './lib/add-interfaces' );
var DEFAULT_CONFIG = require( './lib/default-config' );
module.exports = function BalsaLogger ( config ) {
var self = {};
// Process configuration, set internal state
self.config = config = config || {};
config.enabled = typeof config.enabled !== 'undefined' ? config.enabled : DEFAULT_CONFIG.enabled;
config.levels = config.levels || DEFAULT_CONFIG.levels.slice( 0 );
config.aliases = config.aliases || JSON.parse( JSON.stringify ( DEFAULT_CONFIG.aliases ) );
config.prefix = typeof config.prefix !== 'undefined' ? config.prefix : DEFAULT_CONFIG.prefix;
config.minLevel = typeof config.minLevel !== 'undefined' ? config.minLevel : DEFAULT_CONFIG.minLevel;
config.messageFormat = config.messageFormat || DEFAULT_CONFIG.messageFormat;
config.relays = config.relays || DEFAULT_CONFIG.relays.slice( 0 );
// Add logging interfaces
addInterfaceMethods( self );
// Add aliases
for( var alias in self.config.aliases ) {
self[ alias ] = self[ self.config.aliases[ alias ] ];
}
/** Enable logging */
self.enable = function () {
self.config.enabled = true;
};
/** Disable all logging */
self.disable = function () {
self.config.enabled = false;
};
/**
* Adds a new relay
* @param {object} relay - A relay object extended from BaseRelay.
* @returns {number} The relay ID for use during removal
*/
self.add = function ( relay ) {
// A relay's "ID" is just its index in the array
return self.config.relays.push( relay ) - 1;
};
/**
* Removes a relay
* @param {number} relayID - The ID of the relay to remove
*/
self.remove = function ( relayID ) {
self.config.relays.splice( relayID, 1 );
};
/**
* Set the prefix that will be prepended to every log message.
* @param {string} prefix - The prefix string, e.g., 'myApp'
*/
self.prefix = function ( prefix ) {
self.config.prefix = prefix;
};
/**
* Sets the default minimum logging level, i.e., don't log messages below
* the specified level. If `null`, all levels will be logged. Possible
* values are:
*
* - null - Log ALL THE THINGS
* - 'debug' - Don't log below debug (lowest level, so log everything)
* - 'info' - Don't log below info
* - 'warn' - Don't log below warn
* - 'error' - Don't log below error
*
* Relays may override this value in their own configuration.
*
* @param {string|null} level - The minimum default level
*/
self.minLevel = function ( level ) {
self.config.minLevel = level;
};
/**
* Sets the default message format. Available variables are:
*
* - `$TIMESTAMP` - Timestamp of the log, in [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601)
* - `$LEVEL` - The logging level
* - `$PREFIX` - The message prefix
* - `$MESSAGE` - The message body
*
* Relays may override this value in their own configuration.
*
* @param {string} format - The message format string
*/
self.messageFormat = function ( format ) {
self.config.messageFormat = format;
};
return self;
};