/
logger.js
80 lines (71 loc) · 2.47 KB
/
logger.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
var fs = require('fs');
var util = require('util');
var Options = require('./options');
var defaults = {
autoInit: true
};
var Logger = module.exports = function(options, callInit){
var self = this;
options = Options.ensure(options||{}, defaults);
if(options.autoInit){
self.init(options);
}
};
Logger.prototype.debug = function(message, object){
this.logItem('debug', message, object);
};
Logger.prototype.debug.description = 'Writes a debug message to the log.';
Logger.prototype.log = function(message, object){
this.logItem('log', message, object);
};
Logger.prototype.log.description = 'Writes a log message to the log.';
Logger.prototype.error = function(message, object){
this.logItem('ERROR', message, object);
};
Logger.prototype.error.description = 'Writes a error message to the log.';
Logger.prototype.init = function(options){
var self = this;
options=options||{};
self.fileStream = options.toFile!==false?fs.createWriteStream(fs.existsSync(process.argv[1])?process.argv[1]+'.log':'log.txt')||false:false;
self.toScreen = !!options.toScreen;
self.log('started', process);
self.divider();
};
Logger.prototype.init.description = 'Initializes the logging object, this is the other routine to replace if you want to write your own logger.';
Logger.prototype.logItem = function(level, message, object){
this.write((level||'').toLowerCase()==='error', level+':', message+(object?'\r\n'+util.inspect(object, false, null, false, false):''));
};
Logger.prototype.logItem.description = 'Writes a specific type of message to the log.';
Logger.prototype.write = function(force){
var self = this;
var startAt = 1, args = Array.prototype.slice.apply(arguments), i, l = args.length, msg = '';
if(typeof(force)!=='boolean'){
startAt--;
force = false;
}
for(i=startAt; i<l; i++){
if(typeof(args[i])!=='string'){
msg += util.inspect(args[i], false, null, false, false);
}else{
msg += args[i];
}
msg+='\t';
}
if(force){
console.log(msg);
}
if(self.fileStream){
self.fileStream.write((new Date())+'\r\n'+msg+'\r\n');
}
if((!force)&&(self.toScreen)){
console.log(msg);
}
};
Logger.prototype.write.description = 'Performs the actual write, if your going to derrive from Logger this is the routine to replace.';
Logger.prototype.divider = function(){
var self = this;
if(self.toFile){
self.toFile.write((new Array(40).join('-='))+'\r\n');
}
};
Logger.prototype.divider.description = 'Writes a divider to the log.';