-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.js
114 lines (91 loc) · 3.04 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
var winston= require ( 'winston' );
require ( 'winston-daily-rotate-file' );
const moment = require('moment-timezone');
var fs = require('fs');
const archiver = require('archiver');
if ( process.type === 'browser') // 'browser' means we are in the main process
var USER_DATA_PATH = require('electron').app.getPath('userData');
else // if not in main process, we must be in the renderer process process.type will then be 'renderer'
var USER_DATA_PATH = require ( '@electron/remote' ).app.getPath('userData');
if ( !fs.existsSync ( USER_DATA_PATH + '/log' ) )
fs.mkdirSync ( USER_DATA_PATH + '/log' );
const LOG_FILE = USER_DATA_PATH + '/log/%DATE%logfile.txt';//error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5
global.ERROR = 0;
global.WARNING = 1;
global.INFO = 2;
function getFormattedTimestamp ( withTimezone ) {
var ts = moment().valueOf()
var utcOffset = moment().utcOffset()
if ( withTimezone ) {
let tmp_str = moment(ts).utcOffset(utcOffset).format('YY-MM-DD HH:mm:ss.SSS UTCZ')
if ( tmp_str.substr ( -3 ) === ':00' )
tmp_str = tmp_str.substr ( 0, tmp_str.length -3 )
return tmp_str
} else
return moment(ts).utcOffset(utcOffset).format('YY-MM-DD HH:mm:ss.SSS')
}
const wlog = winston.createLogger({
format: winston.format.combine (
winston.format.timestamp(),
winston.format.printf ( info => `${getFormattedTimestamp(true)} [${info.level.toUpperCase()}] ${info.message}`)
),
transports: [
new winston.transports.DailyRotateFile ({
filename : LOG_FILE,
datePattern : 'YYYY-MM-DD_',
maxFiles : '30d',
maxSize : '20m',
zippedArchive: true
})
]
});
global.log = {}
global.log.info = str => {
console.log ( getFormattedTimestamp(false), " [INFO] ", str );
wlog.info ( str );
}
global.log.warn = str => {
console.warn ( getFormattedTimestamp(false), " [WARNING] ", str );
wlog.warn ( str );
}
global.log.error = str => {
console.error ( getFormattedTimestamp(false), " [ERROR] ", str );
wlog.error ( str );
}
global.log.hex = data => {
let hexStr =''
if ( typeof data === 'string' || (Array.isArray(data) && typeof data[0] === 'string')) {
let charCodeArr = []
for ( const char of data ) {
charCodeArr.push(char.charCodeAt(0))
}
data = charCodeArr
}
for ( const byte of data ) {
hexStr += byte.toString(16).toUpperCase().padStart(2,'0')
hexStr += ' '
}
console.log ( hexStr )
wlog.info ( hexStr )
}
global.zipLogs = (targetPath) => {
const archive = archiver('zip', { zlib: { level: 9 }});
const stream = fs.createWriteStream(targetPath);
return new Promise((resolve, reject) => {
archive
.directory(USER_DATA_PATH + '/log', false)
.on('error', err => {
console.error ( getFormattedTimestamp(false), " [ERROR] ", err );
wlog.error ( err );
reject(err);
})
.pipe(stream);
stream.on('close', () => resolve());
archive.finalize();
});
}
if ( process.type === 'browser' ) {
log.info("Main process is writing log data to: " + USER_DATA_PATH + '/log')
} else {
log.info("Renderer process is writing log data to: " + USER_DATA_PATH + '/log')
}