-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
40 lines (35 loc) · 1.03 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
import fs from 'fs';
let saveLog = (info) => {
let ret = getLogFile();
let filepath = ret[0];
let log = ret[1];
log.push(createLogEntry(info));
fs.writeFileSync(filepath, JSON.stringify(log));
}
let createLogEntry = (info) => {
return {
ip: info.ip,
timestamp: Date.now(),
handler: info.handler,
contestid: info.contestid,
contestsolve: info.contestsolve,
upsolve: info.upsolve
};
}
let getLogFile = (logdir='./LOGS/') => {
let filePath = logdir + getLogFileName();
if (fs.existsSync(filePath)){
return [filePath, JSON.parse(fs.readFileSync(filePath))];
}
else{
return [filePath, JSON.parse('[]')];
}
}
let getLogFileName = () => {
let d = new Date();
let returnDate = String(d.getFullYear());
returnDate += (d.getMonth() < 10 ? '0'+String(d.getMonth()) : String(d.getMonth()));
returnDate += (d.getDate() < 10 ? '0' + String(d.getDate()) : String(d.getDate()));
return returnDate + '.json';
}
export default saveLog;