forked from quirkey/node-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
148 lines (130 loc) · 4.46 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// name: logger.js
// version: 0.0.1
// http://github.com/quirkey/node-logger
/*
Copyright (c) 2010 Aaron Quint
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
var path = require('path'),
util = require('util'),
fs = require('fs');
var makeArray = function (nonarray) {
return Array.prototype.slice.call(nonarray);
};
// Create a new instance of Logger, logging to the file at `log_file_path`
// if `log_file_path` is null, log to STDOUT.
var Logger = function (log_file_path) {
this.default_log_level_index = 3;
// default write is STDOUT
this.log_level_index = 0;
this.write = function (text) {
text = text.trim();
switch (this.log_level_index) {
case 0:
console.log(text);
break;
case 1:
console.error(text);
break;
case 2:
console.warn(text);
break;
case 3:
console.info(text);
break;
case 4:
console.debug(text);
break
}
};
// if a path is given, try to write to it
if (log_file_path) {
// Write to a file
log_file_path = path.normalize(log_file_path);
if (!fs.existsSync(log_file_path)) {
if (!fs.existsSync(path.dirname(log_file_path))) {
fs.mkdirSync(path.dirname(log_file_path))
}
}
this.stream =
fs.createWriteStream(log_file_path, {flags: 'a', encoding: 'utf8', mode: 0o666});
this.write = function (text) {
this.stream.write(text);
};
}
};
Logger.levels = ['trace', 'error', 'warn', 'info', 'debug'];
// The default log formatting function. The default format looks something like:
//
// error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message
//
Logger.prototype.format = function (level, date, message) {
return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getMinutes()} [${level.toUpperCase()}] ${message}`;
};
// Set the maximum log level. The default level is "info".
Logger.prototype.setLevel = function (new_level) {
var index = Logger.levels.indexOf(new_level.toLowerCase());
return (index !== -1) ? this.default_log_level_index = index : false;
};
// The base logging method. If the first argument is one of the levels, it logs
// to that level, otherwise, logs to the default level. Can take `n` arguments
// and joins them by ' '. If the argument is not a string, it runs `sys.inspect()`
// to print a string representation of the object.
Logger.prototype.log = function () {
var args = makeArray(arguments),
log_index = Logger.levels.indexOf(args[0]),
message = '';
// if you're just default logging
if (log_index === -1) {
log_index = this.default_log_level_index;
} else {
// the first arguement actually was the log level
args.shift();
}
if (log_index < Logger.levels.length) {
this.log_level_index = log_index;
// join the arguments into a loggable string
args.forEach(function (arg) {
if (typeof arg === 'string') {
message += ' ' + arg;
} else {
message += ' ' + util.inspect(arg, false, null);
}
});
message = this.format(Logger.levels[log_index], new Date(), message);
this.write(message + "\n");
return message;
}
return false;
};
Logger.levels.forEach(function (level) {
Logger.prototype[level] = function () {
var args = makeArray(arguments);
args.unshift(level);
return this.log.apply(this, args);
};
});
/**
* Api
*/
exports.Logger = Logger;
exports.createLogger = function (log_file_path) {
return new Logger(log_file_path);
};