-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (78 loc) · 2.42 KB
/
index.js
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
/* ************************************************** *
* ******************** Constructor
* ************************************************** */
var Log = function (options) {
var bunyan = require('bunyan'),
MongoStream = require('./libs/mongoStream'),
PrettySteam = require('./libs/prettyStream');
if(!options) { options = {} }
this.debug = getDefaultValue(options.debug, true);
this.trace = getDefaultValue(options.trace, false);
this.error = getDefaultValue(options.error, true);
this.name = getDefaultValue(options.name, 'Log');
this.databaseLog = getDefaultValue(options.databaseLog, false);
var loggingStreams = [
{
type: 'raw',
level: 'trace',
stream: new PrettySteam()
}
];
if(this.databaseLog) {
if(!options.mongoose) {
throw new Error('An instance of mongoose is required to enabled database logging')
}
loggingStreams.push( {
type: 'raw',
level: 'info',
stream: new MongoStream({mongoose: options.mongoose})
})
}
this.log = bunyan.createLogger({
name: this.name,
serializers: bunyan.stdSerializers,
streams: loggingStreams});
this.requestLog = require('express-bunyan-logger')({
name: this.name + ' Requests',
serializers: bunyan.stdSerializers,
streams: loggingStreams});
};
/* ************************************************** *
* ******************** Methods
* ************************************************** */
Log.prototype.requestLogger = function() {
return this.requestLog;
};
Log.prototype.i = function() {
this.log.info.apply(this.log, arguments);
};
Log.prototype.d = function() {
if(this.debug) {
this.log.debug.apply(this.log, arguments);
}
};
Log.prototype.t = function() {
if(this.trace) {
this.log.trace.apply(this.log, arguments);
}
};
Log.prototype.e = function() {
if(this.error) {
this.log.error.apply(this.log, arguments);
}
};
Log.prototype.w = function() {
if(this.error) {
this.log.warn.apply(this.log, arguments);
}
};
/* ************************************************** *
* ******************** Helper Functions
* ************************************************** */
function getDefaultValue(obj, objectDefault) {
return (obj === undefined || obj === null) ? objectDefault : obj
}
/* ************************************************** *
* ******************** Exports
* ************************************************** */
module.exports = Log;