@eggjs/logrotator 5.0.0-beta.20 → 5.0.0-beta.22
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/dist/agent.d.ts +2 -2
- package/dist/agent.js +3 -7
- package/dist/app/extend/agent.d.ts +3 -6
- package/dist/app/extend/agent.js +6 -7
- package/dist/app/extend/application.d.ts +5 -3
- package/dist/app/extend/application.js +5 -4
- package/dist/app/schedule/clean_log.d.ts +7 -10
- package/dist/app/schedule/clean_log.js +67 -57
- package/dist/app/schedule/rotate_by_file.d.ts +8 -11
- package/dist/app/schedule/rotate_by_file.js +14 -20
- package/dist/app/schedule/rotate_by_hour.d.ts +8 -11
- package/dist/app/schedule/rotate_by_hour.js +14 -19
- package/dist/app/schedule/rotate_by_size.d.ts +8 -11
- package/dist/app/schedule/rotate_by_size.js +14 -19
- package/dist/app.d.ts +2 -2
- package/dist/app.js +3 -7
- package/dist/boot.d.ts +6 -2
- package/dist/boot.js +14 -3
- package/dist/config/config.default.d.ts +60 -2
- package/dist/config/config.default.js +14 -3
- package/dist/index.d.ts +4 -5
- package/dist/index.js +5 -6
- package/dist/lib/day_rotator.d.ts +7 -11
- package/dist/lib/day_rotator.js +80 -5
- package/dist/lib/hour_rotator.d.ts +5 -9
- package/dist/lib/hour_rotator.js +39 -4
- package/dist/lib/rotator.d.ts +16 -2
- package/dist/lib/rotator.js +73 -3
- package/dist/lib/size_rotator.d.ts +4 -8
- package/dist/lib/size_rotator.js +67 -4
- package/dist/lib/utils.d.ts +5 -7
- package/dist/lib/utils.js +16 -3
- package/dist/types.d.ts +17 -3
- package/dist/types.js +2 -3
- package/package.json +6 -6
- package/dist/application-DLlqYOTH.d.ts +0 -8
- package/dist/application-DUxqk1cL.js +0 -7
- package/dist/boot-Fk-0g48D.js +0 -16
- package/dist/boot-GzaTgWud.d.ts +0 -10
- package/dist/config.default-CnLV2bGK.d.ts +0 -62
- package/dist/config.default-CvMzfv2b.js +0 -15
- package/dist/day_rotator-DHd2Parx.js +0 -64
- package/dist/hour_rotator-JmysNFky.js +0 -38
- package/dist/rotator-BjJMXyaY.d.ts +0 -20
- package/dist/rotator-CRROE_AN.js +0 -56
- package/dist/size_rotator-B94wnaTP.js +0 -61
- package/dist/types-CQtQr8gw.js +0 -1
- package/dist/types-RmfrK2ke.d.ts +0 -19
- package/dist/utils-Br9jwJMb.js +0 -15
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { LogRotator } from "./rotator-CRROE_AN.js";
|
|
2
|
-
import { debuglog } from "node:util";
|
|
3
|
-
import { exists } from "utility";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import moment from "moment";
|
|
6
|
-
|
|
7
|
-
//#region src/lib/hour_rotator.ts
|
|
8
|
-
const debug = debuglog("egg/logrotator/lib/hour_rotator");
|
|
9
|
-
var HourRotator = class extends LogRotator {
|
|
10
|
-
async getRotateFiles() {
|
|
11
|
-
const files = /* @__PURE__ */ new Map();
|
|
12
|
-
const logDir = this.app.config.logger.dir;
|
|
13
|
-
const filesRotateByHour = this.app.config.logrotator.filesRotateByHour ?? [];
|
|
14
|
-
for (let logPath of filesRotateByHour) {
|
|
15
|
-
if (!path.isAbsolute(logPath)) logPath = path.join(logDir, logPath);
|
|
16
|
-
if (!await exists(logPath)) continue;
|
|
17
|
-
this._setFile(logPath, files);
|
|
18
|
-
}
|
|
19
|
-
return files;
|
|
20
|
-
}
|
|
21
|
-
get hourDelimiter() {
|
|
22
|
-
return this.app.config.logrotator.hourDelimiter;
|
|
23
|
-
}
|
|
24
|
-
_setFile(srcPath, files) {
|
|
25
|
-
if (!files.has(srcPath)) {
|
|
26
|
-
const ext = this.app.config.logrotator.gzip === true ? ".gz" : "";
|
|
27
|
-
const targetPath = srcPath + moment().subtract(1, "hours").format(`.YYYY-MM-DD${this.hourDelimiter}HH`) + ext;
|
|
28
|
-
debug("set file %s => %s", srcPath, targetPath);
|
|
29
|
-
files.set(srcPath, {
|
|
30
|
-
srcPath,
|
|
31
|
-
targetPath
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
//#endregion
|
|
38
|
-
export { HourRotator };
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Application } from "egg";
|
|
2
|
-
|
|
3
|
-
//#region src/lib/rotator.d.ts
|
|
4
|
-
interface RotatorOptions {
|
|
5
|
-
app: Application;
|
|
6
|
-
}
|
|
7
|
-
interface RotateFile {
|
|
8
|
-
srcPath: string;
|
|
9
|
-
targetPath: string;
|
|
10
|
-
}
|
|
11
|
-
declare abstract class LogRotator {
|
|
12
|
-
protected readonly options: RotatorOptions;
|
|
13
|
-
protected readonly app: Application;
|
|
14
|
-
protected readonly logger: Application['coreLogger'];
|
|
15
|
-
constructor(options: RotatorOptions);
|
|
16
|
-
abstract getRotateFiles(): Promise<Map<string, RotateFile>>;
|
|
17
|
-
rotate(): Promise<void>;
|
|
18
|
-
}
|
|
19
|
-
//#endregion
|
|
20
|
-
export { LogRotator, RotateFile, RotatorOptions };
|
package/dist/rotator-CRROE_AN.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert";
|
|
2
|
-
import { createReadStream, createWriteStream } from "node:fs";
|
|
3
|
-
import fs from "node:fs/promises";
|
|
4
|
-
import { pipeline } from "node:stream/promises";
|
|
5
|
-
import { createGzip } from "node:zlib";
|
|
6
|
-
import { debuglog } from "node:util";
|
|
7
|
-
import { exists } from "utility";
|
|
8
|
-
|
|
9
|
-
//#region src/lib/rotator.ts
|
|
10
|
-
const debug = debuglog("egg/logrotator/lib/rotator");
|
|
11
|
-
var LogRotator = class {
|
|
12
|
-
options;
|
|
13
|
-
app;
|
|
14
|
-
logger;
|
|
15
|
-
constructor(options) {
|
|
16
|
-
this.options = options;
|
|
17
|
-
assert(this.options.app, "options.app is required");
|
|
18
|
-
this.app = this.options.app;
|
|
19
|
-
this.logger = this.app.coreLogger;
|
|
20
|
-
}
|
|
21
|
-
async rotate() {
|
|
22
|
-
const files = await this.getRotateFiles();
|
|
23
|
-
assert(files instanceof Map, "getRotateFiles should return a Map");
|
|
24
|
-
const rotatedFiles = [];
|
|
25
|
-
for (const file of files.values()) try {
|
|
26
|
-
debug("rename from %s to %s", file.srcPath, file.targetPath);
|
|
27
|
-
await renameOrDelete(file.srcPath, file.targetPath, this.app.config.logrotator.gzip);
|
|
28
|
-
rotatedFiles.push(`${file.srcPath} -> ${file.targetPath}`);
|
|
29
|
-
} catch (e) {
|
|
30
|
-
const err = e;
|
|
31
|
-
err.message = `[@eggjs/logrotator] rename ${file.srcPath}, found exception: ${err.message}`;
|
|
32
|
-
this.logger.error(err);
|
|
33
|
-
}
|
|
34
|
-
if (rotatedFiles.length > 0) {
|
|
35
|
-
debug("broadcast log-reload, rotated files: %j", rotatedFiles);
|
|
36
|
-
this.logger.info("[@eggjs/logrotator] broadcast log-reload");
|
|
37
|
-
this.app.messenger.sendToApp("log-reload");
|
|
38
|
-
this.app.messenger.sendToAgent("log-reload");
|
|
39
|
-
}
|
|
40
|
-
this.logger.info("[@eggjs/logrotator] rotate files success by %s, files %j", this.constructor.name, rotatedFiles);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
async function renameOrDelete(srcPath, targetPath, gzip) {
|
|
44
|
-
if (srcPath === targetPath) return;
|
|
45
|
-
if (!await exists(srcPath)) return;
|
|
46
|
-
if (await exists(targetPath)) throw /* @__PURE__ */ new Error(`targetFile ${targetPath} exists!!!`);
|
|
47
|
-
if (gzip === true) {
|
|
48
|
-
const tmpPath = `${targetPath}.tmp`;
|
|
49
|
-
await fs.rename(srcPath, tmpPath);
|
|
50
|
-
await pipeline(createReadStream(tmpPath), createGzip(), createWriteStream(targetPath));
|
|
51
|
-
await fs.unlink(tmpPath);
|
|
52
|
-
} else await fs.rename(srcPath, targetPath);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
//#endregion
|
|
56
|
-
export { LogRotator };
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { LogRotator } from "./rotator-CRROE_AN.js";
|
|
2
|
-
import fs from "node:fs/promises";
|
|
3
|
-
import { debuglog } from "node:util";
|
|
4
|
-
import { exists } from "utility";
|
|
5
|
-
import path from "node:path";
|
|
6
|
-
|
|
7
|
-
//#region src/lib/size_rotator.ts
|
|
8
|
-
const debug = debuglog("egg/logrotator/lib/size_rotator");
|
|
9
|
-
var SizeRotator = class extends LogRotator {
|
|
10
|
-
async getRotateFiles() {
|
|
11
|
-
const files = /* @__PURE__ */ new Map();
|
|
12
|
-
const logDir = this.app.config.logger.dir;
|
|
13
|
-
const filesRotateBySize = this.app.config.logrotator.filesRotateBySize ?? [];
|
|
14
|
-
const maxFileSize = this.app.config.logrotator.maxFileSize;
|
|
15
|
-
const maxFiles = this.app.config.logrotator.maxFiles;
|
|
16
|
-
for (let logPath of filesRotateBySize) {
|
|
17
|
-
if (!path.isAbsolute(logPath)) logPath = path.join(logDir, logPath);
|
|
18
|
-
const stat = await exists(logPath);
|
|
19
|
-
if (!stat) continue;
|
|
20
|
-
const size = stat.size;
|
|
21
|
-
try {
|
|
22
|
-
if (size >= maxFileSize) {
|
|
23
|
-
this.logger.info(`[@eggjs/logrotator] file ${logPath} reach the maximum file size, current size: ${size}, max size: ${maxFileSize}`);
|
|
24
|
-
const maxFileName = `${logPath}.${maxFiles}`;
|
|
25
|
-
if (await exists(maxFileName)) {
|
|
26
|
-
await fs.unlink(maxFileName);
|
|
27
|
-
this.logger.info(`[@eggjs/logrotator] delete max log file ${maxFileName}`);
|
|
28
|
-
}
|
|
29
|
-
this._setFile(logPath, files);
|
|
30
|
-
}
|
|
31
|
-
} catch (e) {
|
|
32
|
-
const err = e;
|
|
33
|
-
err.message = `[@eggjs/logrotator] ${err.message}`;
|
|
34
|
-
this.logger.error(err);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return files;
|
|
38
|
-
}
|
|
39
|
-
_setFile(logPath, files) {
|
|
40
|
-
const maxFiles = this.app.config.logrotator.maxFiles;
|
|
41
|
-
if (files.has(logPath)) return;
|
|
42
|
-
const ext = this.app.config.logrotator.gzip === true ? ".gz" : "";
|
|
43
|
-
for (let i = maxFiles - 1; i >= 1; i--) {
|
|
44
|
-
const srcPath = `${logPath}.${i}`;
|
|
45
|
-
const targetPath = `${logPath}.${i + 1}${ext}`;
|
|
46
|
-
debug("set file %s => %s", srcPath, targetPath);
|
|
47
|
-
files.set(srcPath, {
|
|
48
|
-
srcPath,
|
|
49
|
-
targetPath
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
debug("set file %s => %s", logPath, `${logPath}.1`);
|
|
53
|
-
files.set(logPath, {
|
|
54
|
-
srcPath: logPath,
|
|
55
|
-
targetPath: `${logPath}.1${ext}`
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
//#endregion
|
|
61
|
-
export { SizeRotator };
|
package/dist/types-CQtQr8gw.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/types-RmfrK2ke.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { LogRotator } from "./rotator-BjJMXyaY.js";
|
|
2
|
-
import { LogrotatorConfig } from "./config.default-CnLV2bGK.js";
|
|
3
|
-
|
|
4
|
-
//#region src/types.d.ts
|
|
5
|
-
declare module 'egg' {
|
|
6
|
-
interface EggAppConfig {
|
|
7
|
-
/**
|
|
8
|
-
* logrotator options
|
|
9
|
-
* @member Config#logrotator
|
|
10
|
-
*/
|
|
11
|
-
logrotator: LogrotatorConfig;
|
|
12
|
-
}
|
|
13
|
-
interface Agent {
|
|
14
|
-
LogRotator: typeof LogRotator;
|
|
15
|
-
}
|
|
16
|
-
interface Application {
|
|
17
|
-
LogRotator: typeof LogRotator;
|
|
18
|
-
}
|
|
19
|
-
}
|
package/dist/utils-Br9jwJMb.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
//#region src/lib/utils.ts
|
|
2
|
-
/**
|
|
3
|
-
* Walk all logger files from loggers
|
|
4
|
-
*/
|
|
5
|
-
function walkLoggerFile(loggers) {
|
|
6
|
-
const files = [];
|
|
7
|
-
for (const registeredLogger of Object.values(loggers)) for (const transport of registeredLogger.values()) {
|
|
8
|
-
const file = transport.options.file;
|
|
9
|
-
if (file) files.push(file);
|
|
10
|
-
}
|
|
11
|
-
return files;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
//#endregion
|
|
15
|
-
export { walkLoggerFile };
|