@opengis/fastify-table 1.1.32 → 1.1.34
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/index.js +3 -6
- package/logger/createFileStream.js +17 -25
- package/logger/getLogger.js +6 -0
- package/package.json +1 -1
- package/server.js +9 -1
- package/util/controllers/logger.file.js +1 -1
- package/util/controllers/utils/getRootDir.js +11 -8
package/index.js
CHANGED
|
@@ -51,17 +51,14 @@ async function plugin(fastify, opt) {
|
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
fastify.setErrorHandler((error, request, reply) => {
|
|
54
|
-
// console.log(error)
|
|
55
|
-
debugger;
|
|
56
54
|
if (error.validation) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
return reply.status(422).send(`invalid params: ${instancePath?.substring(1)} (${params?.pattern})`);
|
|
55
|
+
request.log.warn(request, { code: error?.code, status: 422, error: error.toString() });
|
|
56
|
+
return reply.status(422).send(error.toString());
|
|
60
57
|
}
|
|
61
58
|
if (error.stack?.startsWith('Error: unhandled exception')) {
|
|
59
|
+
request.log.fatal(request, { status: 501, error: error.toString() });
|
|
62
60
|
return reply.status(500).send('ServerError');
|
|
63
61
|
}
|
|
64
|
-
|
|
65
62
|
return reply.status(error.statusCode || 500).send(error);
|
|
66
63
|
});
|
|
67
64
|
|
|
@@ -1,30 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
const streams = {}
|
|
1
|
+
const streams = {};
|
|
3
2
|
|
|
3
|
+
import build from 'pino-abstract-transport';
|
|
4
4
|
import { createStream } from 'rotating-file-stream';
|
|
5
5
|
import labels from './labels.js';
|
|
6
6
|
|
|
7
7
|
import config from '../config.js';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
const generator = (opt) => () => {
|
|
11
|
-
|
|
9
|
+
const generator = () => () => {
|
|
12
10
|
const date = new Date();
|
|
13
11
|
const tzOffset = date.getTimezoneOffset();
|
|
14
12
|
const currentTimeWithTimezome = date - tzOffset * 60 * 1000;
|
|
15
13
|
|
|
16
|
-
if (opt.interval === '
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
if (opt.interval === '1m') {
|
|
20
|
-
return `${new Date(currentTimeWithTimezome).toISOString().substring(0, 16).replace(/:/g, '_')}.log`;
|
|
21
|
-
}
|
|
22
|
-
if (!opt.interval || opt.interval === '1d') {
|
|
23
|
-
return `${new Date(currentTimeWithTimezome).toISOString().split('T')[0].replace(/:/g, '_')}.log`;
|
|
24
|
-
}
|
|
14
|
+
// if (!opt.interval || opt.interval === '1d') {
|
|
15
|
+
return `${new Date(currentTimeWithTimezome).toISOString().split('T')[0].replace(/:/g, '_')}.log`;
|
|
16
|
+
// }
|
|
25
17
|
};
|
|
26
18
|
|
|
27
|
-
const {
|
|
19
|
+
const {
|
|
20
|
+
dir = 'log', interval = '1d', compress = 'gzip', maxFiles = 90, local: teeToStdout,
|
|
21
|
+
} = config?.log || {};
|
|
28
22
|
function createFileStream({ level }) {
|
|
29
23
|
console.log(dir, level, generator({ interval })(), interval, compress);
|
|
30
24
|
const params = {
|
|
@@ -41,18 +35,16 @@ function createFileStream({ level }) {
|
|
|
41
35
|
return createStream(generator({ interval }), params);
|
|
42
36
|
}
|
|
43
37
|
|
|
44
|
-
|
|
45
|
-
import build from 'pino-abstract-transport'
|
|
46
|
-
|
|
47
38
|
export default function () {
|
|
48
|
-
return build(
|
|
49
|
-
source.on('data',
|
|
39
|
+
return build((source) => {
|
|
40
|
+
source.on('data', (obj) => {
|
|
41
|
+
if (['incoming request', 'request completed'].includes(obj.msg)) return;
|
|
50
42
|
// console.log(obj)
|
|
51
43
|
const lvl = obj.msg?.file || obj.file || labels[obj.level];
|
|
52
|
-
streams[lvl] = streams[lvl] || createFileStream({ level: lvl })
|
|
53
|
-
streams[lvl].write(JSON.stringify(obj)
|
|
54
|
-
})
|
|
44
|
+
streams[lvl] = streams[lvl] || createFileStream({ level: lvl });
|
|
45
|
+
streams[lvl].write(`${JSON.stringify(obj)}\n`);
|
|
46
|
+
});
|
|
55
47
|
}, {
|
|
56
|
-
parseLine: (line) => JSON.parse(line)
|
|
57
|
-
})
|
|
48
|
+
parseLine: (line) => JSON.parse(line),
|
|
49
|
+
});
|
|
58
50
|
}
|
package/logger/getLogger.js
CHANGED
|
@@ -19,6 +19,12 @@ const options = {
|
|
|
19
19
|
transport: {
|
|
20
20
|
targets: [{
|
|
21
21
|
target: './createFileStream.js', // path.resolve('utils/createFileStream.js')
|
|
22
|
+
}, {
|
|
23
|
+
// Use target: 'pino/file' with STDOUT descriptor 1 to write
|
|
24
|
+
// logs without any change.
|
|
25
|
+
level: 'error',
|
|
26
|
+
target: 'pino/file',
|
|
27
|
+
options: { destination: 1 },
|
|
22
28
|
}],
|
|
23
29
|
},
|
|
24
30
|
};
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -9,7 +9,15 @@ import loggerTest from './logger/logger.test.api.js';
|
|
|
9
9
|
const app = Fastify({ logger });
|
|
10
10
|
app.register(appService, config);
|
|
11
11
|
|
|
12
|
-
app.get('/logger-test',
|
|
12
|
+
app.get('/logger-test', {
|
|
13
|
+
schema: {
|
|
14
|
+
querystring: {
|
|
15
|
+
name: { type: 'string' },
|
|
16
|
+
excitement: { type: 'integer' },
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
}, loggerTest);
|
|
20
|
+
|
|
13
21
|
app.listen({ host: '0.0.0.0', port: process.env.PORT || 3000 }, (err) => {
|
|
14
22
|
if (err) {
|
|
15
23
|
app.log.error(err);
|
|
@@ -23,7 +23,7 @@ export default async function loggerFile({
|
|
|
23
23
|
if (access?.status === 403 && !config?.local) return access;
|
|
24
24
|
|
|
25
25
|
// absolute / relative path
|
|
26
|
-
const rootDir = getRootDir(
|
|
26
|
+
const rootDir = getRootDir();
|
|
27
27
|
|
|
28
28
|
const filepath = path.join(rootDir, params['*'] || '');
|
|
29
29
|
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import config from '../../../config.js';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
// import { existsSync } from 'fs';
|
|
6
|
+
let logDir = null;
|
|
7
|
+
export default function getRootDir() {
|
|
5
8
|
// absolute / relative path
|
|
6
|
-
if (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
return
|
|
9
|
+
if (logDir) return logDir;
|
|
10
|
+
const file = ['config.json', '/data/local/config.json'].find(el => (fs.existsSync(el) ? el : null));
|
|
11
|
+
const root = file === 'config.json' ? process.cwd() : '/data/local';
|
|
12
|
+
logDir = config.logDir || path.join(root, config.log?.dir || 'log');
|
|
13
|
+
console.log({ logDir });
|
|
14
|
+
return logDir;
|
|
12
15
|
|
|
13
16
|
// windows debug support
|
|
14
17
|
/* const customLogDir = process.cwd().includes(':') ? 'c:/data/local' : '/data/local';
|