@prosopo/logger 2.0.3 → 2.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/.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 +14 -0
- package/dist/_virtual/_rolldown/runtime.js +3 -0
- package/dist/cjs/index.cjs +9 -10
- package/dist/cjs/logger.cjs +227 -241
- package/dist/index.js +2 -10
- package/dist/logger.js +225 -246
- package/dist/tests/loggerBrowser.unit.test.d.ts +2 -0
- package/dist/tests/loggerBrowser.unit.test.d.ts.map +1 -0
- package/dist/tests/loggerBrowser.unit.test.js +34 -0
- package/dist/tests/loggerBrowser.unit.test.js.map +1 -0
- package/dist/tests/loggerLevels.unit.test.d.ts +2 -0
- package/dist/tests/loggerLevels.unit.test.d.ts.map +1 -0
- package/dist/tests/loggerLevels.unit.test.js +132 -0
- package/dist/tests/loggerLevels.unit.test.js.map +1 -0
- package/package.json +7 -6
- package/src/tests/loggerBrowser.unit.test.ts +85 -0
- package/src/tests/loggerLevels.unit.test.ts +215 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @prosopo/logger@2.0.
|
|
2
|
+
> @prosopo/logger@2.0.5 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 57ms[39m
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
> @prosopo/logger@2.0.
|
|
2
|
+
> @prosopo/logger@2.0.5 build:tsc
|
|
3
3
|
> tsc --build --verbose
|
|
4
4
|
|
|
5
|
-
1:
|
|
5
|
+
1:56:44 PM - Projects in this build:
|
|
6
6
|
* ../../dev/config/tsconfig.json
|
|
7
7
|
* ../util/tsconfig.json
|
|
8
8
|
* tsconfig.json
|
|
9
9
|
|
|
10
|
-
1:
|
|
10
|
+
1:56:44 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
|
-
1:
|
|
12
|
+
1:56:44 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
|
-
1:
|
|
14
|
+
1:56:44 PM - Project 'tsconfig.json' is out of date because output file 'tsconfig.tsbuildinfo' does not exist
|
|
15
15
|
|
|
16
|
-
1:
|
|
16
|
+
1:56:44 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.5 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.5 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 59ms[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @prosopo/logger
|
|
2
2
|
|
|
3
|
+
## 2.0.5
|
|
4
|
+
### Patch Changes
|
|
5
|
+
|
|
6
|
+
- 2aabe73: Remove the client-controlled `detectorUnavailable` frictionless bypass. A client could set the flag and be handed a PoW challenge without any detection running. The flag is gone from the wire format, the API client and the widget; the only remaining bypasses are provider-side (maintenance mode, empty detector bundle pool).
|
|
7
|
+
|
|
8
|
+
The frictionless decision machine now gates on payload presence after the access-rule ladder: no token serves a 3-round image captcha, a token without its head hash serves a 2-round one.
|
|
9
|
+
|
|
10
|
+
## 2.0.4
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [2c47bb7]
|
|
14
|
+
- Updated dependencies [0e1171c]
|
|
15
|
+
- @prosopo/util@3.3.5
|
|
16
|
+
|
|
3
17
|
## 2.0.3
|
|
4
18
|
### Patch Changes
|
|
5
19
|
|
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,258 +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 dataMaybeRequestId = data;
|
|
184
|
-
const reqId = dataMaybeRequestId && typeof dataMaybeRequestId.requestId === "string" ? dataMaybeRequestId.requestId : void 0;
|
|
185
|
-
const baseRecord = { scope: this.scope, ts, level };
|
|
186
|
-
if (reqId) {
|
|
187
|
-
baseRecord.req_id = reqId;
|
|
188
|
-
}
|
|
189
|
-
if (data) {
|
|
190
|
-
baseRecord.data = data;
|
|
191
|
-
}
|
|
192
|
-
if (msg) {
|
|
193
|
-
baseRecord.msg = msg;
|
|
194
|
-
}
|
|
195
|
-
if (errMsg) {
|
|
196
|
-
baseRecord.err = errMsg;
|
|
197
|
-
}
|
|
198
|
-
if (errData) {
|
|
199
|
-
baseRecord.errData = errData;
|
|
200
|
-
}
|
|
201
|
-
if (inBrowser) {
|
|
202
|
-
if (msg || errMsg) {
|
|
203
|
-
dest(msg || errMsg, baseRecord);
|
|
204
|
-
} else {
|
|
205
|
-
dest(baseRecord);
|
|
206
|
-
}
|
|
207
|
-
} else {
|
|
208
|
-
const logRecord = util.stringifyBigInts(baseRecord);
|
|
209
|
-
const output = JSON.stringify(logRecord, null, this.pretty);
|
|
210
|
-
dest(output);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
info(fn) {
|
|
214
|
-
this.print(console.info.bind(console), fn, InfoLevel);
|
|
215
|
-
}
|
|
216
|
-
debug(fn) {
|
|
217
|
-
this.print(console.debug.bind(console), fn, DebugLevel);
|
|
218
|
-
}
|
|
219
|
-
trace(fn) {
|
|
220
|
-
this.print(console.trace.bind(console), fn, TraceLevel);
|
|
221
|
-
}
|
|
222
|
-
warn(fn) {
|
|
223
|
-
this.print(console.warn.bind(console), fn, WarnLevel);
|
|
224
|
-
}
|
|
225
|
-
error(fn) {
|
|
226
|
-
this.print(console.error.bind(console), fn, ErrorLevel);
|
|
227
|
-
}
|
|
228
|
-
fatal(fn) {
|
|
229
|
-
this.print(console.error.bind(console), fn, FatalLevel);
|
|
230
|
-
}
|
|
231
|
-
log(level, fn) {
|
|
232
|
-
switch (level) {
|
|
233
|
-
case "trace":
|
|
234
|
-
this.trace(fn);
|
|
235
|
-
break;
|
|
236
|
-
case "debug":
|
|
237
|
-
this.debug(fn);
|
|
238
|
-
break;
|
|
239
|
-
case "info":
|
|
240
|
-
this.info(fn);
|
|
241
|
-
break;
|
|
242
|
-
case "warn":
|
|
243
|
-
this.warn(fn);
|
|
244
|
-
break;
|
|
245
|
-
case "error":
|
|
246
|
-
this.error(fn);
|
|
247
|
-
break;
|
|
248
|
-
case "fatal":
|
|
249
|
-
this.fatal(fn);
|
|
250
|
-
break;
|
|
251
|
-
default:
|
|
252
|
-
throw new Error(`Unknown log level: ${level}`);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
87
|
+
const logger = new NativeLogger(scope);
|
|
88
|
+
logger.setLogLevel(logLevel);
|
|
89
|
+
return logger;
|
|
255
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
|
|
256
242
|
exports.Format = Format;
|
|
257
243
|
exports.LogLevel = LogLevel;
|
|
258
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 };
|