logger.js
1.75 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
"use strict";
const fs = require('fs');
const strftime = require('strftime');
const winston = require('winston');
require('winston-daily-rotate-file');
var loggerTimestamp = function() {
return strftime('%F %T', new Date());
}
var logger;
function init(_filenamePrefix) {
//console.trace('Inisialisasi logger');
const logDirectory = 'logs';
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
let filenamePrefix;
if (_filenamePrefix) {
filenamePrefix = logDirectory + '/' + _filenamePrefix;
}
else {
filenamePrefix = logDirectory + "/log";
}
logger = new winston.Logger({
transports: [
new (winston.transports.Console)({
timestamp: function() {
return strftime('%F %T', new Date());
},
level: 'verbose',
}),
new (winston.transports.DailyRotateFile) ({
name: 'log-file-txt',
filename: filenamePrefix,
timestamp: loggerTimestamp,
formatter: function(options) {
return options.timestamp()
+' ' + options.level.toUpperCase()
+' ' + (undefined !== options.message ? options.message : '')
+ (options.meta && Object.keys(options.meta).length ? '\n\t' + JSON.stringify(options.meta) : '' );
},
level: 'debug',
}),
]
});
logger.verbose(__filename + ': initialized');
return logger;
}
function get() {
if (!logger) {
console.trace('Logger uninitialized, aborting process');
process.exit(1);
} else {
return logger;
}
}
exports.init = init;
exports.get = get;