@naturalcycles/backend-lib 9.30.2 → 9.31.0
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/cloudrun/cloudRun.util.d.ts +1 -1
- package/dist/sentry/sentry.shared.service.js +1 -0
- package/dist/server/asyncLocalStorageMiddleware.js +1 -0
- package/dist/server/logMiddleware.js +12 -3
- package/dist/server/server.model.d.ts +2 -0
- package/package.json +1 -1
- package/src/cloudrun/cloudRun.util.ts +1 -1
- package/src/sentry/sentry.shared.service.ts +1 -0
- package/src/server/asyncLocalStorageMiddleware.ts +1 -0
- package/src/server/logMiddleware.ts +13 -5
- package/src/server/server.model.ts +2 -0
|
@@ -13,7 +13,7 @@ export interface CloudRunDeployInfo {
|
|
|
13
13
|
cloudRunRegion: string;
|
|
14
14
|
sqlInstance?: string;
|
|
15
15
|
vpcConnector?: string;
|
|
16
|
-
serviceUrl
|
|
16
|
+
serviceUrl?: string;
|
|
17
17
|
/**
|
|
18
18
|
* Service URL that is used to access the service externally (through load balancer)
|
|
19
19
|
* todo: overlaps with env.K_EXTERNAL_URL
|
|
@@ -38,6 +38,7 @@ export function getRequestLogger() {
|
|
|
38
38
|
* @experimental
|
|
39
39
|
*/
|
|
40
40
|
export const requestLogger = {
|
|
41
|
+
debug: (...args) => getRequestLogger().debug(...args),
|
|
41
42
|
log: (...args) => getRequestLogger().log(...args),
|
|
42
43
|
warn: (...args) => getRequestLogger().warn(...args),
|
|
43
44
|
error: (...args) => getRequestLogger().error(...args),
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
|
+
import { _objectAssign } from '@naturalcycles/js-lib/types';
|
|
2
3
|
import { _inspect } from '@naturalcycles/nodejs-lib';
|
|
3
4
|
import { dimGrey } from '@naturalcycles/nodejs-lib/colors';
|
|
4
5
|
const { GOOGLE_CLOUD_PROJECT, GAE_INSTANCE, K_SERVICE, APP_ENV } = process.env;
|
|
@@ -13,6 +14,7 @@ let reqCounter = 0;
|
|
|
13
14
|
* To be used in outside-of-request situations (otherwise req.log should be used).
|
|
14
15
|
*/
|
|
15
16
|
export const gcpStructuredLogger = {
|
|
17
|
+
debug: (...args) => writeGCPStructuredLog({ severity: 'DEBUG' }, args),
|
|
16
18
|
log: (...args) => writeGCPStructuredLog({}, args),
|
|
17
19
|
warn: (...args) => writeGCPStructuredLog({ severity: 'WARNING' }, args),
|
|
18
20
|
error: (...args) => writeGCPStructuredLog({ severity: 'ERROR' }, args),
|
|
@@ -22,6 +24,7 @@ export const gcpStructuredLogger = {
|
|
|
22
24
|
* (otherwise req.log should be used).
|
|
23
25
|
*/
|
|
24
26
|
export const devLogger = {
|
|
27
|
+
debug: (...args) => logToDev(null, args),
|
|
25
28
|
log: (...args) => logToDev(null, args),
|
|
26
29
|
warn: (...args) => logToDev(null, args),
|
|
27
30
|
error: (...args) => logToDev(null, args),
|
|
@@ -30,6 +33,7 @@ export const devLogger = {
|
|
|
30
33
|
* Same as devLogger, but without colors (e.g to not confuse Sentry).
|
|
31
34
|
*/
|
|
32
35
|
export const ciLogger = {
|
|
36
|
+
debug: (...args) => logToCI(args),
|
|
33
37
|
log: (...args) => logToCI(args),
|
|
34
38
|
warn: (...args) => logToCI(args),
|
|
35
39
|
error: (...args) => logToCI(args),
|
|
@@ -81,7 +85,8 @@ export function logMiddleware() {
|
|
|
81
85
|
if (isGAE) {
|
|
82
86
|
meta['appengine.googleapis.com/request_id'] = req.header('x-appengine-request-log-id');
|
|
83
87
|
}
|
|
84
|
-
|
|
88
|
+
_objectAssign(req, {
|
|
89
|
+
debug: (...args) => writeGCPStructuredLog({ ...meta, severity: 'DEBUG' }, args),
|
|
85
90
|
log: (...args) => writeGCPStructuredLog({ ...meta, severity: 'INFO' }, args),
|
|
86
91
|
warn: (...args) => writeGCPStructuredLog({ ...meta, severity: 'WARNING' }, args),
|
|
87
92
|
error: (...args) => writeGCPStructuredLog({ ...meta, severity: 'ERROR' }, args),
|
|
@@ -94,14 +99,18 @@ export function logMiddleware() {
|
|
|
94
99
|
return function devLogHandler(req, _res, next) {
|
|
95
100
|
// Local machine
|
|
96
101
|
req.requestId = String(++reqCounter);
|
|
97
|
-
req.
|
|
102
|
+
req.debug =
|
|
103
|
+
req.log =
|
|
104
|
+
req.warn =
|
|
105
|
+
req.error =
|
|
106
|
+
(...args) => logToDev(req.requestId, args);
|
|
98
107
|
next();
|
|
99
108
|
};
|
|
100
109
|
}
|
|
101
110
|
// Otherwise, return "simple" logger
|
|
102
111
|
// This includes: unit tests, CI environments
|
|
103
112
|
return function simpleLogHandler(req, _res, next) {
|
|
104
|
-
req.log = req.warn = req.error = (...args) => logToCI(args);
|
|
113
|
+
req.debug = req.log = req.warn = req.error = (...args) => logToCI(args);
|
|
105
114
|
next();
|
|
106
115
|
};
|
|
107
116
|
}
|
|
@@ -9,6 +9,7 @@ import type { Application, IRouter, NextFunction, Request, Response } from 'expr
|
|
|
9
9
|
* Previous name `ExpressRequest` was clashing with Sentry.
|
|
10
10
|
*/
|
|
11
11
|
export interface BackendRequest extends Request {
|
|
12
|
+
debug: CommonLogFunction;
|
|
12
13
|
log: CommonLogFunction;
|
|
13
14
|
warn: CommonLogFunction;
|
|
14
15
|
error: CommonLogFunction;
|
|
@@ -31,6 +32,7 @@ export type BackendRouter = IRouter;
|
|
|
31
32
|
export type BackendApplication = Application;
|
|
32
33
|
declare module 'http' {
|
|
33
34
|
interface IncomingMessage {
|
|
35
|
+
debug: CommonLogFunction;
|
|
34
36
|
log: CommonLogFunction;
|
|
35
37
|
warn: CommonLogFunction;
|
|
36
38
|
error: CommonLogFunction;
|
package/package.json
CHANGED
|
@@ -53,6 +53,7 @@ export function getRequestLogger(): CommonLogger {
|
|
|
53
53
|
* @experimental
|
|
54
54
|
*/
|
|
55
55
|
export const requestLogger: CommonLogger = {
|
|
56
|
+
debug: (...args) => getRequestLogger().debug(...args),
|
|
56
57
|
log: (...args) => getRequestLogger().log(...args),
|
|
57
58
|
warn: (...args) => getRequestLogger().warn(...args),
|
|
58
59
|
error: (...args) => getRequestLogger().error(...args),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { inspect } from 'node:util'
|
|
2
2
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
3
|
-
import type
|
|
3
|
+
import { _objectAssign, type AnyObject } from '@naturalcycles/js-lib/types'
|
|
4
4
|
import { _inspect } from '@naturalcycles/nodejs-lib'
|
|
5
5
|
import { dimGrey } from '@naturalcycles/nodejs-lib/colors'
|
|
6
6
|
import type { BackendRequestHandler } from './server.model.js'
|
|
@@ -19,6 +19,7 @@ let reqCounter = 0
|
|
|
19
19
|
* To be used in outside-of-request situations (otherwise req.log should be used).
|
|
20
20
|
*/
|
|
21
21
|
export const gcpStructuredLogger: CommonLogger = {
|
|
22
|
+
debug: (...args) => writeGCPStructuredLog({ severity: 'DEBUG' }, args),
|
|
22
23
|
log: (...args) => writeGCPStructuredLog({}, args),
|
|
23
24
|
warn: (...args) => writeGCPStructuredLog({ severity: 'WARNING' }, args),
|
|
24
25
|
error: (...args) => writeGCPStructuredLog({ severity: 'ERROR' }, args),
|
|
@@ -29,6 +30,7 @@ export const gcpStructuredLogger: CommonLogger = {
|
|
|
29
30
|
* (otherwise req.log should be used).
|
|
30
31
|
*/
|
|
31
32
|
export const devLogger: CommonLogger = {
|
|
33
|
+
debug: (...args) => logToDev(null, args),
|
|
32
34
|
log: (...args) => logToDev(null, args),
|
|
33
35
|
warn: (...args) => logToDev(null, args),
|
|
34
36
|
error: (...args) => logToDev(null, args),
|
|
@@ -38,6 +40,7 @@ export const devLogger: CommonLogger = {
|
|
|
38
40
|
* Same as devLogger, but without colors (e.g to not confuse Sentry).
|
|
39
41
|
*/
|
|
40
42
|
export const ciLogger: CommonLogger = {
|
|
43
|
+
debug: (...args) => logToCI(args),
|
|
41
44
|
log: (...args) => logToCI(args),
|
|
42
45
|
warn: (...args) => logToCI(args),
|
|
43
46
|
error: (...args) => logToCI(args),
|
|
@@ -99,11 +102,12 @@ export function logMiddleware(): BackendRequestHandler {
|
|
|
99
102
|
meta['appengine.googleapis.com/request_id'] = req.header('x-appengine-request-log-id')
|
|
100
103
|
}
|
|
101
104
|
|
|
102
|
-
|
|
105
|
+
_objectAssign(req, {
|
|
106
|
+
debug: (...args: any[]) => writeGCPStructuredLog({ ...meta, severity: 'DEBUG' }, args),
|
|
103
107
|
log: (...args: any[]) => writeGCPStructuredLog({ ...meta, severity: 'INFO' }, args),
|
|
104
108
|
warn: (...args: any[]) => writeGCPStructuredLog({ ...meta, severity: 'WARNING' }, args),
|
|
105
109
|
error: (...args: any[]) => writeGCPStructuredLog({ ...meta, severity: 'ERROR' }, args),
|
|
106
|
-
})
|
|
110
|
+
} satisfies CommonLogger)
|
|
107
111
|
|
|
108
112
|
next()
|
|
109
113
|
}
|
|
@@ -114,7 +118,11 @@ export function logMiddleware(): BackendRequestHandler {
|
|
|
114
118
|
return function devLogHandler(req, _res, next) {
|
|
115
119
|
// Local machine
|
|
116
120
|
req.requestId = String(++reqCounter)
|
|
117
|
-
req.
|
|
121
|
+
req.debug =
|
|
122
|
+
req.log =
|
|
123
|
+
req.warn =
|
|
124
|
+
req.error =
|
|
125
|
+
(...args: any[]) => logToDev(req.requestId!, args)
|
|
118
126
|
next()
|
|
119
127
|
}
|
|
120
128
|
}
|
|
@@ -122,7 +130,7 @@ export function logMiddleware(): BackendRequestHandler {
|
|
|
122
130
|
// Otherwise, return "simple" logger
|
|
123
131
|
// This includes: unit tests, CI environments
|
|
124
132
|
return function simpleLogHandler(req, _res, next) {
|
|
125
|
-
req.log = req.warn = req.error = (...args: any[]) => logToCI(args)
|
|
133
|
+
req.debug = req.log = req.warn = req.error = (...args: any[]) => logToCI(args)
|
|
126
134
|
next()
|
|
127
135
|
}
|
|
128
136
|
}
|
|
@@ -10,6 +10,7 @@ import type { Application, IRouter, NextFunction, Request, Response } from 'expr
|
|
|
10
10
|
* Previous name `ExpressRequest` was clashing with Sentry.
|
|
11
11
|
*/
|
|
12
12
|
export interface BackendRequest extends Request {
|
|
13
|
+
debug: CommonLogFunction
|
|
13
14
|
log: CommonLogFunction
|
|
14
15
|
warn: CommonLogFunction
|
|
15
16
|
error: CommonLogFunction
|
|
@@ -49,6 +50,7 @@ export type BackendApplication = Application
|
|
|
49
50
|
|
|
50
51
|
declare module 'http' {
|
|
51
52
|
interface IncomingMessage {
|
|
53
|
+
debug: CommonLogFunction
|
|
52
54
|
log: CommonLogFunction
|
|
53
55
|
warn: CommonLogFunction
|
|
54
56
|
error: CommonLogFunction
|