@nocobase/logger 0.11.1-alpha.5 → 0.12.0-alpha.2
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/package.json +2 -2
- package/src/create-app-logger.ts +0 -81
- package/src/create-logger.ts +0 -71
- package/src/index.ts +0 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/logger",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0-alpha.2",
|
|
4
4
|
"description": "nocobase logging library",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -15,5 +15,5 @@
|
|
|
15
15
|
"winston": "^3.8.2",
|
|
16
16
|
"winston-daily-rotate-file": "^4.7.1"
|
|
17
17
|
},
|
|
18
|
-
"gitHead": "
|
|
18
|
+
"gitHead": "a95e9e2666f0318c955113a4735bc005a2c9a767"
|
|
19
19
|
}
|
package/src/create-app-logger.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from 'crypto';
|
|
2
|
-
import Koa from 'koa';
|
|
3
|
-
import { pick } from 'lodash';
|
|
4
|
-
import { createLogger, LoggerOptions } from './create-logger';
|
|
5
|
-
|
|
6
|
-
const defaultRequestWhitelist = [
|
|
7
|
-
'action',
|
|
8
|
-
'header.x-role',
|
|
9
|
-
'header.x-hostname',
|
|
10
|
-
'header.x-timezone',
|
|
11
|
-
'header.x-locale',
|
|
12
|
-
'referer',
|
|
13
|
-
];
|
|
14
|
-
|
|
15
|
-
const defaultResponseWhitelist = ['status'];
|
|
16
|
-
|
|
17
|
-
export interface AppLoggerOptions extends LoggerOptions {
|
|
18
|
-
skip?: (ctx?: any) => Promise<boolean>;
|
|
19
|
-
requestWhitelist?: string[];
|
|
20
|
-
responseWhitelist?: string[];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function createAppLogger(options: AppLoggerOptions = {}) {
|
|
24
|
-
const {
|
|
25
|
-
skip,
|
|
26
|
-
requestWhitelist = defaultRequestWhitelist,
|
|
27
|
-
responseWhitelist = defaultResponseWhitelist,
|
|
28
|
-
...others
|
|
29
|
-
} = options;
|
|
30
|
-
const instance = createLogger(others);
|
|
31
|
-
const middleware = async (ctx: Koa.Context, next: Koa.Next) => {
|
|
32
|
-
if (skip && (await skip(ctx))) {
|
|
33
|
-
return next();
|
|
34
|
-
}
|
|
35
|
-
const logger = ctx.app['logger'];
|
|
36
|
-
const startTime = Date.now();
|
|
37
|
-
const info = {
|
|
38
|
-
level: 'info',
|
|
39
|
-
message: `END: ${ctx.method} ${ctx.url}`,
|
|
40
|
-
req: ctx.request.toJSON(),
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
ctx.reqId = ctx.req['id'] = randomUUID();
|
|
44
|
-
ctx.logger = ctx.log = logger.child({ reqId: ctx.reqId });
|
|
45
|
-
|
|
46
|
-
ctx.logger.info(`BEGIN: ${ctx.method} ${ctx.url}`);
|
|
47
|
-
|
|
48
|
-
let error;
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
await next();
|
|
52
|
-
} catch (e) {
|
|
53
|
-
error = e;
|
|
54
|
-
} finally {
|
|
55
|
-
info['res'] = ctx.response.toJSON();
|
|
56
|
-
// info['status'] = ctx.status;
|
|
57
|
-
if (Math.floor(ctx.status / 100) == 5) {
|
|
58
|
-
info.level = 'error';
|
|
59
|
-
info['errors'] = ctx.body?.['errors'] || ctx.body;
|
|
60
|
-
} else if (Math.floor(ctx.status / 100) == 4) {
|
|
61
|
-
info.level = 'warn';
|
|
62
|
-
info['errors'] = ctx.body?.['errors'] || ctx.body;
|
|
63
|
-
}
|
|
64
|
-
info['responseTime'] = Date.now() - startTime;
|
|
65
|
-
if (ctx.action) {
|
|
66
|
-
info['req']['action'] = ctx.action?.toJSON?.();
|
|
67
|
-
}
|
|
68
|
-
info['req'] = pick(info['req'], requestWhitelist);
|
|
69
|
-
info['res'] = pick(info['res'], responseWhitelist);
|
|
70
|
-
ctx.logger.log(info);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (error) {
|
|
74
|
-
throw error;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
return { instance, middleware };
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export default createAppLogger;
|
package/src/create-logger.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import winston, { format, Logger } from 'winston';
|
|
3
|
-
import 'winston-daily-rotate-file';
|
|
4
|
-
|
|
5
|
-
const { combine, timestamp, colorize, simple } = format;
|
|
6
|
-
|
|
7
|
-
export function getLoggerLevel(): string {
|
|
8
|
-
return process.env.LOGGER_LEVEL || 'info';
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function getLoggerFilePath(...paths: string[]): string {
|
|
12
|
-
return path.resolve(process.env.LOGGER_BASE_PATH || path.resolve(process.cwd(), 'storage', 'logs'), ...paths);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const Transports = {
|
|
16
|
-
console(options) {
|
|
17
|
-
return new winston.transports.Console({
|
|
18
|
-
format: combine(simple(), colorize()),
|
|
19
|
-
...options,
|
|
20
|
-
});
|
|
21
|
-
},
|
|
22
|
-
dailyRotateFile(options: any) {
|
|
23
|
-
let dirname = getLoggerFilePath();
|
|
24
|
-
if (!path.isAbsolute(dirname)) {
|
|
25
|
-
dirname = path.resolve(process.cwd(), dirname);
|
|
26
|
-
}
|
|
27
|
-
return new winston.transports.DailyRotateFile({
|
|
28
|
-
dirname,
|
|
29
|
-
level: getLoggerLevel(),
|
|
30
|
-
filename: 'nocobase-%DATE%.log',
|
|
31
|
-
datePattern: 'YYYY-MM-DD-HH',
|
|
32
|
-
maxFiles: '14d',
|
|
33
|
-
...options,
|
|
34
|
-
});
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
function toArr(value: any) {
|
|
39
|
-
if (Array.isArray(value)) {
|
|
40
|
-
return value;
|
|
41
|
-
}
|
|
42
|
-
return value ? value.split(',') : [];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
type WinstonTransport = string | string[] | winston.transport | winston.transport[];
|
|
46
|
-
|
|
47
|
-
interface LoggerOptions extends Omit<winston.LoggerOptions, 'transports'> {
|
|
48
|
-
transports?: WinstonTransport;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function createLogger(options: LoggerOptions = {}) {
|
|
52
|
-
const transports: winston.transport[] = toArr(options?.transports || ['console', 'dailyRotateFile'])
|
|
53
|
-
.map((t) => {
|
|
54
|
-
if (typeof t === 'string') {
|
|
55
|
-
return Transports[t]();
|
|
56
|
-
}
|
|
57
|
-
return t;
|
|
58
|
-
})
|
|
59
|
-
.filter((t) => t);
|
|
60
|
-
const logger = winston.createLogger({
|
|
61
|
-
level: getLoggerLevel(),
|
|
62
|
-
levels: winston.config.cli.levels,
|
|
63
|
-
format: combine(timestamp(), format.errors({ stack: true }), format.json(), colorize()),
|
|
64
|
-
...options,
|
|
65
|
-
transports,
|
|
66
|
-
});
|
|
67
|
-
winston.addColors(winston.config.cli.colors);
|
|
68
|
-
return logger;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export { Logger, LoggerOptions, Transports, createLogger };
|
package/src/index.ts
DELETED