@kevsjh/cloud-trace-logger 0.0.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.
- package/LICENSE +21 -0
- package/README.md +103 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +71 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kevsjh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @kevsjh/cloud-trace-logger
|
|
2
|
+
|
|
3
|
+
Structured JSON logger for Google Cloud. Correlates logs with [Cloud Trace](https://cloud.google.com/trace) and annotates errors for [Cloud Error Reporting](https://cloud.google.com/error-reporting) — zero dependencies.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @kevsjh/cloud-trace-logger
|
|
9
|
+
# or
|
|
10
|
+
npm install @kevsjh/cloud-trace-logger
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createCloudTraceLogger } from "@kevsjh/cloud-trace-logger";
|
|
17
|
+
|
|
18
|
+
const log = createCloudTraceLogger({ projectId: "my-project" });
|
|
19
|
+
|
|
20
|
+
log({ message: "Server started", severity: "INFO" });
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### With request tracing (Express, Fastify, etc.)
|
|
24
|
+
|
|
25
|
+
Pass the request object to correlate logs with Cloud Trace:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
app.get("/", (req, res) => {
|
|
29
|
+
log({ message: "Handling request", req });
|
|
30
|
+
res.send("ok");
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The logger reads the `x-cloud-trace-context` header and adds the `logging.googleapis.com/trace` field automatically.
|
|
35
|
+
|
|
36
|
+
### With error reporting
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
try {
|
|
40
|
+
await doSomething();
|
|
41
|
+
} catch (err) {
|
|
42
|
+
log({ message: "doSomething failed", severity: "ERROR", error: err });
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
In production, errors are annotated with the Cloud Error Reporting `@type` so they appear in the Error Reporting console.
|
|
47
|
+
|
|
48
|
+
### With structured data
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
log({ message: "User signed up", data: { userId: "abc", plan: "pro" } });
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Data fields are spread into the top-level log entry.
|
|
55
|
+
|
|
56
|
+
## Configuration
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const log = createCloudTraceLogger({
|
|
60
|
+
// Google Cloud project ID.
|
|
61
|
+
// Falls back to PROJECT_ID or GOOGLE_CLOUD_PROJECT env vars.
|
|
62
|
+
projectId: "my-project",
|
|
63
|
+
|
|
64
|
+
// Suppress DEBUG logs. Defaults to true when NODE_ENV=production.
|
|
65
|
+
suppressDebug: true,
|
|
66
|
+
|
|
67
|
+
// Add Cloud Error Reporting @type on error-class severities.
|
|
68
|
+
// Defaults to true when NODE_ENV=production.
|
|
69
|
+
errorReporting: true,
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
All options are optional — env vars are used as defaults.
|
|
74
|
+
|
|
75
|
+
## Severity levels
|
|
76
|
+
|
|
77
|
+
All [Google Cloud Logging severity levels](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity) are supported:
|
|
78
|
+
|
|
79
|
+
`DEBUG` | `INFO` | `WARNING` | `ERROR` | `CRITICAL` | `ALERT` | `EMERGENCY`
|
|
80
|
+
|
|
81
|
+
`WARN` is accepted as an alias for `WARNING`.
|
|
82
|
+
|
|
83
|
+
## Output
|
|
84
|
+
|
|
85
|
+
Each call writes a single JSON line to stdout via `console.log`, which Cloud Logging picks up automatically on Cloud Run, Cloud Functions, and GKE.
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"logging.googleapis.com/trace": "projects/my-project/traces/abc123",
|
|
90
|
+
"severity": "ERROR",
|
|
91
|
+
"message": "doSomething failed",
|
|
92
|
+
"error": {
|
|
93
|
+
"errorMessage": "connection refused",
|
|
94
|
+
"errorStack": "Error: connection refused\n at ...",
|
|
95
|
+
"errorName": "Error"
|
|
96
|
+
},
|
|
97
|
+
"@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent"
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type Severity = "DEBUG" | "INFO" | "WARNING" | "WARN" | "ERROR" | "CRITICAL" | "ALERT" | "EMERGENCY";
|
|
2
|
+
export type LoggableRequest = {
|
|
3
|
+
headers: Record<string, string | string[] | undefined>;
|
|
4
|
+
};
|
|
5
|
+
export type LoggerOptions = {
|
|
6
|
+
/** Google Cloud project ID. Falls back to PROJECT_ID then GOOGLE_CLOUD_PROJECT env vars. */
|
|
7
|
+
projectId?: string;
|
|
8
|
+
/** Suppress DEBUG-level logs. Defaults to true when NODE_ENV === "production". */
|
|
9
|
+
suppressDebug?: boolean;
|
|
10
|
+
/** Add Cloud Error Reporting @type on error-class severities. Defaults to true when NODE_ENV === "production". */
|
|
11
|
+
errorReporting?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type LogOptions = {
|
|
14
|
+
message: string;
|
|
15
|
+
req?: LoggableRequest;
|
|
16
|
+
severity?: Severity;
|
|
17
|
+
data?: Record<string, any> | string;
|
|
18
|
+
error?: unknown;
|
|
19
|
+
};
|
|
20
|
+
export declare function createCloudTraceLogger(options?: LoggerOptions): ({ message, req, severity, data, error, }: LogOptions) => void;
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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;IAC5B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;CACxD,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,4FAA4F;IAC5F,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,kFAAkF;IAClF,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,kHAAkH;IAClH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,eAAe,CAAC;IACtB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC;IACpC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAIF,wBAAgB,sBAAsB,CAAC,OAAO,GAAE,aAAkB,IAY5C,0CAMjB,UAAU,KAAG,IAAI,CAwErB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCloudTraceLogger = createCloudTraceLogger;
|
|
4
|
+
const ERROR_SEVERITIES = ["ERROR", "CRITICAL", "ALERT", "EMERGENCY"];
|
|
5
|
+
function createCloudTraceLogger(options = {}) {
|
|
6
|
+
const projectId = options.projectId ??
|
|
7
|
+
process.env.PROJECT_ID ??
|
|
8
|
+
process.env.GOOGLE_CLOUD_PROJECT;
|
|
9
|
+
const suppressDebug = options.suppressDebug ?? process.env.NODE_ENV === "production";
|
|
10
|
+
const errorReporting = options.errorReporting ?? process.env.NODE_ENV === "production";
|
|
11
|
+
return function log({ message, req, severity = "INFO", data, error, }) {
|
|
12
|
+
try {
|
|
13
|
+
const normalizedSeverity = severity === "WARN" ? "WARNING" : severity;
|
|
14
|
+
if (suppressDebug && normalizedSeverity === "DEBUG") {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
// Trace extraction
|
|
18
|
+
let trace;
|
|
19
|
+
if (req && projectId) {
|
|
20
|
+
const traceHeader = req.headers["x-cloud-trace-context"] ??
|
|
21
|
+
req.headers["X-Cloud-Trace-Context"];
|
|
22
|
+
if (traceHeader) {
|
|
23
|
+
const traceValue = Array.isArray(traceHeader)
|
|
24
|
+
? traceHeader[0]
|
|
25
|
+
: traceHeader;
|
|
26
|
+
const traceId = traceValue?.split("/")[0];
|
|
27
|
+
if (traceId) {
|
|
28
|
+
trace = `projects/${projectId}/traces/${traceId}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// Data normalization
|
|
33
|
+
const dataFields = data === undefined
|
|
34
|
+
? {}
|
|
35
|
+
: typeof data === "object" && data !== null && !Array.isArray(data)
|
|
36
|
+
? data
|
|
37
|
+
: { payload: data };
|
|
38
|
+
// Error serialization
|
|
39
|
+
const errorFields = error === undefined
|
|
40
|
+
? undefined
|
|
41
|
+
: error instanceof Error
|
|
42
|
+
? {
|
|
43
|
+
errorMessage: error.message,
|
|
44
|
+
errorStack: error.stack,
|
|
45
|
+
errorName: error.name,
|
|
46
|
+
}
|
|
47
|
+
: { errorValue: String(error) };
|
|
48
|
+
const entry = {
|
|
49
|
+
...(trace && { "logging.googleapis.com/trace": trace }),
|
|
50
|
+
...dataFields,
|
|
51
|
+
...(errorFields && { error: errorFields }),
|
|
52
|
+
severity: normalizedSeverity,
|
|
53
|
+
message,
|
|
54
|
+
...(errorReporting &&
|
|
55
|
+
ERROR_SEVERITIES.includes(normalizedSeverity) && {
|
|
56
|
+
"@type": "type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent",
|
|
57
|
+
}),
|
|
58
|
+
};
|
|
59
|
+
console.log(JSON.stringify(entry));
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
console.error(JSON.stringify({
|
|
63
|
+
loggingInternalError: `Error during logging execution: ${err instanceof Error ? err.message : String(err)}`,
|
|
64
|
+
originalSeverity: severity,
|
|
65
|
+
originalMessage: message,
|
|
66
|
+
severity: "ERROR",
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iBACvF,CAAC;aACL,CAAC;YAEF,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;QACrC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,CACX,IAAI,CAAC,SAAS,CAAC;gBACb,oBAAoB,EAAE,mCAAmC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE;gBAC3G,gBAAgB,EAAE,QAAQ;gBAC1B,eAAe,EAAE,OAAO;gBACxB,QAAQ,EAAE,OAAO;aAClB,CAAC,CACH,CAAC;QACJ,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kevsjh/cloud-trace-logger",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Structured JSON logger with Google Cloud Trace correlation and Cloud Error Reporting support",
|
|
5
|
+
"author": "kevsjh",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/kevsjh/cloud-trace-logger"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"google-cloud",
|
|
13
|
+
"cloud-trace",
|
|
14
|
+
"structured-logging",
|
|
15
|
+
"cloud-run",
|
|
16
|
+
"cloud-error-reporting"
|
|
17
|
+
],
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"require": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc",
|
|
33
|
+
"clean": "rm -rf dist",
|
|
34
|
+
"dev": "tsc -w",
|
|
35
|
+
"prepublishOnly": "bun run clean && bun run build"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^20.11.24",
|
|
39
|
+
"typescript": "^5.0.0"
|
|
40
|
+
}
|
|
41
|
+
}
|