@prosopo/logger 2.0.2 → 2.0.4
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/.turbo/turbo-build$colon$cjs.log +10 -8
- package/.turbo/turbo-build$colon$tsc.log +6 -6
- package/.turbo/turbo-build.log +12 -9
- package/CHANGELOG.md +47 -0
- package/dist/_virtual/_rolldown/runtime.js +3 -0
- package/dist/cjs/index.cjs +9 -10
- package/dist/cjs/logger.cjs +227 -236
- package/dist/index.js +2 -10
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +225 -241
- package/dist/logger.js.map +1 -1
- package/dist/tests/logger.unit.test.js +34 -0
- package/dist/tests/logger.unit.test.js.map +1 -1
- package/package.json +6 -6
- package/src/logger.ts +15 -0
- package/src/tests/logger.unit.test.ts +41 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @prosopo/logger@2.0.
|
|
2
|
+
> @prosopo/logger@2.0.4 build:cjs
|
|
3
3
|
> NODE_ENV=${NODE_ENV:-development}; vite build --config vite.cjs.config.ts --mode $NODE_ENV
|
|
4
4
|
|
|
5
5
|
ViteCommonJSConfig: .
|
|
@@ -7,12 +7,14 @@ ViteCommonJSConfig: .
|
|
|
7
7
|
tsConfigPaths: [ '/home/runner/work/captcha/captcha/packages/util/tsconfig.json' ]
|
|
8
8
|
}
|
|
9
9
|
{ externals: [ '@prosopo/util' ] }
|
|
10
|
-
[
|
|
10
|
+
[33mThe plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the [1mresolve.tsconfigPaths[22m option. You can remove the plugin and set [1mresolve.tsconfigPaths: true[22m in your Vite config instead.[39m
|
|
11
|
+
[36mvite v8.1.5 [32mbuilding ssr environment for production...[36m[39m
|
|
11
12
|
Bundle build started
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
[32m✓[39m 2 modules transformed.
|
|
13
|
+
[2K
|
|
14
|
+
✓ 3 modules transformed.
|
|
15
15
|
rendering chunks...
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
computing gzip size...
|
|
17
|
+
dist/cjs/index.cjs 0.53 kB │ gzip: 0.22 kB
|
|
18
|
+
dist/cjs/logger.cjs 7.10 kB │ gzip: 2.37 kB
|
|
19
|
+
|
|
20
|
+
[32m✓ built in 44ms[39m
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
> @prosopo/logger@2.0.
|
|
2
|
+
> @prosopo/logger@2.0.4 build:tsc
|
|
3
3
|
> tsc --build --verbose
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
8:58:46 PM - Projects in this build:
|
|
6
6
|
* ../../dev/config/tsconfig.json
|
|
7
7
|
* ../util/tsconfig.json
|
|
8
8
|
* tsconfig.json
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
8:58:46 PM - Project '../../dev/config/tsconfig.json' is up to date because newest input '../../dev/config/src/webpack/webpack.config.ts' is older than output '../../dev/config/tsconfig.tsbuildinfo'
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
8:58:46 PM - Project '../util/tsconfig.json' is up to date because newest input '../util/src/url.ts' is older than output '../util/tsconfig.tsbuildinfo'
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
8:58:46 PM - Project 'tsconfig.json' is out of date because output file 'tsconfig.tsbuildinfo' does not exist
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
8:58:46 PM - Building project '/home/runner/work/captcha/captcha/packages/logger/tsconfig.json'...
|
|
17
17
|
|
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
> @prosopo/logger@2.0.
|
|
2
|
+
> @prosopo/logger@2.0.4 build
|
|
3
3
|
> npm run build:cross-env -- --mode ${NODE_ENV:-development}
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
> @prosopo/logger@2.0.
|
|
6
|
+
> @prosopo/logger@2.0.4 build:cross-env
|
|
7
7
|
> vite build --config vite.esm.config.ts --mode production
|
|
8
8
|
|
|
9
9
|
ViteEsmConfig: .
|
|
@@ -11,12 +11,15 @@ ViteEsmConfig: .
|
|
|
11
11
|
tsConfigPaths: [ '/home/runner/work/captcha/captcha/packages/util/tsconfig.json' ]
|
|
12
12
|
}
|
|
13
13
|
{ externals: [ '@prosopo/util' ] }
|
|
14
|
-
[
|
|
14
|
+
[33mThe plugin "vite-tsconfig-paths" is detected. Vite now supports tsconfig paths resolution natively via the [1mresolve.tsconfigPaths[22m option. You can remove the plugin and set [1mresolve.tsconfigPaths: true[22m in your Vite config instead.[39m
|
|
15
|
+
[36mvite v8.1.5 [32mbuilding ssr environment for production...[36m[39m
|
|
15
16
|
Bundle build started
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
[32m✓[39m 2 modules transformed.
|
|
17
|
+
[2K
|
|
18
|
+
✓ 3 modules transformed.
|
|
19
19
|
rendering chunks...
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
computing gzip size...
|
|
21
|
+
dist/_virtual/_rolldown/runtime.js 0.04 kB │ gzip: 0.06 kB
|
|
22
|
+
dist/index.js 0.30 kB │ gzip: 0.15 kB
|
|
23
|
+
dist/logger.js 6.90 kB │ gzip: 2.33 kB
|
|
24
|
+
|
|
25
|
+
[32m✓ built in 49ms[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,52 @@
|
|
|
1
1
|
# @prosopo/logger
|
|
2
2
|
|
|
3
|
+
## 2.0.4
|
|
4
|
+
### Patch Changes
|
|
5
|
+
|
|
6
|
+
- Updated dependencies [2c47bb7]
|
|
7
|
+
- Updated dependencies [0e1171c]
|
|
8
|
+
- @prosopo/util@3.3.5
|
|
9
|
+
|
|
10
|
+
## 2.0.3
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 6abff15: Fix request-scoped logger fields leaking across concurrent captcha requests, and give every endpoint a proper request/response envelope in OpenObserve.
|
|
14
|
+
|
|
15
|
+
Three interlocking changes:
|
|
16
|
+
|
|
17
|
+
- **`Tasks.setLogger` no longer mutates `db.logger`.** `env.getDb()` returns a
|
|
18
|
+
process-wide singleton; overwriting `db.logger` on every request meant two
|
|
19
|
+
concurrent captcha submits raced, and whichever request landed second
|
|
20
|
+
stamped its `user`/`siteKey`/`sessionId` bindings onto the *other* request's
|
|
21
|
+
DB-level log lines. In practice you'd see a `PuzzleCaptcha record updated
|
|
22
|
+
successfully` for user A's challenge tagged with user B's account and site
|
|
23
|
+
key, breaking log-based forensics. `setLogger` still updates the per-request
|
|
24
|
+
Tasks instance and its per-request manager instances (those are safe —
|
|
25
|
+
they're constructed inside the Tasks constructor) but stops mutating the
|
|
26
|
+
shared DB. Callers in `getPoWCaptchaChallenge` and `getPuzzleCaptchaChallenge`
|
|
27
|
+
now pass `req.logger` directly into `new Tasks(env, req.logger)` and drop
|
|
28
|
+
the redundant `.setLogger(req.logger)` call that followed.
|
|
29
|
+
|
|
30
|
+
- **`requestLoggerMiddleware` now emits `Request received` and `Response sent`
|
|
31
|
+
envelope lines on every route** (with `method`, `path`, `status`,
|
|
32
|
+
`durationMs`, and the request id). Previously only `/frictionless` had a
|
|
33
|
+
`res.on('finish', ...)` block, so `getPow/PuzzleCaptchaChallenge`,
|
|
34
|
+
`submitPow/PuzzleCaptchaSolution`, `verify.ts` etc. produced no envelope in
|
|
35
|
+
OO — a challenge issued by one endpoint and verified by another shared
|
|
36
|
+
nothing you could group on. Health-probe paths (`/healthz`, `/health`,
|
|
37
|
+
`/readyz`) are excluded so they don't drown the stream. The middleware
|
|
38
|
+
also now mirrors `x-request-id` back on the outbound response so callers
|
|
39
|
+
downstream of the Node process can correlate without depending on Caddy.
|
|
40
|
+
|
|
41
|
+
- **`requestId` (set on the request logger via `.with({requestId})`) is
|
|
42
|
+
promoted to a top-level `req_id` field on the emitted JSON log record.**
|
|
43
|
+
OpenObserve indexes top-level fields as their own columns, so
|
|
44
|
+
`WHERE req_id = '…'` is now cheap; previously the id only lived inside
|
|
45
|
+
`data.requestId`, which flattened to `data_requestid` in OO's ingestion
|
|
46
|
+
and had no top-level column. `data.requestId` is preserved for backwards
|
|
47
|
+
compatibility with existing dashboards. Two new unit tests in
|
|
48
|
+
`@prosopo/logger` cover the promotion and the "absent when unset" case.
|
|
49
|
+
|
|
3
50
|
## 2.0.2
|
|
4
51
|
### Patch Changes
|
|
5
52
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
exports.Format =
|
|
5
|
-
exports.LogLevel =
|
|
6
|
-
exports.NativeLogger =
|
|
7
|
-
exports.getLogger =
|
|
8
|
-
exports.parseDirectives =
|
|
9
|
-
exports.parseLogLevel =
|
|
10
|
-
exports.resolveLevel =
|
|
11
|
-
exports.setGlobalDirectives =
|
|
2
|
+
const require_logger = require("./logger.cjs");
|
|
3
|
+
exports.Format = require_logger.Format;
|
|
4
|
+
exports.LogLevel = require_logger.LogLevel;
|
|
5
|
+
exports.NativeLogger = require_logger.NativeLogger;
|
|
6
|
+
exports.getLogger = require_logger.getLogger;
|
|
7
|
+
exports.parseDirectives = require_logger.parseDirectives;
|
|
8
|
+
exports.parseLogLevel = require_logger.parseLogLevel;
|
|
9
|
+
exports.resolveLevel = require_logger.resolveLevel;
|
|
10
|
+
exports.setGlobalDirectives = require_logger.setGlobalDirectives;
|
package/dist/cjs/logger.cjs
CHANGED
|
@@ -1,253 +1,244 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
FatalLevel
|
|
1
|
+
let _prosopo_util = require("@prosopo/util");
|
|
2
|
+
let zod = require("zod");
|
|
3
|
+
//#region src/logger.ts
|
|
4
|
+
var InfoLevel = "info";
|
|
5
|
+
var DebugLevel = "debug";
|
|
6
|
+
var TraceLevel = "trace";
|
|
7
|
+
var WarnLevel = "warn";
|
|
8
|
+
var ErrorLevel = "error";
|
|
9
|
+
var FatalLevel = "fatal";
|
|
10
|
+
var LogLevel = zod.z.enum([
|
|
11
|
+
InfoLevel,
|
|
12
|
+
DebugLevel,
|
|
13
|
+
TraceLevel,
|
|
14
|
+
WarnLevel,
|
|
15
|
+
ErrorLevel,
|
|
16
|
+
FatalLevel
|
|
18
17
|
]);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
18
|
+
var logLevelMap = {
|
|
19
|
+
[TraceLevel]: 0,
|
|
20
|
+
[DebugLevel]: 1,
|
|
21
|
+
[InfoLevel]: 2,
|
|
22
|
+
[WarnLevel]: 3,
|
|
23
|
+
[ErrorLevel]: 4,
|
|
24
|
+
[FatalLevel]: 5
|
|
26
25
|
};
|
|
27
26
|
function parseLogLevel(level, or = InfoLevel) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
if (!level) return or;
|
|
28
|
+
for (const part of level.split(",")) {
|
|
29
|
+
const trimmed = part.trim();
|
|
30
|
+
if (trimmed && !trimmed.includes("=")) {
|
|
31
|
+
const parsed = LogLevel.safeParse(trimmed);
|
|
32
|
+
if (parsed.success) return parsed.data;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return or;
|
|
37
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse a PROSOPO_LOG_LEVEL directive string into a scope→level map.
|
|
39
|
+
* An entry with an empty-string key represents the global default.
|
|
40
|
+
*/
|
|
38
41
|
function parseDirectives(raw) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
42
|
+
const map = /* @__PURE__ */ new Map();
|
|
43
|
+
for (const part of raw.split(",")) {
|
|
44
|
+
const trimmed = part.trim();
|
|
45
|
+
if (!trimmed) continue;
|
|
46
|
+
const eqIdx = trimmed.indexOf("=");
|
|
47
|
+
if (eqIdx === -1) {
|
|
48
|
+
const parsed = LogLevel.safeParse(trimmed);
|
|
49
|
+
if (parsed.success) map.set("", parsed.data);
|
|
50
|
+
} else {
|
|
51
|
+
const scope = trimmed.slice(0, eqIdx).trim();
|
|
52
|
+
if (!scope) continue;
|
|
53
|
+
const parsed = LogLevel.safeParse(trimmed.slice(eqIdx + 1).trim());
|
|
54
|
+
if (parsed.success) map.set(scope, parsed.data);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return map;
|
|
55
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Find the most specific directive that matches the given scope.
|
|
61
|
+
* Walks from the full scope up to the empty-string global default.
|
|
62
|
+
* Returns the fallback if nothing matches.
|
|
63
|
+
*/
|
|
56
64
|
function resolveLevel(scope, directives, fallback) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
if (directives.has(scope)) return directives.get(scope);
|
|
66
|
+
const parts = scope.split(":");
|
|
67
|
+
for (let i = parts.length - 1; i > 0; i--) {
|
|
68
|
+
const prefix = parts.slice(0, i).join(":");
|
|
69
|
+
if (directives.has(prefix)) return directives.get(prefix);
|
|
70
|
+
}
|
|
71
|
+
if (directives.has("")) return directives.get("");
|
|
72
|
+
return fallback;
|
|
65
73
|
}
|
|
66
|
-
|
|
74
|
+
var _globalDirectives;
|
|
67
75
|
function getGlobalDirectives() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
_globalDirectives = parseDirectives(raw);
|
|
71
|
-
}
|
|
72
|
-
return _globalDirectives;
|
|
76
|
+
if (!_globalDirectives) _globalDirectives = parseDirectives(typeof process !== "undefined" ? process.env?.PROSOPO_LOG_LEVEL ?? "" : "");
|
|
77
|
+
return _globalDirectives;
|
|
73
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Override the global directives at runtime (useful in tests or at app startup).
|
|
81
|
+
* Accepts the same directive string as PROSOPO_LOG_LEVEL.
|
|
82
|
+
*/
|
|
74
83
|
function setGlobalDirectives(raw) {
|
|
75
|
-
|
|
84
|
+
_globalDirectives = parseDirectives(raw);
|
|
76
85
|
}
|
|
77
86
|
function getLogger(logLevel, scope) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
const inBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
83
|
-
const FormatJson = "json";
|
|
84
|
-
const FormatPlain = "plain";
|
|
85
|
-
const Format = zod.z.enum([FormatJson, FormatPlain]);
|
|
86
|
-
class NativeLogger {
|
|
87
|
-
constructor(scope, levelMap = logLevelMap) {
|
|
88
|
-
this.scope = scope;
|
|
89
|
-
this.levelMap = levelMap;
|
|
90
|
-
this.pretty = 0;
|
|
91
|
-
this.printStack = false;
|
|
92
|
-
this.format = FormatJson;
|
|
93
|
-
this.level = InfoLevel;
|
|
94
|
-
}
|
|
95
|
-
setFormat(format) {
|
|
96
|
-
this.format = format;
|
|
97
|
-
if (this.format !== FormatJson) {
|
|
98
|
-
throw new Error("Only JSON format implemented for now");
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
getFormat() {
|
|
102
|
-
return this.format;
|
|
103
|
-
}
|
|
104
|
-
with(obj, subscope) {
|
|
105
|
-
const trimmedSubscope = subscope?.trim();
|
|
106
|
-
const newScope = trimmedSubscope ? this.scope ? `${this.scope}:${trimmedSubscope}` : trimmedSubscope : this.scope;
|
|
107
|
-
const newLogger = new NativeLogger(newScope, this.levelMap);
|
|
108
|
-
const hasObjData = Object.keys(obj).length > 0;
|
|
109
|
-
if (hasObjData) {
|
|
110
|
-
newLogger.defaultData = { ...this.defaultData, ...obj };
|
|
111
|
-
} else if (this.defaultData) {
|
|
112
|
-
newLogger.defaultData = this.defaultData;
|
|
113
|
-
}
|
|
114
|
-
newLogger.setPretty(this.getPretty());
|
|
115
|
-
newLogger.setPrintStack(this.getPrintStack());
|
|
116
|
-
newLogger.setLogLevel(this.getLogLevel());
|
|
117
|
-
return newLogger;
|
|
118
|
-
}
|
|
119
|
-
getPrintStack() {
|
|
120
|
-
return this.printStack;
|
|
121
|
-
}
|
|
122
|
-
setPrintStack(printStack) {
|
|
123
|
-
this.printStack = printStack;
|
|
124
|
-
}
|
|
125
|
-
getPretty() {
|
|
126
|
-
return this.pretty > 0;
|
|
127
|
-
}
|
|
128
|
-
setPretty(pretty) {
|
|
129
|
-
this.pretty = pretty ? 2 : 0;
|
|
130
|
-
}
|
|
131
|
-
getScope() {
|
|
132
|
-
return this.scope;
|
|
133
|
-
}
|
|
134
|
-
setLogLevel(level) {
|
|
135
|
-
this.level = level;
|
|
136
|
-
}
|
|
137
|
-
getLogLevel() {
|
|
138
|
-
return this.level;
|
|
139
|
-
}
|
|
140
|
-
unpackError(err) {
|
|
141
|
-
const e = err;
|
|
142
|
-
const data = {};
|
|
143
|
-
for (const key of Object.getOwnPropertyNames(err)) {
|
|
144
|
-
if (!this.printStack && (key === "stack" || key === "stacktrace"))
|
|
145
|
-
continue;
|
|
146
|
-
data[key] = e[key];
|
|
147
|
-
}
|
|
148
|
-
if (!data.name) {
|
|
149
|
-
data.name = err.name ?? "Error";
|
|
150
|
-
}
|
|
151
|
-
const cause = e.cause;
|
|
152
|
-
if (cause !== void 0) {
|
|
153
|
-
if (cause instanceof Error || typeof cause === "object" && cause !== null) {
|
|
154
|
-
data.cause = this.unpackError(cause);
|
|
155
|
-
} else {
|
|
156
|
-
data.cause = cause;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
const msg = String(e.translationKey ?? e.message ?? e.msg ?? "");
|
|
160
|
-
return { msg, data };
|
|
161
|
-
}
|
|
162
|
-
print(dest, fn, level) {
|
|
163
|
-
const effectiveLevelNum = this.levelMap[resolveLevel(this.scope, getGlobalDirectives(), this.level)];
|
|
164
|
-
if (this.levelMap[level] < effectiveLevelNum) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
168
|
-
let { data, msg, err } = fn();
|
|
169
|
-
let errMsg;
|
|
170
|
-
let errData;
|
|
171
|
-
if (err) {
|
|
172
|
-
if (err instanceof Error || typeof err === "object") {
|
|
173
|
-
const result = this.unpackError(err);
|
|
174
|
-
errMsg = result.msg;
|
|
175
|
-
errData = result.data;
|
|
176
|
-
} else {
|
|
177
|
-
errMsg = String(err);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
if (this.defaultData) {
|
|
181
|
-
data = { ...this.defaultData, ...data };
|
|
182
|
-
}
|
|
183
|
-
const baseRecord = { scope: this.scope, ts, level };
|
|
184
|
-
if (data) {
|
|
185
|
-
baseRecord.data = data;
|
|
186
|
-
}
|
|
187
|
-
if (msg) {
|
|
188
|
-
baseRecord.msg = msg;
|
|
189
|
-
}
|
|
190
|
-
if (errMsg) {
|
|
191
|
-
baseRecord.err = errMsg;
|
|
192
|
-
}
|
|
193
|
-
if (errData) {
|
|
194
|
-
baseRecord.errData = errData;
|
|
195
|
-
}
|
|
196
|
-
if (inBrowser) {
|
|
197
|
-
if (msg || errMsg) {
|
|
198
|
-
dest(msg || errMsg, baseRecord);
|
|
199
|
-
} else {
|
|
200
|
-
dest(baseRecord);
|
|
201
|
-
}
|
|
202
|
-
} else {
|
|
203
|
-
const logRecord = util.stringifyBigInts(baseRecord);
|
|
204
|
-
const output = JSON.stringify(logRecord, null, this.pretty);
|
|
205
|
-
dest(output);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
info(fn) {
|
|
209
|
-
this.print(console.info.bind(console), fn, InfoLevel);
|
|
210
|
-
}
|
|
211
|
-
debug(fn) {
|
|
212
|
-
this.print(console.debug.bind(console), fn, DebugLevel);
|
|
213
|
-
}
|
|
214
|
-
trace(fn) {
|
|
215
|
-
this.print(console.trace.bind(console), fn, TraceLevel);
|
|
216
|
-
}
|
|
217
|
-
warn(fn) {
|
|
218
|
-
this.print(console.warn.bind(console), fn, WarnLevel);
|
|
219
|
-
}
|
|
220
|
-
error(fn) {
|
|
221
|
-
this.print(console.error.bind(console), fn, ErrorLevel);
|
|
222
|
-
}
|
|
223
|
-
fatal(fn) {
|
|
224
|
-
this.print(console.error.bind(console), fn, FatalLevel);
|
|
225
|
-
}
|
|
226
|
-
log(level, fn) {
|
|
227
|
-
switch (level) {
|
|
228
|
-
case "trace":
|
|
229
|
-
this.trace(fn);
|
|
230
|
-
break;
|
|
231
|
-
case "debug":
|
|
232
|
-
this.debug(fn);
|
|
233
|
-
break;
|
|
234
|
-
case "info":
|
|
235
|
-
this.info(fn);
|
|
236
|
-
break;
|
|
237
|
-
case "warn":
|
|
238
|
-
this.warn(fn);
|
|
239
|
-
break;
|
|
240
|
-
case "error":
|
|
241
|
-
this.error(fn);
|
|
242
|
-
break;
|
|
243
|
-
case "fatal":
|
|
244
|
-
this.fatal(fn);
|
|
245
|
-
break;
|
|
246
|
-
default:
|
|
247
|
-
throw new Error(`Unknown log level: ${level}`);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
87
|
+
const logger = new NativeLogger(scope);
|
|
88
|
+
logger.setLogLevel(logLevel);
|
|
89
|
+
return logger;
|
|
250
90
|
}
|
|
91
|
+
var inBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
92
|
+
var FormatJson = "json";
|
|
93
|
+
var Format = zod.z.enum([FormatJson, "plain"]);
|
|
94
|
+
/**
|
|
95
|
+
* Native logger which uses console.log, console.error, etc, without any libraries.
|
|
96
|
+
*/
|
|
97
|
+
var NativeLogger = class NativeLogger {
|
|
98
|
+
constructor(scope, levelMap = logLevelMap) {
|
|
99
|
+
this.scope = scope;
|
|
100
|
+
this.levelMap = levelMap;
|
|
101
|
+
this.pretty = 0;
|
|
102
|
+
this.printStack = false;
|
|
103
|
+
this.format = FormatJson;
|
|
104
|
+
this.level = InfoLevel;
|
|
105
|
+
}
|
|
106
|
+
setFormat(format) {
|
|
107
|
+
this.format = format;
|
|
108
|
+
if (this.format !== FormatJson) throw new Error("Only JSON format implemented for now");
|
|
109
|
+
}
|
|
110
|
+
getFormat() {
|
|
111
|
+
return this.format;
|
|
112
|
+
}
|
|
113
|
+
with(obj, subscope) {
|
|
114
|
+
const trimmedSubscope = subscope?.trim();
|
|
115
|
+
const newScope = trimmedSubscope ? this.scope ? `${this.scope}:${trimmedSubscope}` : trimmedSubscope : this.scope;
|
|
116
|
+
const newLogger = new NativeLogger(newScope, this.levelMap);
|
|
117
|
+
if (Object.keys(obj).length > 0) newLogger.defaultData = {
|
|
118
|
+
...this.defaultData,
|
|
119
|
+
...obj
|
|
120
|
+
};
|
|
121
|
+
else if (this.defaultData) newLogger.defaultData = this.defaultData;
|
|
122
|
+
newLogger.setPretty(this.getPretty());
|
|
123
|
+
newLogger.setPrintStack(this.getPrintStack());
|
|
124
|
+
newLogger.setLogLevel(this.getLogLevel());
|
|
125
|
+
return newLogger;
|
|
126
|
+
}
|
|
127
|
+
getPrintStack() {
|
|
128
|
+
return this.printStack;
|
|
129
|
+
}
|
|
130
|
+
setPrintStack(printStack) {
|
|
131
|
+
this.printStack = printStack;
|
|
132
|
+
}
|
|
133
|
+
getPretty() {
|
|
134
|
+
return this.pretty > 0;
|
|
135
|
+
}
|
|
136
|
+
setPretty(pretty) {
|
|
137
|
+
this.pretty = pretty ? 2 : 0;
|
|
138
|
+
}
|
|
139
|
+
getScope() {
|
|
140
|
+
return this.scope;
|
|
141
|
+
}
|
|
142
|
+
setLogLevel(level) {
|
|
143
|
+
this.level = level;
|
|
144
|
+
}
|
|
145
|
+
getLogLevel() {
|
|
146
|
+
return this.level;
|
|
147
|
+
}
|
|
148
|
+
unpackError(err) {
|
|
149
|
+
const e = err;
|
|
150
|
+
const data = {};
|
|
151
|
+
for (const key of Object.getOwnPropertyNames(err)) {
|
|
152
|
+
if (!this.printStack && (key === "stack" || key === "stacktrace")) continue;
|
|
153
|
+
data[key] = e[key];
|
|
154
|
+
}
|
|
155
|
+
if (!data.name) data.name = err.name ?? "Error";
|
|
156
|
+
const cause = e.cause;
|
|
157
|
+
if (cause !== void 0) if (cause instanceof Error || typeof cause === "object" && cause !== null) data.cause = this.unpackError(cause);
|
|
158
|
+
else data.cause = cause;
|
|
159
|
+
return {
|
|
160
|
+
msg: String(e.translationKey ?? e.message ?? e.msg ?? ""),
|
|
161
|
+
data
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
print(dest, fn, level) {
|
|
165
|
+
const effectiveLevelNum = this.levelMap[resolveLevel(this.scope, getGlobalDirectives(), this.level)];
|
|
166
|
+
if (this.levelMap[level] < effectiveLevelNum) return;
|
|
167
|
+
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
168
|
+
let { data, msg, err } = fn();
|
|
169
|
+
let errMsg;
|
|
170
|
+
let errData;
|
|
171
|
+
if (err) if (err instanceof Error || typeof err === "object") {
|
|
172
|
+
const result = this.unpackError(err);
|
|
173
|
+
errMsg = result.msg;
|
|
174
|
+
errData = result.data;
|
|
175
|
+
} else errMsg = String(err);
|
|
176
|
+
if (this.defaultData) data = {
|
|
177
|
+
...this.defaultData,
|
|
178
|
+
...data
|
|
179
|
+
};
|
|
180
|
+
const dataMaybeRequestId = data;
|
|
181
|
+
const reqId = dataMaybeRequestId && typeof dataMaybeRequestId.requestId === "string" ? dataMaybeRequestId.requestId : void 0;
|
|
182
|
+
const baseRecord = {
|
|
183
|
+
scope: this.scope,
|
|
184
|
+
ts,
|
|
185
|
+
level
|
|
186
|
+
};
|
|
187
|
+
if (reqId) baseRecord.req_id = reqId;
|
|
188
|
+
if (data) baseRecord.data = data;
|
|
189
|
+
if (msg) baseRecord.msg = msg;
|
|
190
|
+
if (errMsg) baseRecord.err = errMsg;
|
|
191
|
+
if (errData) baseRecord.errData = errData;
|
|
192
|
+
if (inBrowser) if (msg || errMsg) dest(msg || errMsg, baseRecord);
|
|
193
|
+
else dest(baseRecord);
|
|
194
|
+
else {
|
|
195
|
+
const logRecord = (0, _prosopo_util.stringifyBigInts)(baseRecord);
|
|
196
|
+
dest(JSON.stringify(logRecord, null, this.pretty));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
info(fn) {
|
|
200
|
+
this.print(console.info.bind(console), fn, InfoLevel);
|
|
201
|
+
}
|
|
202
|
+
debug(fn) {
|
|
203
|
+
this.print(console.debug.bind(console), fn, DebugLevel);
|
|
204
|
+
}
|
|
205
|
+
trace(fn) {
|
|
206
|
+
this.print(console.trace.bind(console), fn, TraceLevel);
|
|
207
|
+
}
|
|
208
|
+
warn(fn) {
|
|
209
|
+
this.print(console.warn.bind(console), fn, WarnLevel);
|
|
210
|
+
}
|
|
211
|
+
error(fn) {
|
|
212
|
+
this.print(console.error.bind(console), fn, ErrorLevel);
|
|
213
|
+
}
|
|
214
|
+
fatal(fn) {
|
|
215
|
+
this.print(console.error.bind(console), fn, FatalLevel);
|
|
216
|
+
}
|
|
217
|
+
log(level, fn) {
|
|
218
|
+
switch (level) {
|
|
219
|
+
case "trace":
|
|
220
|
+
this.trace(fn);
|
|
221
|
+
break;
|
|
222
|
+
case "debug":
|
|
223
|
+
this.debug(fn);
|
|
224
|
+
break;
|
|
225
|
+
case "info":
|
|
226
|
+
this.info(fn);
|
|
227
|
+
break;
|
|
228
|
+
case "warn":
|
|
229
|
+
this.warn(fn);
|
|
230
|
+
break;
|
|
231
|
+
case "error":
|
|
232
|
+
this.error(fn);
|
|
233
|
+
break;
|
|
234
|
+
case "fatal":
|
|
235
|
+
this.fatal(fn);
|
|
236
|
+
break;
|
|
237
|
+
default: throw new Error(`Unknown log level: ${level}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
//#endregion
|
|
251
242
|
exports.Format = Format;
|
|
252
243
|
exports.LogLevel = LogLevel;
|
|
253
244
|
exports.NativeLogger = NativeLogger;
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
+
import "./_virtual/_rolldown/runtime.js";
|
|
1
2
|
import { Format, LogLevel, NativeLogger, getLogger, parseDirectives, parseLogLevel, resolveLevel, setGlobalDirectives } from "./logger.js";
|
|
2
|
-
export {
|
|
3
|
-
Format,
|
|
4
|
-
LogLevel,
|
|
5
|
-
NativeLogger,
|
|
6
|
-
getLogger,
|
|
7
|
-
parseDirectives,
|
|
8
|
-
parseLogLevel,
|
|
9
|
-
resolveLevel,
|
|
10
|
-
setGlobalDirectives
|
|
11
|
-
};
|
|
3
|
+
export { Format, LogLevel, NativeLogger, getLogger, parseDirectives, parseLogLevel, resolveLevel, setGlobalDirectives };
|
package/dist/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC;AAC/B,MAAM,MAAM,SAAS,GAAG;IACvB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AACF,MAAM,MAAM,WAAW,GAAG,MAAM,SAAS,CAAC;AAE1C,MAAM,MAAM,MAAM,GAAG;IACpB,WAAW,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI,CAAC;IACnC,WAAW,IAAI,QAAQ,CAAC;IACxB,QAAQ,IAAI,MAAM,CAAC;IAC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