1 |
|
2 |
|
3 | import { formatDate } from "./format/formatDate.js";
|
4 | import { isBn } from "./is/bn.js";
|
5 | import { isBuffer } from "./is/buffer.js";
|
6 | import { isFunction } from "./is/function.js";
|
7 | import { isObject } from "./is/object.js";
|
8 | import { isU8a } from "./is/u8a.js";
|
9 | import { u8aToHex } from "./u8a/toHex.js";
|
10 | import { u8aToU8a } from "./u8a/toU8a.js";
|
11 | import { hasProcess } from "./has.js";
|
12 | const logTo = {
|
13 | debug: 'log',
|
14 | error: 'error',
|
15 | log: 'log',
|
16 | warn: 'warn'
|
17 | };
|
18 |
|
19 | function formatOther(value) {
|
20 | if (value && isObject(value) && value.constructor === Object) {
|
21 | const result = {};
|
22 |
|
23 | for (const k of Object.keys(value)) {
|
24 | result[k] = loggerFormat(value[k]);
|
25 | }
|
26 |
|
27 | return result;
|
28 | }
|
29 |
|
30 | return value;
|
31 | }
|
32 |
|
33 | export function loggerFormat(value) {
|
34 | if (Array.isArray(value)) {
|
35 | return value.map(loggerFormat);
|
36 | } else if (isBn(value)) {
|
37 | return value.toString();
|
38 | } else if (isU8a(value) || isBuffer(value)) {
|
39 | return u8aToHex(u8aToU8a(value));
|
40 | }
|
41 |
|
42 | return formatOther(value);
|
43 | }
|
44 |
|
45 | function formatWithLength(maxLength) {
|
46 | return v => {
|
47 | if (maxLength <= 0) {
|
48 | return v;
|
49 | }
|
50 |
|
51 | const r = `${v}`;
|
52 | return r.length < maxLength ? v : `${r.substring(0, maxLength)} ...`;
|
53 | };
|
54 | }
|
55 |
|
56 | function apply(log, type, values, maxSize = -1) {
|
57 | if (values.length === 1 && isFunction(values[0])) {
|
58 | const fnResult = values[0]();
|
59 | return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult], maxSize);
|
60 | }
|
61 |
|
62 | console[logTo[log]](formatDate(new Date()), type, ...values.map(loggerFormat).map(formatWithLength(maxSize)));
|
63 | }
|
64 |
|
65 | function noop() {
|
66 | }
|
67 |
|
68 | function isDebugOn(e, type) {
|
69 | return !!e && (e === '*' || type === e || e.endsWith('*') && type.startsWith(e.slice(0, -1)));
|
70 | }
|
71 |
|
72 | function isDebugOff(e, type) {
|
73 | return !!e && e.startsWith('-') && (type === e.slice(1) || e.endsWith('*') && type.startsWith(e.slice(1, -1)));
|
74 | }
|
75 |
|
76 | function getDebugFlag(env, type) {
|
77 | let flag = false;
|
78 |
|
79 | for (const e of env) {
|
80 | if (isDebugOn(e, type)) {
|
81 | flag = true;
|
82 | } else if (isDebugOff(e, type)) {
|
83 | flag = false;
|
84 | }
|
85 | }
|
86 |
|
87 | return flag;
|
88 | }
|
89 |
|
90 | function parseEnv(type) {
|
91 | const env = (hasProcess ? process : {}).env || {};
|
92 | const maxSize = parseInt(env.DEBUG_MAX || '-1', 10);
|
93 | return [getDebugFlag((env.DEBUG || '').toLowerCase().split(','), type), isNaN(maxSize) ? -1 : maxSize];
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | export function logger(_type) {
|
112 | const type = `${_type.toUpperCase()}:`.padStart(16);
|
113 | const [isDebug, maxSize] = parseEnv(_type.toLowerCase());
|
114 | return {
|
115 | debug: isDebug ? (...values) => apply('debug', type, values, maxSize) : noop,
|
116 | error: (...values) => apply('error', type, values),
|
117 | log: (...values) => apply('log', type, values),
|
118 | noop,
|
119 | warn: (...values) => apply('warn', type, values)
|
120 | };
|
121 | } |
\ | No newline at end of file |