@abtnode/logger 1.8.56 → 1.8.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/logger.js +47 -4
- package/lib/util.js +16 -6
- package/package.json +3 -2
package/lib/logger.js
CHANGED
|
@@ -6,8 +6,9 @@ const { createLogger, format, transports } = require('winston');
|
|
|
6
6
|
const debug = require('debug');
|
|
7
7
|
const rfs = require('rotating-file-stream');
|
|
8
8
|
require('winston-daily-rotate-file');
|
|
9
|
+
const { LOG_RETAIN_IN_DAYS } = require('@abtnode/constant');
|
|
9
10
|
const CustomRotateFileTransport = require('./transport');
|
|
10
|
-
const {
|
|
11
|
+
const { getAccessLogFilenameGenerator } = require('./util');
|
|
11
12
|
|
|
12
13
|
const instanceMap = new Map();
|
|
13
14
|
|
|
@@ -100,17 +101,22 @@ const initLogger =
|
|
|
100
101
|
maxSize: '20m',
|
|
101
102
|
};
|
|
102
103
|
|
|
104
|
+
const infoAuditFilename = `.${filename}-info.audit.json`;
|
|
105
|
+
const errorAuditFilename = `.${filename}-error.audit.json`;
|
|
106
|
+
|
|
103
107
|
const infoRotateFileParams = {
|
|
104
108
|
...rotateConfig,
|
|
105
109
|
filename: `${filename}-%DATE%.log`,
|
|
106
110
|
ignoreLevels: ['error'],
|
|
107
111
|
symlinkName: `${filename}.log`,
|
|
112
|
+
auditFile: path.join(logDir, infoAuditFilename),
|
|
108
113
|
};
|
|
109
114
|
|
|
110
115
|
const errorRotateFileParams = {
|
|
111
116
|
...rotateConfig,
|
|
112
117
|
filename: `${filename}-error-%DATE%.log`,
|
|
113
118
|
symlinkName: `${filename}-error.log`,
|
|
119
|
+
auditFile: path.join(logDir, errorAuditFilename),
|
|
114
120
|
level: 'error',
|
|
115
121
|
};
|
|
116
122
|
|
|
@@ -155,22 +161,59 @@ const initLogger =
|
|
|
155
161
|
|
|
156
162
|
const getLogger = (label = '', options = {}) => {
|
|
157
163
|
if (!instanceMap.has(label)) {
|
|
158
|
-
instanceMap.set(label, initLogger(label)({ retain:
|
|
164
|
+
instanceMap.set(label, initLogger(label)({ retain: LOG_RETAIN_IN_DAYS, ...options }));
|
|
159
165
|
}
|
|
160
166
|
|
|
161
167
|
return instanceMap.get(label);
|
|
162
168
|
};
|
|
163
169
|
|
|
164
|
-
const
|
|
165
|
-
|
|
170
|
+
const deleteOldLogfiles = (file, retain) => {
|
|
171
|
+
if (typeof retain !== 'number') {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (file === '/dev/null') {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const extname = path.extname(file);
|
|
180
|
+
const fileBaseName = `${path.basename(file, extname)}`;
|
|
181
|
+
const dirName = path.dirname(file);
|
|
182
|
+
const files = fs.readdirSync(dirName);
|
|
183
|
+
let i;
|
|
184
|
+
let len;
|
|
185
|
+
const rotatedFiles = [];
|
|
186
|
+
for (i = 0, len = files.length; i < len; i++) {
|
|
187
|
+
if (files[i].startsWith(`${fileBaseName}-`)) {
|
|
188
|
+
rotatedFiles.push(files[i]);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
rotatedFiles.sort().reverse();
|
|
193
|
+
|
|
194
|
+
for (i = rotatedFiles.length - 1; i >= retain; i--) {
|
|
195
|
+
fs.unlinkSync(path.resolve(dirName, rotatedFiles[i]));
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const getAccessLogStream = (dir, filename = 'access.log') => {
|
|
200
|
+
const stream = rfs.createStream(getAccessLogFilenameGenerator(filename), {
|
|
166
201
|
interval: '1d',
|
|
167
202
|
path: dir,
|
|
168
203
|
compress: 'gzip',
|
|
169
204
|
});
|
|
170
205
|
|
|
206
|
+
stream.on('rotated', () => {
|
|
207
|
+
deleteOldLogfiles(path.join(dir, filename), LOG_RETAIN_IN_DAYS);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
return stream;
|
|
211
|
+
};
|
|
212
|
+
|
|
171
213
|
module.exports = getLogger;
|
|
172
214
|
module.exports.initLogger = initLogger;
|
|
173
215
|
module.exports.getNoopLogger = getNoopLogger;
|
|
174
216
|
module.exports.customPrintfCallback = customPrintfCallback;
|
|
175
217
|
module.exports.getInstanceSize = () => instanceMap.size;
|
|
176
218
|
module.exports.getAccessLogStream = getAccessLogStream;
|
|
219
|
+
module.exports.deleteOldLogfiles = deleteOldLogfiles;
|
package/lib/util.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
|
|
1
3
|
const getPreDate = (date) => {
|
|
2
4
|
date.setDate(date.getDate() - 1);
|
|
3
5
|
|
|
@@ -8,23 +10,31 @@ const getPreDate = (date) => {
|
|
|
8
10
|
return { year, month, day };
|
|
9
11
|
};
|
|
10
12
|
|
|
11
|
-
const
|
|
13
|
+
const getAccessLogFilenameGenerator = (filename) => (time, index) => {
|
|
14
|
+
let tempFilename = filename || 'access.log';
|
|
15
|
+
|
|
16
|
+
if (path.extname(tempFilename) !== '.log') {
|
|
17
|
+
tempFilename = `${tempFilename}.log`;
|
|
18
|
+
}
|
|
19
|
+
|
|
12
20
|
if (!time) {
|
|
13
|
-
return
|
|
21
|
+
return tempFilename;
|
|
14
22
|
}
|
|
15
23
|
|
|
16
24
|
const { year, month, day } = getPreDate(time);
|
|
17
25
|
|
|
18
|
-
|
|
26
|
+
const nameWithoutExtension = path.basename(tempFilename, '.log');
|
|
27
|
+
|
|
28
|
+
let finalName = `${nameWithoutExtension}-${year}-${month}-${day}`;
|
|
19
29
|
|
|
20
30
|
if (index > 1) {
|
|
21
|
-
|
|
31
|
+
finalName = `${finalName}-${index}`;
|
|
22
32
|
}
|
|
23
33
|
|
|
24
|
-
return `${
|
|
34
|
+
return `${finalName}.log.gz`;
|
|
25
35
|
};
|
|
26
36
|
|
|
27
37
|
module.exports = {
|
|
28
38
|
getPreDate,
|
|
29
|
-
|
|
39
|
+
getAccessLogFilenameGenerator,
|
|
30
40
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abtnode/logger",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.57",
|
|
4
4
|
"description": "ABT Node logger lib",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logger",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"url": "https://github.com/ArcBlock/blocklet-server/issues"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"@abtnode/constant": "1.8.57",
|
|
37
38
|
"debug": "^4.3.4",
|
|
38
39
|
"fast-safe-stringify": "^2.1.1",
|
|
39
40
|
"fs-extra": "^10.1.0",
|
|
@@ -46,5 +47,5 @@
|
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"express": "^4.18.2"
|
|
48
49
|
},
|
|
49
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "2573eb9370c79853cba88cba418a964fb1cc8949"
|
|
50
51
|
}
|