@kevsjh/cloud-trace-logger 0.0.1 → 0.0.5
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/README.md +19 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,7 +43,15 @@ try {
|
|
|
43
43
|
}
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
In production, errors are annotated with the Cloud Error Reporting `@type` so they appear in the Error Reporting console.
|
|
46
|
+
In production, errors are annotated with the Cloud Error Reporting `@type`, `stack_trace`, and `serviceContext` fields so they appear in the Error Reporting console.
|
|
47
|
+
|
|
48
|
+
On Cloud Run, `serviceContext` is auto-detected from the `K_SERVICE` and `K_REVISION` environment variables. On other platforms, pass it explicitly:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const log = createCloudTraceLogger({
|
|
52
|
+
serviceContext: { service: "my-api", version: "1.0.0" },
|
|
53
|
+
});
|
|
54
|
+
```
|
|
47
55
|
|
|
48
56
|
### With structured data
|
|
49
57
|
|
|
@@ -67,6 +75,10 @@ const log = createCloudTraceLogger({
|
|
|
67
75
|
// Add Cloud Error Reporting @type on error-class severities.
|
|
68
76
|
// Defaults to true when NODE_ENV=production.
|
|
69
77
|
errorReporting: true,
|
|
78
|
+
|
|
79
|
+
// Service context for Error Reporting.
|
|
80
|
+
// Auto-detected from K_SERVICE/K_REVISION on Cloud Run.
|
|
81
|
+
serviceContext: { service: "my-api", version: "1.0.0" },
|
|
70
82
|
});
|
|
71
83
|
```
|
|
72
84
|
|
|
@@ -94,7 +106,12 @@ Each call writes a single JSON line to stdout via `console.log`, which Cloud Log
|
|
|
94
106
|
"errorStack": "Error: connection refused\n at ...",
|
|
95
107
|
"errorName": "Error"
|
|
96
108
|
},
|
|
97
|
-
"@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"
|
|
109
|
+
"@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
|
|
110
|
+
"stack_trace": "Error: connection refused\n at ...",
|
|
111
|
+
"serviceContext": {
|
|
112
|
+
"service": "my-api",
|
|
113
|
+
"version": "my-api-00001-abc"
|
|
114
|
+
}
|
|
98
115
|
}
|
|
99
116
|
```
|
|
100
117
|
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,10 @@ export type Severity = "DEBUG" | "INFO" | "WARNING" | "WARN" | "ERROR" | "CRITIC
|
|
|
2
2
|
export type LoggableRequest = {
|
|
3
3
|
headers: Record<string, string | string[] | undefined>;
|
|
4
4
|
};
|
|
5
|
+
export type ServiceContext = {
|
|
6
|
+
service: string;
|
|
7
|
+
version?: string;
|
|
8
|
+
};
|
|
5
9
|
export type LoggerOptions = {
|
|
6
10
|
/** Google Cloud project ID. Falls back to PROJECT_ID then GOOGLE_CLOUD_PROJECT env vars. */
|
|
7
11
|
projectId?: string;
|
|
@@ -9,6 +13,8 @@ export type LoggerOptions = {
|
|
|
9
13
|
suppressDebug?: boolean;
|
|
10
14
|
/** Add Cloud Error Reporting @type on error-class severities. Defaults to true when NODE_ENV === "production". */
|
|
11
15
|
errorReporting?: boolean;
|
|
16
|
+
/** Service context for Cloud Error Reporting. Auto-detected from K_SERVICE/K_REVISION env vars on Cloud Run if not provided. */
|
|
17
|
+
serviceContext?: ServiceContext;
|
|
12
18
|
};
|
|
13
19
|
export type LogOptions = {
|
|
14
20
|
message: string;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAChB,OAAO,GACP,MAAM,GACN,SAAS,GACT,MAAM,GACN,OAAO,GACP,UAAU,GACV,OAAO,GACP,WAAW,CAAC;AAEhB,MAAM,MAAM,eAAe,GAAG;IAC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F,wBAAgB,sBAAsB,CAAC,OAAO,GAAE,aAAkB,IAqB5C,0CAMjB,UAAU,KAAG,IAAI,CAgFrB"}
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,13 @@ function createCloudTraceLogger(options = {}) {
|
|
|
8
8
|
process.env.GOOGLE_CLOUD_PROJECT;
|
|
9
9
|
const suppressDebug = options.suppressDebug ?? process.env.NODE_ENV === "production";
|
|
10
10
|
const errorReporting = options.errorReporting ?? process.env.NODE_ENV === "production";
|
|
11
|
+
const serviceContext = options.serviceContext ??
|
|
12
|
+
(process.env.K_SERVICE
|
|
13
|
+
? {
|
|
14
|
+
service: process.env.K_SERVICE,
|
|
15
|
+
...(process.env.K_REVISION && { version: process.env.K_REVISION }),
|
|
16
|
+
}
|
|
17
|
+
: undefined);
|
|
11
18
|
return function log({ message, req, severity = "INFO", data, error, }) {
|
|
12
19
|
try {
|
|
13
20
|
const normalizedSeverity = severity === "WARN" ? "WARNING" : severity;
|
|
@@ -45,15 +52,20 @@ function createCloudTraceLogger(options = {}) {
|
|
|
45
52
|
errorName: error.name,
|
|
46
53
|
}
|
|
47
54
|
: { errorValue: String(error) };
|
|
55
|
+
const isErrorReport = errorReporting && ERROR_SEVERITIES.includes(normalizedSeverity);
|
|
56
|
+
const stackTrace = isErrorReport && error instanceof Error && error.stack
|
|
57
|
+
? error.stack
|
|
58
|
+
: undefined;
|
|
48
59
|
const entry = {
|
|
49
60
|
...(trace && { "logging.googleapis.com/trace": trace }),
|
|
50
61
|
...dataFields,
|
|
51
62
|
...(errorFields && { error: errorFields }),
|
|
52
63
|
severity: normalizedSeverity,
|
|
53
64
|
message,
|
|
54
|
-
...(
|
|
55
|
-
ERROR_SEVERITIES.includes(normalizedSeverity) && {
|
|
65
|
+
...(isErrorReport && {
|
|
56
66
|
"@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
|
|
67
|
+
...(stackTrace && { stack_trace: stackTrace }),
|
|
68
|
+
...(serviceContext && { serviceContext }),
|
|
57
69
|
}),
|
|
58
70
|
};
|
|
59
71
|
console.log(JSON.stringify(entry));
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAwCA,wDA2GC;AA7GD,MAAM,gBAAgB,GAAG,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;AAErE,SAAgB,sBAAsB,CAAC,UAAyB,EAAE;IAChE,MAAM,SAAS,GACb,OAAO,CAAC,SAAS;QACjB,OAAO,CAAC,GAAG,CAAC,UAAU;QACtB,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC;IAEnC,MAAM,aAAa,GACjB,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC;IAEjE,MAAM,cAAc,GAClB,OAAO,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC;IAElE,MAAM,cAAc,GAClB,OAAO,CAAC,cAAc;QACtB,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS;YACpB,CAAC,CAAC;gBACE,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS;gBAC9B,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC;aACnE;YACH,CAAC,CAAC,SAAS,CAAC,CAAC;IAEjB,OAAO,SAAS,GAAG,CAAC,EAClB,OAAO,EACP,GAAG,EACH,QAAQ,GAAG,MAAM,EACjB,IAAI,EACJ,KAAK,GACM;QACX,IAAI,CAAC;YACH,MAAM,kBAAkB,GAAG,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAA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