@naturalcycles/backend-lib 9.4.0 → 9.4.1
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.
|
@@ -61,7 +61,9 @@ export function logMiddleware() {
|
|
|
61
61
|
const meta = {
|
|
62
62
|
// Experimental!
|
|
63
63
|
// Testing to include userId in metadata (not message payload) to see if it's searchable
|
|
64
|
-
|
|
64
|
+
labels: {
|
|
65
|
+
userId: req.userId,
|
|
66
|
+
},
|
|
65
67
|
};
|
|
66
68
|
// CloudRun does NOT have this env variable set,
|
|
67
69
|
// so you have to set it manually on deployment, like this:
|
|
@@ -8,11 +8,18 @@ import { onFinished } from '../index.js';
|
|
|
8
8
|
export function requestLoggerMiddleware() {
|
|
9
9
|
return (req, res, next) => {
|
|
10
10
|
const started = Date.now();
|
|
11
|
-
|
|
11
|
+
// todo: include requestId (3-character hash of it?)
|
|
12
|
+
req.log(['>>', req.method, req.originalUrl, req.userId].filter(Boolean).join(' '));
|
|
12
13
|
onFinished(res, () => {
|
|
13
|
-
|
|
14
|
+
const str = ['<<', res.statusCode, _since(started), req.method, req.originalUrl, req.userId]
|
|
14
15
|
.filter(Boolean)
|
|
15
|
-
.join(' ')
|
|
16
|
+
.join(' ');
|
|
17
|
+
if (res.statusCode && res.statusCode >= 400) {
|
|
18
|
+
req.error(str);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
req.log(str);
|
|
22
|
+
}
|
|
16
23
|
});
|
|
17
24
|
next();
|
|
18
25
|
};
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { inspect } from 'node:util'
|
|
2
|
-
import type { AnyObject, CommonLogger
|
|
2
|
+
import type { AnyObject, CommonLogger } from '@naturalcycles/js-lib'
|
|
3
3
|
import { _inspect, dimGrey } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import type { BackendRequestHandler } from './server.model.js'
|
|
5
5
|
|
|
@@ -73,10 +73,12 @@ function logToCI(args: any[]): void {
|
|
|
73
73
|
export function logMiddleware(): BackendRequestHandler {
|
|
74
74
|
if (isGAE || isCloudRun) {
|
|
75
75
|
return function gcpStructuredLogHandler(req, _res, next) {
|
|
76
|
-
const meta:
|
|
76
|
+
const meta: AnyObject = {
|
|
77
77
|
// Experimental!
|
|
78
78
|
// Testing to include userId in metadata (not message payload) to see if it's searchable
|
|
79
|
-
|
|
79
|
+
labels: {
|
|
80
|
+
userId: req.userId,
|
|
81
|
+
},
|
|
80
82
|
}
|
|
81
83
|
|
|
82
84
|
// CloudRun does NOT have this env variable set,
|
|
@@ -12,14 +12,19 @@ export function requestLoggerMiddleware(): BackendRequestHandler {
|
|
|
12
12
|
return (req, res, next) => {
|
|
13
13
|
const started = Date.now() as UnixTimestampMillis
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
// todo: include requestId (3-character hash of it?)
|
|
16
|
+
req.log(['>>', req.method, req.originalUrl, req.userId].filter(Boolean).join(' '))
|
|
16
17
|
|
|
17
18
|
onFinished(res, () => {
|
|
18
|
-
req.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
)
|
|
19
|
+
const str = ['<<', res.statusCode, _since(started), req.method, req.originalUrl, req.userId]
|
|
20
|
+
.filter(Boolean)
|
|
21
|
+
.join(' ')
|
|
22
|
+
|
|
23
|
+
if (res.statusCode && res.statusCode >= 400) {
|
|
24
|
+
req.error(str)
|
|
25
|
+
} else {
|
|
26
|
+
req.log(str)
|
|
27
|
+
}
|
|
23
28
|
})
|
|
24
29
|
|
|
25
30
|
next()
|