UNPKG

3.58 kBJavaScriptView Raw
1import { xglobal } from '@polkadot/x-global';
2import { formatDate } from './format/formatDate.js';
3import { isBn } from './is/bn.js';
4import { isBuffer } from './is/buffer.js';
5import { isFunction } from './is/function.js';
6import { isObject } from './is/object.js';
7import { isU8a } from './is/u8a.js';
8import { u8aToHex } from './u8a/toHex.js';
9import { u8aToU8a } from './u8a/toU8a.js';
10import { noop } from './noop.js';
11const logTo = {
12 debug: 'log',
13 error: 'error',
14 log: 'log',
15 warn: 'warn'
16};
17function formatOther(value) {
18 if (value && isObject(value) && value.constructor === Object) {
19 const result = {};
20 for (const [k, v] of Object.entries(value)) {
21 result[k] = loggerFormat(v);
22 }
23 return result;
24 }
25 return value;
26}
27export function loggerFormat(value) {
28 if (Array.isArray(value)) {
29 return value.map(loggerFormat);
30 }
31 else if (isBn(value)) {
32 return value.toString();
33 }
34 else if (isU8a(value) || isBuffer(value)) {
35 return u8aToHex(u8aToU8a(value));
36 }
37 return formatOther(value);
38}
39function formatWithLength(maxLength) {
40 return (v) => {
41 if (maxLength <= 0) {
42 return v;
43 }
44 const r = `${v}`;
45 return r.length < maxLength
46 ? v
47 : `${r.substring(0, maxLength)} ...`;
48 };
49}
50function apply(log, type, values, maxSize = -1) {
51 if (values.length === 1 && isFunction(values[0])) {
52 const fnResult = values[0]();
53 return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult], maxSize);
54 }
55 console[logTo[log]](formatDate(new Date()), type, ...values
56 .map(loggerFormat)
57 .map(formatWithLength(maxSize)));
58}
59function isDebugOn(e, type) {
60 return !!e && (e === '*' ||
61 type === e ||
62 (e.endsWith('*') &&
63 type.startsWith(e.slice(0, -1))));
64}
65function isDebugOff(e, type) {
66 return !!e && (e.startsWith('-') &&
67 (type === e.slice(1) ||
68 (e.endsWith('*') &&
69 type.startsWith(e.slice(1, -1)))));
70}
71function getDebugFlag(env, type) {
72 let flag = false;
73 for (const e of env) {
74 if (isDebugOn(e, type)) {
75 flag = true;
76 }
77 else if (isDebugOff(e, type)) {
78 flag = false;
79 }
80 }
81 return flag;
82}
83function parseEnv(type) {
84 const maxSize = parseInt(xglobal.process?.env?.['DEBUG_MAX'] || '-1', 10);
85 return [
86 getDebugFlag((xglobal.process?.env?.['DEBUG'] || '').toLowerCase().split(','), type),
87 isNaN(maxSize)
88 ? -1
89 : maxSize
90 ];
91}
92/**
93 * @name Logger
94 * @summary Creates a consistent log interface for messages
95 * @description
96 * Returns a `Logger` that has `.log`, `.error`, `.warn` and `.debug` (controlled with environment `DEBUG=typeA,typeB`) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console.
97 * @example
98 * <BR>
99 *
100 * ```javascript
101 * import { logger } from '@polkadot/util';
102 *
103 * const l = logger('test');
104 * ```
105 */
106export function logger(origin) {
107 const type = `${origin.toUpperCase()}:`.padStart(16);
108 const [isDebug, maxSize] = parseEnv(origin.toLowerCase());
109 return {
110 debug: isDebug
111 ? (...values) => apply('debug', type, values, maxSize)
112 : noop,
113 error: (...values) => apply('error', type, values),
114 log: (...values) => apply('log', type, values),
115 noop,
116 warn: (...values) => apply('warn', type, values)
117 };
118}