@jaypie/logger 1.1.0
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/dist/cjs/JaypieLogger.d.ts +32 -0
- package/dist/cjs/Logger.d.ts +30 -0
- package/dist/cjs/__tests__/index.spec.d.ts +1 -0
- package/dist/cjs/constants.d.ts +29 -0
- package/dist/cjs/forceVar.d.ts +1 -0
- package/dist/cjs/index.cjs +606 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/logTags.d.ts +1 -0
- package/dist/cjs/logVar.d.ts +1 -0
- package/dist/cjs/pipelines.d.ts +8 -0
- package/dist/cjs/utils.d.ts +11 -0
- package/dist/esm/JaypieLogger.d.ts +32 -0
- package/dist/esm/Logger.d.ts +30 -0
- package/dist/esm/__tests__/index.spec.d.ts +1 -0
- package/dist/esm/constants.d.ts +29 -0
- package/dist/esm/forceVar.d.ts +1 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +597 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/logTags.d.ts +1 -0
- package/dist/esm/logVar.d.ts +1 -0
- package/dist/esm/pipelines.d.ts +8 -0
- package/dist/esm/utils.d.ts +11 -0
- package/package.json +44 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import Logger from "./Logger";
|
|
2
|
+
interface JaypieLoggerOptions {
|
|
3
|
+
level?: string;
|
|
4
|
+
tags?: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
declare class JaypieLogger {
|
|
7
|
+
debug: Logger["debug"];
|
|
8
|
+
error: Logger["error"];
|
|
9
|
+
fatal: Logger["fatal"];
|
|
10
|
+
info: Logger["info"];
|
|
11
|
+
level: string;
|
|
12
|
+
trace: Logger["trace"];
|
|
13
|
+
var: Logger["var"];
|
|
14
|
+
warn: Logger["warn"];
|
|
15
|
+
private _logger;
|
|
16
|
+
private _loggers;
|
|
17
|
+
private _params;
|
|
18
|
+
private _tags;
|
|
19
|
+
private _withLoggers;
|
|
20
|
+
constructor({ level, tags, }?: JaypieLoggerOptions);
|
|
21
|
+
init(): void;
|
|
22
|
+
lib({ level, lib, tags, }?: {
|
|
23
|
+
level?: string;
|
|
24
|
+
lib?: string;
|
|
25
|
+
tags?: Record<string, string>;
|
|
26
|
+
}): JaypieLogger;
|
|
27
|
+
tag(tags: Record<string, unknown>): void;
|
|
28
|
+
untag(...args: any[]): void;
|
|
29
|
+
with(...args: any[]): JaypieLogger;
|
|
30
|
+
}
|
|
31
|
+
export declare function createLogger(tags?: Record<string, string>): JaypieLogger;
|
|
32
|
+
export default JaypieLogger;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type LogLevel = string;
|
|
2
|
+
type LogFormat = "json" | "text";
|
|
3
|
+
type Tags = Record<string, string>;
|
|
4
|
+
interface LoggerOptions {
|
|
5
|
+
format?: LogFormat;
|
|
6
|
+
level?: LogLevel;
|
|
7
|
+
tags?: Tags;
|
|
8
|
+
varLevel?: LogLevel;
|
|
9
|
+
}
|
|
10
|
+
type LogMethod = {
|
|
11
|
+
(...messages: unknown[]): void;
|
|
12
|
+
var: (messageObject: unknown, messageValue?: unknown) => void;
|
|
13
|
+
};
|
|
14
|
+
declare class Logger {
|
|
15
|
+
debug: LogMethod;
|
|
16
|
+
error: LogMethod;
|
|
17
|
+
fatal: LogMethod;
|
|
18
|
+
info: LogMethod;
|
|
19
|
+
options: LoggerOptions;
|
|
20
|
+
tags: Tags;
|
|
21
|
+
trace: LogMethod;
|
|
22
|
+
var: (messageObject: unknown, messageValue?: unknown) => void;
|
|
23
|
+
warn: LogMethod;
|
|
24
|
+
constructor({ format, level, tags, varLevel, }?: LoggerOptions);
|
|
25
|
+
private createLogMethod;
|
|
26
|
+
tag(key: unknown, value?: unknown): void;
|
|
27
|
+
untag(key: unknown): void;
|
|
28
|
+
with(key: unknown, value?: unknown): Logger;
|
|
29
|
+
}
|
|
30
|
+
export default Logger;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare const DEFAULT: {
|
|
2
|
+
LEVEL: string;
|
|
3
|
+
VAR_LEVEL: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const ERROR_PREFIX = "[logger]";
|
|
6
|
+
export declare const ERROR: {
|
|
7
|
+
VAR: {
|
|
8
|
+
EMPTY_OBJECT: string;
|
|
9
|
+
MULTIPLE_KEYS: string;
|
|
10
|
+
NULL_OBJECT: string;
|
|
11
|
+
UNDEFINED_MESSAGE: string;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export declare const FORMAT: {
|
|
15
|
+
readonly JSON: "json";
|
|
16
|
+
readonly TEXT: "text";
|
|
17
|
+
};
|
|
18
|
+
export declare const LEVEL: {
|
|
19
|
+
readonly ALL: "all";
|
|
20
|
+
readonly DEBUG: "debug";
|
|
21
|
+
readonly ERROR: "error";
|
|
22
|
+
readonly FATAL: "fatal";
|
|
23
|
+
readonly INFO: "info";
|
|
24
|
+
readonly SILENT: "silent";
|
|
25
|
+
readonly TRACE: "trace";
|
|
26
|
+
readonly WARN: "warn";
|
|
27
|
+
};
|
|
28
|
+
export declare const LEVEL_VALUES: Record<string, number>;
|
|
29
|
+
export declare const PSEUDO_LEVELS: string[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function forceVar(key: unknown, value?: unknown): Record<string, unknown>;
|
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const DEFAULT = {
|
|
6
|
+
LEVEL: "debug",
|
|
7
|
+
VAR_LEVEL: "debug",
|
|
8
|
+
};
|
|
9
|
+
const ERROR_PREFIX = "[logger]";
|
|
10
|
+
const ERROR = {
|
|
11
|
+
VAR: {
|
|
12
|
+
EMPTY_OBJECT: `${ERROR_PREFIX} Logger.var() called with empty object`,
|
|
13
|
+
MULTIPLE_KEYS: `${ERROR_PREFIX} Logger.var() called with multiple keys`,
|
|
14
|
+
NULL_OBJECT: `${ERROR_PREFIX} Logger.var() called with null`,
|
|
15
|
+
UNDEFINED_MESSAGE: `${ERROR_PREFIX} Logger.var() called with \`undefined\` message`,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
const FORMAT = {
|
|
19
|
+
JSON: "json",
|
|
20
|
+
TEXT: "text",
|
|
21
|
+
};
|
|
22
|
+
const LEVEL = {
|
|
23
|
+
ALL: "all",
|
|
24
|
+
DEBUG: "debug",
|
|
25
|
+
ERROR: "error",
|
|
26
|
+
FATAL: "fatal",
|
|
27
|
+
INFO: "info",
|
|
28
|
+
SILENT: "silent",
|
|
29
|
+
TRACE: "trace",
|
|
30
|
+
WARN: "warn",
|
|
31
|
+
};
|
|
32
|
+
const LEVEL_VALUES = {
|
|
33
|
+
all: 100,
|
|
34
|
+
debug: 70,
|
|
35
|
+
error: 10,
|
|
36
|
+
fatal: 1,
|
|
37
|
+
info: 50,
|
|
38
|
+
none: 0,
|
|
39
|
+
silent: 0,
|
|
40
|
+
trace: 90,
|
|
41
|
+
warn: 30,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function forceString(value, defaultValue = "") {
|
|
45
|
+
if (value === null)
|
|
46
|
+
return "null";
|
|
47
|
+
if (value === undefined)
|
|
48
|
+
return String(defaultValue);
|
|
49
|
+
if (typeof value === "object")
|
|
50
|
+
return JSON.stringify(value);
|
|
51
|
+
return String(value);
|
|
52
|
+
}
|
|
53
|
+
function formatAsJsonString(subject) {
|
|
54
|
+
const SPACES = 0;
|
|
55
|
+
const UNUSED_PARAM = null;
|
|
56
|
+
switch (typeof subject) {
|
|
57
|
+
case "string":
|
|
58
|
+
if (subject === "")
|
|
59
|
+
return `""`;
|
|
60
|
+
try {
|
|
61
|
+
return JSON.stringify(JSON.parse(subject), UNUSED_PARAM, SPACES);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return subject;
|
|
65
|
+
}
|
|
66
|
+
case "object":
|
|
67
|
+
try {
|
|
68
|
+
if (subject &&
|
|
69
|
+
subject instanceof Object &&
|
|
70
|
+
!Array.isArray(subject) &&
|
|
71
|
+
subject.constructor &&
|
|
72
|
+
subject.constructor !== Object &&
|
|
73
|
+
"toString" in subject &&
|
|
74
|
+
typeof subject.toString === "function") {
|
|
75
|
+
return subject.toString();
|
|
76
|
+
}
|
|
77
|
+
return JSON.stringify(subject, UNUSED_PARAM, SPACES);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (error instanceof TypeError) {
|
|
81
|
+
const truncatedSubject = Object.keys(subject).reduce((newSubject, key) => {
|
|
82
|
+
const nextSubject = { ...newSubject };
|
|
83
|
+
nextSubject[key] = String(subject[key]);
|
|
84
|
+
return nextSubject;
|
|
85
|
+
}, {});
|
|
86
|
+
return formatAsJsonString(truncatedSubject);
|
|
87
|
+
}
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
default:
|
|
91
|
+
return String(subject);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function stringify(...params) {
|
|
95
|
+
if (params.length === 0)
|
|
96
|
+
return "";
|
|
97
|
+
if (params.length === 1) {
|
|
98
|
+
return formatAsJsonString(params[0]);
|
|
99
|
+
}
|
|
100
|
+
const noObjects = params.reduce((previous, current) => {
|
|
101
|
+
if (typeof current === "object" && current !== null) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return previous;
|
|
105
|
+
}, true);
|
|
106
|
+
if (noObjects) {
|
|
107
|
+
const formatted = params.map(formatAsJsonString);
|
|
108
|
+
return formatted.join(" ");
|
|
109
|
+
}
|
|
110
|
+
return formatAsJsonString(params);
|
|
111
|
+
}
|
|
112
|
+
function out(line, { level = "debug" } = {}) {
|
|
113
|
+
let lineStr;
|
|
114
|
+
if (typeof line !== "string") {
|
|
115
|
+
lineStr = stringify(line);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
lineStr = line;
|
|
119
|
+
}
|
|
120
|
+
let outputFunction;
|
|
121
|
+
switch (level) {
|
|
122
|
+
case LEVEL.INFO:
|
|
123
|
+
outputFunction = console.info;
|
|
124
|
+
break;
|
|
125
|
+
case LEVEL.WARN:
|
|
126
|
+
outputFunction = console.warn;
|
|
127
|
+
break;
|
|
128
|
+
case LEVEL.ERROR:
|
|
129
|
+
case LEVEL.FATAL:
|
|
130
|
+
outputFunction = console.error;
|
|
131
|
+
break;
|
|
132
|
+
case LEVEL.TRACE:
|
|
133
|
+
case LEVEL.DEBUG:
|
|
134
|
+
outputFunction = console.debug;
|
|
135
|
+
break;
|
|
136
|
+
default:
|
|
137
|
+
outputFunction = console.log;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
outputFunction(lineStr);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.warn(error);
|
|
145
|
+
console.log(lineStr);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function parse(message) {
|
|
149
|
+
if (typeof message !== "string") {
|
|
150
|
+
return message;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
return JSON.parse(message);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return message;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function parsesTo(message) {
|
|
160
|
+
if (typeof message !== "string") {
|
|
161
|
+
return {
|
|
162
|
+
parses: false,
|
|
163
|
+
message,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
try {
|
|
167
|
+
return {
|
|
168
|
+
parses: true,
|
|
169
|
+
message: JSON.parse(message),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
return {
|
|
174
|
+
parses: false,
|
|
175
|
+
message,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
class Logger {
|
|
181
|
+
constructor({ format = process.env.LOG_FORMAT || DEFAULT.LEVEL, level = process.env.LOG_LEVEL || DEFAULT.LEVEL, tags = {}, varLevel = process.env.LOG_VAR_LEVEL || DEFAULT.VAR_LEVEL, } = {}) {
|
|
182
|
+
this.options = {
|
|
183
|
+
format,
|
|
184
|
+
level,
|
|
185
|
+
varLevel,
|
|
186
|
+
};
|
|
187
|
+
this.tags = {};
|
|
188
|
+
Object.keys(tags).forEach((key) => {
|
|
189
|
+
this.tags[key] = forceString(tags[key]);
|
|
190
|
+
});
|
|
191
|
+
this.debug = this.createLogMethod(LEVEL.DEBUG, format, level);
|
|
192
|
+
this.error = this.createLogMethod(LEVEL.ERROR, format, level);
|
|
193
|
+
this.fatal = this.createLogMethod(LEVEL.FATAL, format, level);
|
|
194
|
+
this.info = this.createLogMethod(LEVEL.INFO, format, level);
|
|
195
|
+
this.trace = this.createLogMethod(LEVEL.TRACE, format, level);
|
|
196
|
+
this.warn = this.createLogMethod(LEVEL.WARN, format, level);
|
|
197
|
+
const varLevelMethod = this[varLevel];
|
|
198
|
+
this.var = varLevelMethod?.var || this.debug.var;
|
|
199
|
+
}
|
|
200
|
+
createLogMethod(logLevel, format, checkLevel) {
|
|
201
|
+
const logFn = (...messages) => {
|
|
202
|
+
if (LEVEL_VALUES[logLevel] <= LEVEL_VALUES[checkLevel]) {
|
|
203
|
+
if (format === FORMAT.JSON) {
|
|
204
|
+
const message = stringify(...messages);
|
|
205
|
+
const parses = parsesTo(message);
|
|
206
|
+
const json = {
|
|
207
|
+
log: logLevel,
|
|
208
|
+
message,
|
|
209
|
+
...this.tags,
|
|
210
|
+
};
|
|
211
|
+
if (parses.parses) {
|
|
212
|
+
json.data = parses.message;
|
|
213
|
+
}
|
|
214
|
+
out(json, { level: logLevel });
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
const message = stringify(...messages);
|
|
218
|
+
out(message, { level: logLevel });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
logFn.var = (messageObject, messageValue) => {
|
|
223
|
+
if (messageObject === undefined) {
|
|
224
|
+
this.warn(ERROR.VAR.UNDEFINED_MESSAGE);
|
|
225
|
+
}
|
|
226
|
+
let msgObj;
|
|
227
|
+
if (typeof messageObject !== "object" || messageObject === null) {
|
|
228
|
+
if (typeof messageObject !== "object") {
|
|
229
|
+
if (messageValue === undefined)
|
|
230
|
+
messageValue = "undefined";
|
|
231
|
+
msgObj = { [String(messageObject)]: messageValue };
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
this.warn(ERROR.VAR.NULL_OBJECT);
|
|
235
|
+
return logFn(messageObject);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
msgObj = messageObject;
|
|
240
|
+
}
|
|
241
|
+
const keys = Object.keys(msgObj);
|
|
242
|
+
if (keys.length === 0) {
|
|
243
|
+
this.warn(ERROR.VAR.EMPTY_OBJECT);
|
|
244
|
+
return logFn(msgObj);
|
|
245
|
+
}
|
|
246
|
+
if (keys.length > 1) {
|
|
247
|
+
this.warn(ERROR.VAR.MULTIPLE_KEYS);
|
|
248
|
+
return logFn(msgObj);
|
|
249
|
+
}
|
|
250
|
+
if (format === FORMAT.JSON) {
|
|
251
|
+
const messageKey = keys[0];
|
|
252
|
+
const messageVal = msgObj[messageKey];
|
|
253
|
+
const json = {
|
|
254
|
+
data: parse(messageVal),
|
|
255
|
+
dataType: typeof messageVal,
|
|
256
|
+
log: logLevel,
|
|
257
|
+
message: stringify(messageVal),
|
|
258
|
+
var: messageKey,
|
|
259
|
+
...this.tags,
|
|
260
|
+
};
|
|
261
|
+
if (LEVEL_VALUES[logLevel] <= LEVEL_VALUES[checkLevel]) {
|
|
262
|
+
out(json, { level: logLevel });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
return logFn(msgObj);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
return logFn;
|
|
270
|
+
}
|
|
271
|
+
tag(key, value) {
|
|
272
|
+
if (value) {
|
|
273
|
+
this.tags[forceString(key)] = forceString(value);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (Array.isArray(key)) {
|
|
277
|
+
key.forEach((k) => {
|
|
278
|
+
this.tags[forceString(k)] = "";
|
|
279
|
+
});
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (key === null) {
|
|
283
|
+
this.tags.null = "";
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (typeof key === "object") {
|
|
287
|
+
Object.keys(key).forEach((k) => {
|
|
288
|
+
this.tags[forceString(k)] = forceString(key[k]);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
this.tags[forceString(key)] = "";
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
untag(key) {
|
|
296
|
+
if (Array.isArray(key)) {
|
|
297
|
+
key.forEach((k) => {
|
|
298
|
+
delete this.tags[forceString(k)];
|
|
299
|
+
});
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (key === null) {
|
|
303
|
+
delete this.tags.null;
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (typeof key === "object") {
|
|
307
|
+
Object.keys(key).forEach((k) => {
|
|
308
|
+
delete this.tags[forceString(k)];
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
delete this.tags[forceString(key)];
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
with(key, value) {
|
|
316
|
+
const logger = new Logger(this.options);
|
|
317
|
+
logger.tag(this.tags);
|
|
318
|
+
logger.tag(key, value);
|
|
319
|
+
return logger;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function logTags(withTags) {
|
|
324
|
+
const { PROJECT_COMMIT, PROJECT_ENV, PROJECT_KEY, PROJECT_SERVICE, PROJECT_SPONSOR, PROJECT_VERSION, } = process.env;
|
|
325
|
+
const tags = {};
|
|
326
|
+
if (PROJECT_COMMIT) {
|
|
327
|
+
tags.commit = PROJECT_COMMIT;
|
|
328
|
+
}
|
|
329
|
+
if (PROJECT_ENV) {
|
|
330
|
+
tags.env = PROJECT_ENV;
|
|
331
|
+
}
|
|
332
|
+
if (PROJECT_KEY) {
|
|
333
|
+
tags.project = PROJECT_KEY;
|
|
334
|
+
}
|
|
335
|
+
if (PROJECT_SERVICE) {
|
|
336
|
+
tags.service = PROJECT_SERVICE;
|
|
337
|
+
}
|
|
338
|
+
if (PROJECT_SPONSOR) {
|
|
339
|
+
tags.sponsor = PROJECT_SPONSOR;
|
|
340
|
+
}
|
|
341
|
+
if (process.env.npm_package_version || PROJECT_VERSION) {
|
|
342
|
+
tags.version = (process.env.npm_package_version ||
|
|
343
|
+
PROJECT_VERSION);
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
...tags,
|
|
347
|
+
...withTags,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function forceVar(key, value) {
|
|
352
|
+
if (typeof key === "undefined") {
|
|
353
|
+
return {};
|
|
354
|
+
}
|
|
355
|
+
if (typeof key === "object" && key !== null) {
|
|
356
|
+
if (Object.keys(key).length === 1) {
|
|
357
|
+
return key;
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
return { value: key };
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
const keyStr = forceString(key);
|
|
364
|
+
if (typeof value === "undefined") {
|
|
365
|
+
return { [keyStr]: "" };
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
return { [keyStr]: value };
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function isAxiosResponse(response) {
|
|
373
|
+
if (typeof response !== "object" || response === null) {
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
const r = response;
|
|
377
|
+
return !!(r &&
|
|
378
|
+
r.config &&
|
|
379
|
+
r.data &&
|
|
380
|
+
r.headers &&
|
|
381
|
+
r.request &&
|
|
382
|
+
r.status &&
|
|
383
|
+
r.statusText);
|
|
384
|
+
}
|
|
385
|
+
function filterAxiosResponse(response) {
|
|
386
|
+
if (!isAxiosResponse(response)) {
|
|
387
|
+
return response;
|
|
388
|
+
}
|
|
389
|
+
const r = response;
|
|
390
|
+
const newResponse = {
|
|
391
|
+
data: r.data,
|
|
392
|
+
headers: r.headers,
|
|
393
|
+
status: r.status,
|
|
394
|
+
statusText: r.statusText,
|
|
395
|
+
};
|
|
396
|
+
if (r.isAxiosError) {
|
|
397
|
+
newResponse.isAxiosError = r.isAxiosError;
|
|
398
|
+
}
|
|
399
|
+
return newResponse;
|
|
400
|
+
}
|
|
401
|
+
const axiosResponseVarPipeline = {
|
|
402
|
+
filter: filterAxiosResponse,
|
|
403
|
+
key: "response",
|
|
404
|
+
};
|
|
405
|
+
function isError(item) {
|
|
406
|
+
if (typeof item !== "object" || item === null) {
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
if (item instanceof Error) {
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
const i = item;
|
|
413
|
+
if (i.isProjectError) {
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
return false;
|
|
417
|
+
}
|
|
418
|
+
function filterErrorVar(item) {
|
|
419
|
+
if (!isError(item)) {
|
|
420
|
+
return item;
|
|
421
|
+
}
|
|
422
|
+
const e = item;
|
|
423
|
+
const newItem = {
|
|
424
|
+
message: e.message,
|
|
425
|
+
name: e.name,
|
|
426
|
+
};
|
|
427
|
+
if (e.cause) {
|
|
428
|
+
newItem.cause = e.cause;
|
|
429
|
+
}
|
|
430
|
+
if (e.stack) {
|
|
431
|
+
newItem.stack = e.stack;
|
|
432
|
+
}
|
|
433
|
+
if (e.isProjectError) {
|
|
434
|
+
newItem.isProjectError = e.isProjectError;
|
|
435
|
+
newItem.title = e.title;
|
|
436
|
+
newItem.detail = e.detail;
|
|
437
|
+
newItem.status = e.status;
|
|
438
|
+
}
|
|
439
|
+
return newItem;
|
|
440
|
+
}
|
|
441
|
+
const errorVarPipeline = {
|
|
442
|
+
filter: filterErrorVar,
|
|
443
|
+
key: "error",
|
|
444
|
+
};
|
|
445
|
+
const pipelines = [axiosResponseVarPipeline, errorVarPipeline];
|
|
446
|
+
|
|
447
|
+
function keyValueToArray(keyValue) {
|
|
448
|
+
const key = Object.keys(keyValue)[0];
|
|
449
|
+
return [key, keyValue[key]];
|
|
450
|
+
}
|
|
451
|
+
function logVar(key, value) {
|
|
452
|
+
let [k, v] = keyValueToArray(forceVar(key, value));
|
|
453
|
+
for (const pipeline of pipelines) {
|
|
454
|
+
if (k === pipeline.key) {
|
|
455
|
+
v = pipeline.filter(v);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return { [k]: v };
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function envBoolean(key, { defaultValue }) {
|
|
462
|
+
const value = process.env[key];
|
|
463
|
+
if (value === undefined)
|
|
464
|
+
return defaultValue;
|
|
465
|
+
const lower = value.toLowerCase();
|
|
466
|
+
return !(lower === "" ||
|
|
467
|
+
lower === "0" ||
|
|
468
|
+
lower === "f" ||
|
|
469
|
+
lower === "false" ||
|
|
470
|
+
lower === "n" ||
|
|
471
|
+
lower === "no");
|
|
472
|
+
}
|
|
473
|
+
class JaypieLogger {
|
|
474
|
+
constructor({ level = process.env.LOG_LEVEL, tags = {}, } = {}) {
|
|
475
|
+
this._params = { level, tags };
|
|
476
|
+
this._loggers = [];
|
|
477
|
+
this._tags = {};
|
|
478
|
+
this._withLoggers = {};
|
|
479
|
+
this.level = level || DEFAULT.LEVEL;
|
|
480
|
+
this._tags = { ...logTags(), ...tags };
|
|
481
|
+
this._logger = new Logger({
|
|
482
|
+
format: FORMAT.JSON,
|
|
483
|
+
level: this.level,
|
|
484
|
+
tags: this._tags,
|
|
485
|
+
});
|
|
486
|
+
this._loggers = [this._logger];
|
|
487
|
+
this.debug = ((...args) => this._logger.debug(...args));
|
|
488
|
+
this.debug.var = (...args) => this._logger.debug.var(...args);
|
|
489
|
+
this.error = ((...args) => this._logger.error(...args));
|
|
490
|
+
this.error.var = (...args) => this._logger.error.var(...args);
|
|
491
|
+
this.fatal = ((...args) => this._logger.fatal(...args));
|
|
492
|
+
this.fatal.var = (...args) => this._logger.fatal.var(...args);
|
|
493
|
+
this.info = ((...args) => this._logger.info(...args));
|
|
494
|
+
this.info.var = (...args) => this._logger.info.var(...args);
|
|
495
|
+
this.trace = ((...args) => this._logger.trace(...args));
|
|
496
|
+
this.trace.var = (...args) => this._logger.trace.var(...args);
|
|
497
|
+
this.warn = ((...args) => this._logger.warn(...args));
|
|
498
|
+
this.warn.var = (...args) => this._logger.warn.var(...args);
|
|
499
|
+
this.var = (...args) => this._logger.var(logVar(...args));
|
|
500
|
+
}
|
|
501
|
+
init() {
|
|
502
|
+
for (const logger of this._loggers) {
|
|
503
|
+
if ("init" in logger && typeof logger.init === "function") {
|
|
504
|
+
logger.init();
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const level = this._params.level;
|
|
508
|
+
const tags = this._params.tags;
|
|
509
|
+
this.level = level || DEFAULT.LEVEL;
|
|
510
|
+
this._tags = { ...logTags(), ...tags };
|
|
511
|
+
this._logger = new Logger({
|
|
512
|
+
format: FORMAT.JSON,
|
|
513
|
+
level: this.level,
|
|
514
|
+
tags: this._tags,
|
|
515
|
+
});
|
|
516
|
+
this._loggers = [this._logger];
|
|
517
|
+
this._withLoggers = {};
|
|
518
|
+
const levels = [
|
|
519
|
+
"debug",
|
|
520
|
+
"error",
|
|
521
|
+
"fatal",
|
|
522
|
+
"info",
|
|
523
|
+
"trace",
|
|
524
|
+
"warn",
|
|
525
|
+
];
|
|
526
|
+
levels.forEach((lvl) => {
|
|
527
|
+
this[lvl] = ((...args) => {
|
|
528
|
+
this._logger[lvl](...args);
|
|
529
|
+
});
|
|
530
|
+
this[lvl].var = (...args) => {
|
|
531
|
+
this._logger[lvl].var(...args);
|
|
532
|
+
};
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
lib({ level, lib, tags = {}, } = {}) {
|
|
536
|
+
const newTags = Object.assign({}, this._tags, tags);
|
|
537
|
+
if (lib) {
|
|
538
|
+
newTags.lib = lib;
|
|
539
|
+
}
|
|
540
|
+
const logger = new JaypieLogger({
|
|
541
|
+
level: (() => {
|
|
542
|
+
if (level) {
|
|
543
|
+
return level;
|
|
544
|
+
}
|
|
545
|
+
if (process.env.MODULE_LOG_LEVEL) {
|
|
546
|
+
return process.env.MODULE_LOG_LEVEL;
|
|
547
|
+
}
|
|
548
|
+
if (envBoolean("MODULE_LOGGER", { defaultValue: false })) {
|
|
549
|
+
return process.env.LOG_LEVEL;
|
|
550
|
+
}
|
|
551
|
+
return LEVEL.SILENT;
|
|
552
|
+
})(),
|
|
553
|
+
tags: newTags,
|
|
554
|
+
});
|
|
555
|
+
this._loggers.push(logger._logger);
|
|
556
|
+
return logger;
|
|
557
|
+
}
|
|
558
|
+
tag(tags) {
|
|
559
|
+
for (const logger of this._loggers) {
|
|
560
|
+
logger.tag(tags);
|
|
561
|
+
}
|
|
562
|
+
Object.assign(this._tags, tags);
|
|
563
|
+
}
|
|
564
|
+
untag(...args) {
|
|
565
|
+
for (const logger of this._loggers) {
|
|
566
|
+
logger.untag(...args);
|
|
567
|
+
}
|
|
568
|
+
for (const key of Object.keys(args)) {
|
|
569
|
+
delete this._tags[key];
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
with(...args) {
|
|
573
|
+
if (!args || typeof args !== "object") {
|
|
574
|
+
throw new Error("ConfigurationError: with() requires an object argument");
|
|
575
|
+
}
|
|
576
|
+
const loggerKey = JSON.stringify(args);
|
|
577
|
+
if (Object.keys(this._withLoggers).includes(loggerKey)) {
|
|
578
|
+
return this._withLoggers[loggerKey];
|
|
579
|
+
}
|
|
580
|
+
const logger = new JaypieLogger({
|
|
581
|
+
level: this.level,
|
|
582
|
+
tags: { ...this._tags },
|
|
583
|
+
});
|
|
584
|
+
logger._logger = this._logger.with(...args);
|
|
585
|
+
logger._loggers = [logger._logger];
|
|
586
|
+
this._withLoggers[loggerKey] = logger;
|
|
587
|
+
this._loggers.push(logger._logger);
|
|
588
|
+
return logger;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
function createLogger(tags = {}) {
|
|
592
|
+
const jaypieLogger = new JaypieLogger({
|
|
593
|
+
tags,
|
|
594
|
+
});
|
|
595
|
+
return jaypieLogger;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const log = createLogger();
|
|
599
|
+
|
|
600
|
+
exports.FORMAT = FORMAT;
|
|
601
|
+
exports.LEVEL = LEVEL;
|
|
602
|
+
exports.Logger = Logger;
|
|
603
|
+
exports.createLogger = createLogger;
|
|
604
|
+
exports.default = log;
|
|
605
|
+
exports.log = log;
|
|
606
|
+
//# sourceMappingURL=index.cjs.map
|