-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.js
128 lines (109 loc) · 2.45 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var fs = require('fs');
var Util = require('./util');
var Logger = function (date, source) {
var _path = {
year: '',
month: '',
day: '',
log: ''
};
// log
var _log = {
date: date,
total: 0,
n_failure: 0,
items: {},
n_success: 0
};
var LOGS = process.cwd() + Util.PATH_SEP + 'downloads';
// console.log(LOGS);
if (!Util.isExist(LOGS)) {
fs.mkdirSync(LOGS);
}
// source
LOGS = LOGS + Util.PATH_SEP + source;
if (!Util.isExist(LOGS)) {
fs.mkdirSync(LOGS);
}
if (!Util.isDate(date)) {
throw Error('date format error!');
}
var datepart = date.split(/[-._/]/);
_path.year = LOGS + Util.PATH_SEP + datepart[0];
_path.month = _path.year + Util.PATH_SEP + Util.format2(datepart[1]);
_path.day = _path.month + Util.PATH_SEP + Util.format2(datepart[2]);
_path.log = _path.day + Util.PATH_SEP + Util.format2(datepart[2]) + '.json';
// create year folder
if (!Util.isExist(_path.year)) {
fs.mkdirSync(_path.year);
}
// create month folder
if (!Util.isExist(_path.month)) {
fs.mkdirSync(_path.month);
}
// create day folder
if (!Util.isExist(_path.day)) {
fs.mkdirSync(_path.day);
}
// load log
load();
return {
save: save,
load: load,
get: get,
getItem: getItem,
put: put,
inc: inc,
path: _path,
isExist: isExist
};
function put(name, value) {
if (!name || !value) return;
if (!!_log['items'][name]) { // exist
if (!!value.filename) {
inc('n_failure', -1);
inc('n_success');
}
} else { // not exist
if (!!value.filename) {
inc('n_success');
} else {
inc('n_failure');
}
inc('total');
}
_log['items'][name] = value;
save();
}
function save() {
fs.writeFile(_path.log, JSON.stringify(_log, null, 2), (err) => {
if (err) {
console.log('save ' + _log.date + ' log failed.');
save();
}
});
}
function load() {
if (Util.isExist(_path.log)) {
_log = JSON.parse(fs.readFileSync(_path.log));
}
}
function inc(name, val) {
if (typeof _log[name] === 'number') {
_log[name] += val || 1;
return _log[name];
} else {
return null;
}
}
function getItem(name) {
return _log['items'][name];
}
function get(name) {
return _log[name];
}
function isExist(name) {
return !!_log.items[name] && !!_log.items[name].filename;
}
};
module.exports = Logger;