@aomex/logger-sentry 0.0.0-stage → 3.30.8-alpha.62.sha-92b25bb

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-2024 geekact
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 CHANGED
@@ -1,3 +1,3 @@
1
- # Temporary Holding Version
1
+ # @aomex/logger-sentry
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 文档:https://aomex.js.org
@@ -0,0 +1,46 @@
1
+ import * as Sentry from "@sentry/node";
2
+ import { NodeOptions } from "@sentry/node";
3
+ import { LogRecord, LoggerLevel, Transport } from "@aomex/logger";
4
+ /** 与 Sentry `SeverityLevel` 对齐 */
5
+ type SentrySeverityLevel = 'fatal' | 'error' | 'warning' | 'log' | 'info' | 'debug';
6
+ /** Sentry `dataCollection` 配置(来自 `@sentry/node` 的 `NodeOptions`) */
7
+ type SentryDataCollection = NonNullable<NodeOptions['dataCollection']>;
8
+ /**
9
+ * 默认数据采集策略:对齐 mongoose/SDK 升级前的保守行为(相当于 v10 未开 `sendDefaultPii`)。
10
+ * `@sentry/node` v11 若未传 `dataCollection` 会默认采集 cookie / 用户信息 / 请求体等。
11
+ */
12
+ export declare const DEFAULT_SENTRY_DATA_COLLECTION: SentryDataCollection;
13
+ interface SentryTransportOptions {
14
+ /** Sentry DSN;构造时会调用 `Sentry.init` */
15
+ dsn: string;
16
+ /**
17
+ * `flush()` 等待上报的超时(毫秒)。默认 `2000`。
18
+ */
19
+ flushTimeout?: number;
20
+ /**
21
+ * 控制 SDK 自动采集的数据类别。
22
+ * - 省略 / `false`:使用 {@link DEFAULT_SENTRY_DATA_COLLECTION}(关闭)
23
+ * - `true`:沿用 `@sentry/node` v11 默认(开放采集)
24
+ * - 对象:在关闭默认上浅合并覆盖(如 `{ cookies: true }`)
25
+ */
26
+ dataCollection?: boolean | SentryDataCollection;
27
+ }
28
+ /**
29
+ * 把日志送到 Sentry。构造时用 `dsn` 完成 `Sentry.init`。
30
+ *
31
+ * ```typescript
32
+ * new SentryTransport({ dsn: process.env.SENTRY_DSN! })
33
+ * new SentryTransport({ dsn, dataCollection: true })
34
+ * new SentryTransport({ dsn, dataCollection: { cookies: true } })
35
+ * ```
36
+ */
37
+ export declare class SentryTransport extends Transport {
38
+ protected readonly flushTimeout: number;
39
+ constructor(opts: SentryTransportOptions);
40
+ /** `true` → `undefined`(不传给 init,走 SDK 默认);否则关闭默认 + 浅合并 */
41
+ protected static resolveDataCollection(value: boolean | SentryDataCollection | undefined): SentryDataCollection | undefined;
42
+ protected toSentrySeverity(level: LoggerLevel): SentrySeverityLevel;
43
+ consume(log: LogRecord): Promise<void>;
44
+ flush(): Promise<void>;
45
+ }
46
+ export { Sentry, type SentryDataCollection, type SentrySeverityLevel, type SentryTransportOptions };
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ import * as Sentry from "@sentry/node";
2
+ import { Transport } from "@aomex/logger";
3
+ // src/sentry-transport.ts
4
+ /**
5
+ * 默认数据采集策略:对齐 mongoose/SDK 升级前的保守行为(相当于 v10 未开 `sendDefaultPii`)。
6
+ * `@sentry/node` v11 若未传 `dataCollection` 会默认采集 cookie / 用户信息 / 请求体等。
7
+ */
8
+ const DEFAULT_SENTRY_DATA_COLLECTION = {
9
+ userInfo: false,
10
+ cookies: false,
11
+ httpHeaders: false,
12
+ httpBodies: [],
13
+ urlQueryParams: false,
14
+ genAI: {
15
+ inputs: false,
16
+ outputs: false
17
+ },
18
+ mcp: {
19
+ inputs: false,
20
+ outputs: false
21
+ },
22
+ databaseQueryData: false,
23
+ queues: false,
24
+ graphQL: {
25
+ document: false,
26
+ variables: false
27
+ }
28
+ };
29
+ const LEVEL_TO_SENTRY = {
30
+ trace: "debug",
31
+ debug: "debug",
32
+ info: "info",
33
+ warn: "warning",
34
+ error: "error",
35
+ fatal: "fatal"
36
+ };
37
+ const exceptionToError = (exception) => {
38
+ const error = new Error(exception.message);
39
+ error.name = exception.type || "Error";
40
+ if (exception.stack) error.stack = exception.stack;
41
+ if (exception.cause) error.cause = exceptionToError(exception.cause);
42
+ return error;
43
+ };
44
+ /**
45
+ * 把日志送到 Sentry。构造时用 `dsn` 完成 `Sentry.init`。
46
+ *
47
+ * ```typescript
48
+ * new SentryTransport({ dsn: process.env.SENTRY_DSN! })
49
+ * new SentryTransport({ dsn, dataCollection: true })
50
+ * new SentryTransport({ dsn, dataCollection: { cookies: true } })
51
+ * ```
52
+ */
53
+ var SentryTransport = class SentryTransport extends Transport {
54
+ flushTimeout;
55
+ constructor(opts) {
56
+ super();
57
+ const dataCollection = SentryTransport.resolveDataCollection(opts.dataCollection);
58
+ Sentry.init({
59
+ dsn: opts.dsn,
60
+ ...dataCollection ? { dataCollection } : {}
61
+ });
62
+ this.flushTimeout = opts.flushTimeout ?? 2e3;
63
+ }
64
+ /** `true` → `undefined`(不传给 init,走 SDK 默认);否则关闭默认 + 浅合并 */
65
+ static resolveDataCollection(value) {
66
+ if (value === true) return;
67
+ if (value === false || value === void 0) return DEFAULT_SENTRY_DATA_COLLECTION;
68
+ return {
69
+ ...DEFAULT_SENTRY_DATA_COLLECTION,
70
+ ...value
71
+ };
72
+ }
73
+ toSentrySeverity(level) {
74
+ return LEVEL_TO_SENTRY[level];
75
+ }
76
+ async consume(log) {
77
+ const level = this.toSentrySeverity(log.level);
78
+ Sentry.withScope((scope) => {
79
+ scope.setLevel(level);
80
+ scope.setExtra("logger.level", log.level);
81
+ scope.setExtra("logger.time", log.timestamp.toISOString());
82
+ if (log.bindings) scope.setContext("logger.bindings", log.bindings);
83
+ if (log.exception) {
84
+ scope.setContext("logger.exception", {
85
+ ...log.exception,
86
+ cause: log.exception.cause ?? null
87
+ });
88
+ Sentry.captureException(exceptionToError(log.exception), {
89
+ level,
90
+ extra: { message: log.message }
91
+ });
92
+ return;
93
+ }
94
+ Sentry.captureMessage(log.message, { level });
95
+ });
96
+ }
97
+ async flush() {
98
+ await Sentry.flush(this.flushTimeout);
99
+ }
100
+ };
101
+ export { DEFAULT_SENTRY_DATA_COLLECTION, Sentry, SentryTransport };
102
+
103
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/sentry-transport.ts"],"sourcesContent":["import * as Sentry from '@sentry/node';\nimport type { NodeOptions } from '@sentry/node';\nimport {\n Transport,\n type LogRecord,\n type LoggerLevel,\n type SerializedError,\n} from '@aomex/logger';\n\n/** 与 Sentry `SeverityLevel` 对齐 */\nexport type SentrySeverityLevel =\n 'fatal' | 'error' | 'warning' | 'log' | 'info' | 'debug';\n\n/** Sentry `dataCollection` 配置(来自 `@sentry/node` 的 `NodeOptions`) */\nexport type SentryDataCollection = NonNullable<NodeOptions['dataCollection']>;\n\n/**\n * 默认数据采集策略:对齐 mongoose/SDK 升级前的保守行为(相当于 v10 未开 `sendDefaultPii`)。\n * `@sentry/node` v11 若未传 `dataCollection` 会默认采集 cookie / 用户信息 / 请求体等。\n */\nexport const DEFAULT_SENTRY_DATA_COLLECTION: SentryDataCollection = {\n userInfo: false,\n cookies: false,\n httpHeaders: false,\n httpBodies: [],\n urlQueryParams: false,\n genAI: { inputs: false, outputs: false },\n mcp: { inputs: false, outputs: false },\n databaseQueryData: false,\n queues: false,\n graphQL: { document: false, variables: false },\n};\n\nexport interface SentryTransportOptions {\n /** Sentry DSN;构造时会调用 `Sentry.init` */\n dsn: string;\n /**\n * `flush()` 等待上报的超时(毫秒)。默认 `2000`。\n */\n flushTimeout?: number;\n /**\n * 控制 SDK 自动采集的数据类别。\n * - 省略 / `false`:使用 {@link DEFAULT_SENTRY_DATA_COLLECTION}(关闭)\n * - `true`:沿用 `@sentry/node` v11 默认(开放采集)\n * - 对象:在关闭默认上浅合并覆盖(如 `{ cookies: true }`)\n */\n dataCollection?: boolean | SentryDataCollection;\n}\n\nconst LEVEL_TO_SENTRY: Record<LoggerLevel, SentrySeverityLevel> = {\n trace: 'debug',\n debug: 'debug',\n info: 'info',\n warn: 'warning',\n error: 'error',\n fatal: 'fatal',\n};\n\nconst exceptionToError = (exception: SerializedError): Error => {\n const error = new Error(exception.message);\n error.name = exception.type || 'Error';\n if (exception.stack) {\n error.stack = exception.stack;\n }\n if (exception.cause) {\n (error as Error & { cause: Error }).cause = exceptionToError(exception.cause);\n }\n return error;\n};\n\n/**\n * 把日志送到 Sentry。构造时用 `dsn` 完成 `Sentry.init`。\n *\n * ```typescript\n * new SentryTransport({ dsn: process.env.SENTRY_DSN! })\n * new SentryTransport({ dsn, dataCollection: true })\n * new SentryTransport({ dsn, dataCollection: { cookies: true } })\n * ```\n */\nexport class SentryTransport extends Transport {\n protected readonly flushTimeout: number;\n\n constructor(opts: SentryTransportOptions) {\n super();\n const dataCollection = SentryTransport.resolveDataCollection(opts.dataCollection);\n Sentry.init({\n dsn: opts.dsn,\n ...(dataCollection ? { dataCollection } : {}),\n });\n this.flushTimeout = opts.flushTimeout ?? 2000;\n }\n\n /** `true` → `undefined`(不传给 init,走 SDK 默认);否则关闭默认 + 浅合并 */\n protected static resolveDataCollection(\n value: boolean | SentryDataCollection | undefined,\n ): SentryDataCollection | undefined {\n if (value === true) {\n return undefined;\n }\n if (value === false || value === undefined) {\n return DEFAULT_SENTRY_DATA_COLLECTION;\n }\n return {\n ...DEFAULT_SENTRY_DATA_COLLECTION,\n ...value,\n };\n }\n\n protected toSentrySeverity(level: LoggerLevel): SentrySeverityLevel {\n return LEVEL_TO_SENTRY[level];\n }\n\n override async consume(log: LogRecord): Promise<void> {\n const level = this.toSentrySeverity(log.level);\n\n Sentry.withScope((scope) => {\n scope.setLevel(level);\n scope.setExtra('logger.level', log.level);\n scope.setExtra('logger.time', log.timestamp.toISOString());\n if (log.bindings) {\n scope.setContext('logger.bindings', log.bindings);\n }\n\n if (log.exception) {\n scope.setContext('logger.exception', {\n ...log.exception,\n cause: log.exception.cause ?? null,\n });\n Sentry.captureException(exceptionToError(log.exception), {\n level,\n extra: {\n message: log.message,\n },\n });\n return;\n }\n\n Sentry.captureMessage(log.message, { level });\n });\n }\n\n override async flush(): Promise<void> {\n await Sentry.flush(this.flushTimeout);\n }\n}\n"],"mappings":";;;;;;;AAoBA,MAAa,iCAAuD;CAClE,UAAU;CACV,SAAS;CACT,aAAa;CACb,YAAY,CAAC;CACb,gBAAgB;CAChB,OAAO;EAAE,QAAQ;EAAO,SAAS;CAAM;CACvC,KAAK;EAAE,QAAQ;EAAO,SAAS;CAAM;CACrC,mBAAmB;CACnB,QAAQ;CACR,SAAS;EAAE,UAAU;EAAO,WAAW;CAAM;AAC/C;AAkBA,MAAM,kBAA4D;CAChE,OAAO;CACP,OAAO;CACP,MAAM;CACN,MAAM;CACN,OAAO;CACP,OAAO;AACT;AAEA,MAAM,oBAAoB,cAAsC;CAC9D,MAAM,QAAQ,IAAI,MAAM,UAAU,OAA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package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@aomex/logger-sentry",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "3.30.8-alpha.62.sha-92b25bb",
4
+ "description": "aomex logger Sentry transport",
5
+ "repository": "git@github.com:aomex/aomex.git",
6
+ "homepage": "https://aomex.js.org",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "scripts": {
11
+ "prepublishOnly": "tsdown"
12
+ },
13
+ "types": "src/index.ts",
14
+ "exports": "./src/index.ts",
15
+ "files": [
16
+ "dist",
17
+ "package.json",
18
+ "README.md",
19
+ "CHANGELOG.md"
20
+ ],
21
+ "publishConfig": {
22
+ "types": "dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js"
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "access": "public"
31
+ },
32
+ "peerDependencies": {
33
+ "@aomex/logger": "^4.0.0"
34
+ },
35
+ "dependencies": {
36
+ "@sentry/node": "^11.12.0"
37
+ },
38
+ "devDependencies": {
39
+ "@aomex/logger": "3.30.8-alpha.62.sha-92b25bb"
40
+ },
41
+ "gitHead": "92b25bbcd6c3ae28160316117a74c88619b5bc8d"
42
+ }