@jayfong/x-server 2.2.10 → 2.2.11
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/_cjs/services/log.js +54 -14
- package/lib/_cjs/x.js +12 -0
- package/lib/services/log.d.ts +15 -5
- package/lib/services/log.js +47 -14
- package/lib/x.d.ts +1 -0
- package/lib/x.js +5 -0
- package/package.json +1 -1
package/lib/_cjs/services/log.js
CHANGED
|
@@ -1,36 +1,76 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
3
5
|
exports.__esModule = true;
|
|
4
6
|
exports.LogService = void 0;
|
|
5
7
|
|
|
8
|
+
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
9
|
+
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
|
|
6
12
|
var _date = require("vtils/date");
|
|
7
13
|
|
|
14
|
+
var _vtils = require("vtils");
|
|
15
|
+
|
|
16
|
+
var _x = require("../x");
|
|
17
|
+
|
|
8
18
|
class LogService {
|
|
9
19
|
constructor() {
|
|
10
20
|
this.serviceName = 'log';
|
|
11
|
-
|
|
21
|
+
this.separator = '\t\t\t';
|
|
22
|
+
this.getLogWriter = (0, _vtils.memoize)(logFile => {
|
|
23
|
+
_fsExtra.default.ensureFileSync(logFile);
|
|
12
24
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Object.keys(data).forEach(key => {
|
|
16
|
-
message = message.replaceAll(`{${key}}`, data[key]);
|
|
25
|
+
return _fsExtra.default.createWriteStream(logFile, {
|
|
26
|
+
flags: 'a'
|
|
17
27
|
});
|
|
18
|
-
}
|
|
28
|
+
}, logFile => logFile);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get logFile() {
|
|
32
|
+
const now = new Date();
|
|
33
|
+
const year = now.getFullYear();
|
|
34
|
+
const month = now.getMonth() + 1;
|
|
35
|
+
return _path.default.join(_x.x.dataDir, `log/${year}/${month}.log`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
parseLogLineText(text) {
|
|
39
|
+
const [time, level, title, desc] = text.split(this.separator);
|
|
40
|
+
return {
|
|
41
|
+
time: time,
|
|
42
|
+
level: level,
|
|
43
|
+
title: title,
|
|
44
|
+
desc: JSON.parse(desc)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
19
47
|
|
|
20
|
-
|
|
21
|
-
|
|
48
|
+
log(payload) {
|
|
49
|
+
this.getLogWriter(this.logFile).write(`${(0, _date.formatDate)(new Date(), 'yyyy-mm-dd hh:ii:ss')}${this.separator}${payload.level}${this.separator}${payload.title}${this.separator}${payload.desc ? JSON.stringify(payload.desc) : '{}'}\n`);
|
|
22
50
|
}
|
|
23
51
|
|
|
24
|
-
|
|
25
|
-
this.log(
|
|
52
|
+
info(title, desc) {
|
|
53
|
+
this.log({
|
|
54
|
+
level: 'info',
|
|
55
|
+
title,
|
|
56
|
+
desc
|
|
57
|
+
});
|
|
26
58
|
}
|
|
27
59
|
|
|
28
|
-
|
|
29
|
-
this.log(
|
|
60
|
+
success(title, desc) {
|
|
61
|
+
this.log({
|
|
62
|
+
level: 'success',
|
|
63
|
+
title,
|
|
64
|
+
desc
|
|
65
|
+
});
|
|
30
66
|
}
|
|
31
67
|
|
|
32
|
-
|
|
33
|
-
this.log(
|
|
68
|
+
error(title, desc) {
|
|
69
|
+
this.log({
|
|
70
|
+
level: 'error',
|
|
71
|
+
title,
|
|
72
|
+
desc
|
|
73
|
+
});
|
|
34
74
|
}
|
|
35
75
|
|
|
36
76
|
}
|
package/lib/_cjs/x.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
3
5
|
exports.__esModule = true;
|
|
4
6
|
exports.x = void 0;
|
|
5
7
|
|
|
8
|
+
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
9
|
+
|
|
10
|
+
var _os = _interopRequireDefault(require("os"));
|
|
11
|
+
|
|
12
|
+
var _path = _interopRequireDefault(require("path"));
|
|
13
|
+
|
|
6
14
|
var _dispose = require("./services/dispose");
|
|
7
15
|
|
|
8
16
|
var _log = require("./services/log");
|
|
@@ -11,6 +19,7 @@ const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
|
|
|
11
19
|
const x = {
|
|
12
20
|
appId: env.APP_ID,
|
|
13
21
|
env: env,
|
|
22
|
+
dataDir: _path.default.join(_os.default.homedir(), `.xs/${env.APP_ID}`),
|
|
14
23
|
register: (...services) => {
|
|
15
24
|
for (const service of services) {
|
|
16
25
|
// @ts-ignore
|
|
@@ -19,6 +28,9 @@ const x = {
|
|
|
19
28
|
}
|
|
20
29
|
};
|
|
21
30
|
exports.x = x;
|
|
31
|
+
|
|
32
|
+
_fsExtra.default.ensureDirSync(x.dataDir);
|
|
33
|
+
|
|
22
34
|
x.register(new _dispose.DisposeService({
|
|
23
35
|
disposeOnExit: true
|
|
24
36
|
}), new _log.LogService());
|
package/lib/services/log.d.ts
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import { BaseService } from './base';
|
|
2
|
-
declare type InferKeyword<T extends string> = T extends `${string}{${infer X}}${infer Y}` ? [X, ...InferKeyword<Y>] : [];
|
|
3
2
|
export declare type LogServiceLevel = 'error' | 'info' | 'success';
|
|
3
|
+
export declare type LogServicePayload = {
|
|
4
|
+
level: LogServiceLevel;
|
|
5
|
+
title: string;
|
|
6
|
+
desc?: Record<string, any>;
|
|
7
|
+
};
|
|
8
|
+
export declare type LogServicePayloadWithTime = LogServicePayload & {
|
|
9
|
+
time: string;
|
|
10
|
+
};
|
|
4
11
|
export declare class LogService implements BaseService {
|
|
5
12
|
serviceName: string;
|
|
13
|
+
private separator;
|
|
14
|
+
private get logFile();
|
|
15
|
+
private getLogWriter;
|
|
16
|
+
parseLogLineText(text: string): LogServicePayloadWithTime;
|
|
6
17
|
private log;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
18
|
+
info(title: string, desc?: Record<string, any>): void;
|
|
19
|
+
success(title: string, desc?: Record<string, any>): void;
|
|
20
|
+
error(title: string, desc?: Record<string, any>): void;
|
|
10
21
|
}
|
|
11
22
|
declare module '../x' {
|
|
12
23
|
interface X {
|
|
13
24
|
log: LogService;
|
|
14
25
|
}
|
|
15
26
|
}
|
|
16
|
-
export {};
|
package/lib/services/log.js
CHANGED
|
@@ -1,30 +1,63 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
1
3
|
import { formatDate } from 'vtils/date';
|
|
4
|
+
import { memoize } from 'vtils';
|
|
5
|
+
import { x } from "../x";
|
|
2
6
|
export class LogService {
|
|
3
7
|
constructor() {
|
|
4
8
|
this.serviceName = 'log';
|
|
9
|
+
this.separator = '\t\t\t';
|
|
10
|
+
this.getLogWriter = memoize(logFile => {
|
|
11
|
+
fs.ensureFileSync(logFile);
|
|
12
|
+
return fs.createWriteStream(logFile, {
|
|
13
|
+
flags: 'a'
|
|
14
|
+
});
|
|
15
|
+
}, logFile => logFile);
|
|
5
16
|
}
|
|
6
17
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
18
|
+
get logFile() {
|
|
19
|
+
const now = new Date();
|
|
20
|
+
const year = now.getFullYear();
|
|
21
|
+
const month = now.getMonth() + 1;
|
|
22
|
+
return path.join(x.dataDir, `log/${year}/${month}.log`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
parseLogLineText(text) {
|
|
26
|
+
const [time, level, title, desc] = text.split(this.separator);
|
|
27
|
+
return {
|
|
28
|
+
time: time,
|
|
29
|
+
level: level,
|
|
30
|
+
title: title,
|
|
31
|
+
desc: JSON.parse(desc)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
13
34
|
|
|
14
|
-
|
|
15
|
-
|
|
35
|
+
log(payload) {
|
|
36
|
+
this.getLogWriter(this.logFile).write(`${formatDate(new Date(), 'yyyy-mm-dd hh:ii:ss')}${this.separator}${payload.level}${this.separator}${payload.title}${this.separator}${payload.desc ? JSON.stringify(payload.desc) : '{}'}\n`);
|
|
16
37
|
}
|
|
17
38
|
|
|
18
|
-
|
|
19
|
-
this.log(
|
|
39
|
+
info(title, desc) {
|
|
40
|
+
this.log({
|
|
41
|
+
level: 'info',
|
|
42
|
+
title,
|
|
43
|
+
desc
|
|
44
|
+
});
|
|
20
45
|
}
|
|
21
46
|
|
|
22
|
-
|
|
23
|
-
this.log(
|
|
47
|
+
success(title, desc) {
|
|
48
|
+
this.log({
|
|
49
|
+
level: 'success',
|
|
50
|
+
title,
|
|
51
|
+
desc
|
|
52
|
+
});
|
|
24
53
|
}
|
|
25
54
|
|
|
26
|
-
|
|
27
|
-
this.log(
|
|
55
|
+
error(title, desc) {
|
|
56
|
+
this.log({
|
|
57
|
+
level: 'error',
|
|
58
|
+
title,
|
|
59
|
+
desc
|
|
60
|
+
});
|
|
28
61
|
}
|
|
29
62
|
|
|
30
63
|
}
|
package/lib/x.d.ts
CHANGED
package/lib/x.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import os from 'os';
|
|
3
|
+
import path from 'path';
|
|
1
4
|
import { DisposeService } from "./services/dispose";
|
|
2
5
|
import { LogService } from "./services/log";
|
|
3
6
|
const env = JSON.parse(process.env.X_SERVER_ENVS || '{}');
|
|
4
7
|
export const x = {
|
|
5
8
|
appId: env.APP_ID,
|
|
6
9
|
env: env,
|
|
10
|
+
dataDir: path.join(os.homedir(), `.xs/${env.APP_ID}`),
|
|
7
11
|
register: (...services) => {
|
|
8
12
|
for (const service of services) {
|
|
9
13
|
// @ts-ignore
|
|
@@ -11,6 +15,7 @@ export const x = {
|
|
|
11
15
|
}
|
|
12
16
|
}
|
|
13
17
|
};
|
|
18
|
+
fs.ensureDirSync(x.dataDir);
|
|
14
19
|
x.register(new DisposeService({
|
|
15
20
|
disposeOnExit: true
|
|
16
21
|
}), new LogService());
|