forked from LiskArchive/lisk-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
55 lines (44 loc) · 1.19 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
var strftime = require("strftime").utc();
var fs = require("fs");
require("colors");
module.exports = function (config) {
config = config || {};
var exports = {};
config.levels = config.levels || {
"trace": 0,
"debug": 1,
"log": 2,
"info": 3,
"warn": 4,
"error": 5,
"fatal": 6
}
config.filename = config.filename || __dirname + "/logs.log";
config.errorLevel = config.errorLevel || "log";
var log_file = fs.createWriteStream(config.filename, { flags: "a" });
exports.setLevel = function (errorLevel) {
config.errorLevel = errorLevel;
}
Object.keys(config.levels).forEach(function (name) {
function log(caption, data) {
var log = {
"level": name,
"message": caption,
"timestamp": strftime("%F %T", new Date())
}
data && (log["data"] = data);
if (config.levels[config.errorLevel] <= config.levels[log.level]) {
log_file.write(JSON.stringify(log) + "\n");
}
if (config.echo && config.levels[config.echo] <= config.levels[log.level]) {
try {
console.log(log.level.bgYellow.black, log.timestamp.grey, log.message, log.data ? log.data : "");
}catch (e){
console.log(e)
}
}
}
exports[name] = log;
})
return exports;
}