@prosopo/logger 1.0.2 → 1.0.3
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 +18 -0
- package/.turbo/turbo-build$colon$tsc.log +17 -0
- package/.turbo/turbo-build.log +22 -20
- package/CHANGELOG.md +6 -0
- package/dist/cjs/index.cjs +21 -0
- package/dist/cjs/logger.cjs +213 -0
- package/dist/index.js +18 -2
- package/dist/logger.js +202 -192
- package/package.json +2 -2
- package/src/index.ts +15 -0
- package/src/logger.ts +325 -0
- package/src/tests/logger.unit.test.ts +134 -0
- package/tsconfig.cjs.json +21 -0
- package/tsconfig.json +23 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsconfig.types.json +9 -0
package/dist/logger.js
CHANGED
|
@@ -1,201 +1,211 @@
|
|
|
1
1
|
import { stringifyBigInts } from "@prosopo/util";
|
|
2
|
+
import { stringifyBigInts as stringifyBigInts2 } from "@prosopo/util";
|
|
2
3
|
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
FatalLevel,
|
|
4
|
+
const InfoLevel = "info";
|
|
5
|
+
const DebugLevel = "debug";
|
|
6
|
+
const TraceLevel = "trace";
|
|
7
|
+
const WarnLevel = "warn";
|
|
8
|
+
const ErrorLevel = "error";
|
|
9
|
+
const FatalLevel = "fatal";
|
|
10
|
+
const LogLevel = z.enum([
|
|
11
|
+
InfoLevel,
|
|
12
|
+
DebugLevel,
|
|
13
|
+
TraceLevel,
|
|
14
|
+
WarnLevel,
|
|
15
|
+
ErrorLevel,
|
|
16
|
+
FatalLevel
|
|
17
17
|
]);
|
|
18
18
|
const logLevelMap = {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
[TraceLevel]: 0,
|
|
20
|
+
[DebugLevel]: 1,
|
|
21
|
+
[InfoLevel]: 2,
|
|
22
|
+
[WarnLevel]: 3,
|
|
23
|
+
[ErrorLevel]: 4,
|
|
24
|
+
[FatalLevel]: 5
|
|
25
25
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
function parseLogLevel(level, or = InfoLevel) {
|
|
27
|
+
const result = LogLevel.safeParse(level);
|
|
28
|
+
return result.success ? result.data : or;
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
function getLogger(logLevel, scope) {
|
|
31
|
+
const logger = new NativeLogger(scope);
|
|
32
|
+
logger.setLogLevel(logLevel);
|
|
33
|
+
return logger;
|
|
34
34
|
}
|
|
35
35
|
const inBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
this.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
break;
|
|
196
|
-
default:
|
|
197
|
-
throw new Error(`Unknown log level: ${level}`);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
36
|
+
const FormatJson = "json";
|
|
37
|
+
const FormatPlain = "plain";
|
|
38
|
+
const Format = z.enum([FormatJson, FormatPlain]);
|
|
39
|
+
class NativeLogger {
|
|
40
|
+
constructor(scope, levelMap = logLevelMap) {
|
|
41
|
+
this.scope = scope;
|
|
42
|
+
this.levelMap = levelMap;
|
|
43
|
+
this.pretty = 0;
|
|
44
|
+
this.printStack = false;
|
|
45
|
+
this.format = FormatJson;
|
|
46
|
+
this.level = InfoLevel;
|
|
47
|
+
this.levelNum = this.levelMap[this.level];
|
|
48
|
+
}
|
|
49
|
+
setFormat(format) {
|
|
50
|
+
this.format = format;
|
|
51
|
+
if (this.format !== FormatJson) {
|
|
52
|
+
throw new Error("Only JSON format implemented for now");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
getFormat() {
|
|
56
|
+
return this.format;
|
|
57
|
+
}
|
|
58
|
+
with(obj) {
|
|
59
|
+
const newLogger = new NativeLogger(this.scope);
|
|
60
|
+
newLogger.defaultData = { ...this.defaultData, ...obj };
|
|
61
|
+
newLogger.setLogLevel(this.getLogLevel());
|
|
62
|
+
return newLogger;
|
|
63
|
+
}
|
|
64
|
+
getPrintStack() {
|
|
65
|
+
return this.printStack;
|
|
66
|
+
}
|
|
67
|
+
setPrintStack(printStack) {
|
|
68
|
+
this.printStack = printStack;
|
|
69
|
+
}
|
|
70
|
+
getPretty() {
|
|
71
|
+
return this.pretty > 0;
|
|
72
|
+
}
|
|
73
|
+
setPretty(pretty) {
|
|
74
|
+
this.pretty = pretty ? 2 : 0;
|
|
75
|
+
}
|
|
76
|
+
getScope() {
|
|
77
|
+
return this.scope;
|
|
78
|
+
}
|
|
79
|
+
setLogLevel(level) {
|
|
80
|
+
this.level = level;
|
|
81
|
+
this.levelNum = this.levelMap[level];
|
|
82
|
+
}
|
|
83
|
+
getLogLevel() {
|
|
84
|
+
return this.level;
|
|
85
|
+
}
|
|
86
|
+
unpackError(err) {
|
|
87
|
+
const e = err;
|
|
88
|
+
const data = {};
|
|
89
|
+
for (const key of Object.getOwnPropertyNames(err)) {
|
|
90
|
+
if (!this.printStack && (key === "stack" || key === "stacktrace"))
|
|
91
|
+
continue;
|
|
92
|
+
data[key] = e[key];
|
|
93
|
+
}
|
|
94
|
+
if (!data.name) {
|
|
95
|
+
data.name = err.name ?? "Error";
|
|
96
|
+
}
|
|
97
|
+
const cause = e.cause;
|
|
98
|
+
if (cause !== void 0) {
|
|
99
|
+
if (cause instanceof Error || typeof cause === "object" && cause !== null) {
|
|
100
|
+
data.cause = this.unpackError(cause);
|
|
101
|
+
} else {
|
|
102
|
+
data.cause = cause;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const msg = String(e.translationKey ?? e.message ?? e.msg ?? "");
|
|
106
|
+
return { msg, data };
|
|
107
|
+
}
|
|
108
|
+
print(dest, fn, level) {
|
|
109
|
+
if (this.levelMap[level] < this.levelNum) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const ts = (/* @__PURE__ */ new Date()).toISOString();
|
|
113
|
+
let { data, msg, err } = fn();
|
|
114
|
+
let errMsg;
|
|
115
|
+
let errData;
|
|
116
|
+
if (err) {
|
|
117
|
+
if (err instanceof Error || typeof err === "object") {
|
|
118
|
+
const result = this.unpackError(err);
|
|
119
|
+
errMsg = result.msg;
|
|
120
|
+
errData = result.data;
|
|
121
|
+
} else {
|
|
122
|
+
errMsg = String(err);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (this.defaultData) {
|
|
126
|
+
data = { ...this.defaultData, ...data };
|
|
127
|
+
}
|
|
128
|
+
const baseRecord = { scope: this.scope, ts, level };
|
|
129
|
+
if (data) {
|
|
130
|
+
baseRecord.data = data;
|
|
131
|
+
}
|
|
132
|
+
if (msg) {
|
|
133
|
+
baseRecord.msg = msg;
|
|
134
|
+
}
|
|
135
|
+
if (errMsg) {
|
|
136
|
+
baseRecord.err = errMsg;
|
|
137
|
+
}
|
|
138
|
+
if (errData) {
|
|
139
|
+
baseRecord.errData = errData;
|
|
140
|
+
}
|
|
141
|
+
if (inBrowser) {
|
|
142
|
+
if (msg || errMsg) {
|
|
143
|
+
dest(msg || errMsg, baseRecord);
|
|
144
|
+
} else {
|
|
145
|
+
dest(baseRecord);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
const logRecord = stringifyBigInts(baseRecord);
|
|
149
|
+
const output = JSON.stringify(logRecord, null, this.pretty);
|
|
150
|
+
dest(output);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
info(fn) {
|
|
154
|
+
this.print(console.info.bind(console), fn, InfoLevel);
|
|
155
|
+
}
|
|
156
|
+
debug(fn) {
|
|
157
|
+
this.print(console.debug.bind(console), fn, DebugLevel);
|
|
158
|
+
}
|
|
159
|
+
trace(fn) {
|
|
160
|
+
this.print(console.trace.bind(console), fn, TraceLevel);
|
|
161
|
+
}
|
|
162
|
+
warn(fn) {
|
|
163
|
+
this.print(console.warn.bind(console), fn, WarnLevel);
|
|
164
|
+
}
|
|
165
|
+
error(fn) {
|
|
166
|
+
this.print(console.error.bind(console), fn, ErrorLevel);
|
|
167
|
+
}
|
|
168
|
+
fatal(fn) {
|
|
169
|
+
this.print(console.error.bind(console), fn, FatalLevel);
|
|
170
|
+
}
|
|
171
|
+
log(level, fn) {
|
|
172
|
+
switch (level) {
|
|
173
|
+
case "trace":
|
|
174
|
+
this.trace(fn);
|
|
175
|
+
break;
|
|
176
|
+
case "debug":
|
|
177
|
+
this.debug(fn);
|
|
178
|
+
break;
|
|
179
|
+
case "info":
|
|
180
|
+
this.info(fn);
|
|
181
|
+
break;
|
|
182
|
+
case "warn":
|
|
183
|
+
this.warn(fn);
|
|
184
|
+
break;
|
|
185
|
+
case "error":
|
|
186
|
+
this.error(fn);
|
|
187
|
+
break;
|
|
188
|
+
case "fatal":
|
|
189
|
+
this.fatal(fn);
|
|
190
|
+
break;
|
|
191
|
+
default:
|
|
192
|
+
throw new Error(`Unknown log level: ${level}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
200
195
|
}
|
|
201
|
-
|
|
196
|
+
export {
|
|
197
|
+
DebugLevel,
|
|
198
|
+
ErrorLevel,
|
|
199
|
+
FatalLevel,
|
|
200
|
+
Format,
|
|
201
|
+
FormatJson,
|
|
202
|
+
FormatPlain,
|
|
203
|
+
InfoLevel,
|
|
204
|
+
LogLevel,
|
|
205
|
+
NativeLogger,
|
|
206
|
+
TraceLevel,
|
|
207
|
+
WarnLevel,
|
|
208
|
+
getLogger,
|
|
209
|
+
parseLogLevel,
|
|
210
|
+
stringifyBigInts2 as stringifyBigInts
|
|
211
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosopo/logger",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Prosopo logger library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"author": "Prosopo Limited",
|
|
29
29
|
"license": "Apache-2.0",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@prosopo/util": "3.
|
|
31
|
+
"@prosopo/util": "3.3.0",
|
|
32
32
|
"zod": "3.23.8"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
export * from "./logger.js";
|