forked from satishjoshi95/DDKOIN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
executable file
·88 lines (80 loc) · 2.37 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
let winston = require('winston');
require('winston-daily-rotate-file');
/**
* @desc custom levels for winston logger
*/
const levels = {
archive: 0,
error: 1,
warn: 2,
info: 3,
http: 4,
verbose: 5,
debug: 6,
silly: 7,
trace: 8,
};
/**
* @desc Creating file transport to write logs into a file. This file daily rotates
*/
let transport = new (winston.transports.DailyRotateFile)({
filename: 'logs/./log',
datePattern: 'yyyy-MM-dd.',
prepend: true,
json: false,
level: process.env.NODE_ENV === 'development' ? 'debug' : 'info',
timestamp: function () {
let today = new Date();
return today.toISOString();
}
});
/**
* @desc creating file transport to write archived logs into a file.
* @desc Only "archive" logs will be written in this file.
*/
let traceTransport = new (winston.transports.File)({
filename: './logs/archive.log',
level: 'archive',
levelOnly: true,
prepend: true,
json: false,
timestamp: function () {
let today = new Date();
return today.toISOString();
}
});
const consoleTransport = new winston.transports.Console();
/**
* @desc logger Constructor
* @param {String} sessionId - session is to be written in each log
* @param {String} address - address od the current logged-in user
* @returns {Transport}
*/
class Logger {
constructor(sessionId, address) {
this.transport = transport;
this.consoleTransport = consoleTransport;
this.transport.formatter = function (options) {
if (sessionId && address) {
return options.timestamp() + ' - ' + sessionId + ' - ' + address + ' - [' + options.level + '] : ' + options.message;
} else if (sessionId) {
return options.timestamp() + ' - ' + sessionId + ' - [' + options.level + '] : ' + options.message;
} else if (address) {
return options.timestamp() + ' - ' + address + ' - [' + options.level + '] : ' + options.message;
} else {
return options.timestamp() + ' - [' + options.level + '] : ' + options.message;
}
};
this.traceTransport = traceTransport;
this.traceTransport.formatter = function (options) {
return options.timestamp() + ' - [' + options.level + '] : ' + options.message;
};
this.logger = new (winston.Logger)({
levels: levels,
transports: [this.traceTransport, this.transport, this.consoleTransport]
});
}
}
//exports module
module.exports = Logger;
/*************************************** END OF FILE *************************************/