Limit logger size via DEBUG_MAX (#802)
* Limit logger size via DEBUG_MAX * Any negative disables * Node imports * Update test
This commit is contained in:
@@ -138,9 +138,8 @@ describe('logger', (): void => {
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does debug log when NODE_ENV=test specified', (): void => {
|
||||
process.env.DEBUG = 'blah';
|
||||
process.env.NODE_ENV = 'test';
|
||||
it('does debug log when DEBUG=* specified', (): void => {
|
||||
process.env.DEBUG = '*';
|
||||
|
||||
l = logger('test');
|
||||
l.debug('test');
|
||||
|
||||
+31
-19
@@ -46,17 +46,29 @@ export function format (value: unknown): unknown {
|
||||
return formatOther(value);
|
||||
}
|
||||
|
||||
function apply (log: LogType, type: string, values: Logger$Data): void {
|
||||
function apply (log: LogType, type: string, values: Logger$Data, maxSize = -1): void {
|
||||
if (values.length === 1 && isFunction(values[0])) {
|
||||
const fnResult = values[0]() as unknown;
|
||||
|
||||
return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult]);
|
||||
return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult], maxSize);
|
||||
}
|
||||
|
||||
console[logTo[log] as 'log'](
|
||||
formatDate(new Date()),
|
||||
type,
|
||||
...values.map(format)
|
||||
...values
|
||||
.map(format)
|
||||
.map((v) => {
|
||||
if (maxSize <= 0) {
|
||||
return v;
|
||||
}
|
||||
|
||||
const r = `${v as string}`;
|
||||
|
||||
return r.length < maxSize
|
||||
? v
|
||||
: `${r.substr(0, maxSize)} ...`;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -64,6 +76,15 @@ function noop (): void {
|
||||
// noop
|
||||
}
|
||||
|
||||
function parseEnv (type: string): [boolean, number] {
|
||||
const maxSize = parseInt(process?.env?.DEBUG_MAX || '-1', 10);
|
||||
|
||||
return [
|
||||
(process?.env?.DEBUG || '').toLowerCase().split(',').some((e) => !!e && (e === '*' || type.startsWith(e))),
|
||||
isNaN(maxSize) ? -1 : maxSize
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @name Logger
|
||||
* @summary Creates a consistent log interface for messages
|
||||
@@ -73,31 +94,22 @@ function noop (): void {
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* const l from '@polkadot/util/logger')('test');
|
||||
* import { logger } from '@polkadot';
|
||||
*
|
||||
* l.log('blah'); // <date> TEST: blah
|
||||
* const l = logger('test');
|
||||
* ```
|
||||
*/
|
||||
export function logger (_type: string): Logger {
|
||||
const type = `${_type.toUpperCase()}:`.padStart(16);
|
||||
let isDebug;
|
||||
|
||||
try {
|
||||
const isTest = process.env.NODE_ENV === 'test';
|
||||
const debugList = (process.env.DEBUG || '').split(',');
|
||||
|
||||
isDebug = isTest || !!debugList.find((entry): boolean => _type.startsWith(entry));
|
||||
} catch (error) {
|
||||
isDebug = false;
|
||||
}
|
||||
const [isDebug, maxSize] = parseEnv(_type.toLowerCase());
|
||||
|
||||
return {
|
||||
debug: isDebug
|
||||
? (...values: Logger$Data): void => apply('debug', type, values)
|
||||
? (...values: Logger$Data) => apply('debug', type, values, maxSize)
|
||||
: noop,
|
||||
error: (...values: Logger$Data): void => apply('error', type, values),
|
||||
log: (...values: Logger$Data): void => apply('log', type, values),
|
||||
error: (...values: Logger$Data) => apply('error', type, values),
|
||||
log: (...values: Logger$Data) => apply('log', type, values),
|
||||
noop,
|
||||
warn: (...values: Logger$Data): void => apply('warn', type, values)
|
||||
warn: (...values: Logger$Data) => apply('warn', type, values)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// Copyright 2017-2020 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TextDecoder as NodeTextDecoder } from 'util';
|
||||
import util from 'util';
|
||||
|
||||
export const TextDecoder = typeof global.TextDecoder === 'undefined'
|
||||
? NodeTextDecoder as unknown as typeof global.TextDecoder
|
||||
? util.TextDecoder as unknown as typeof global.TextDecoder
|
||||
: global.TextDecoder;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// Copyright 2017-2020 @polkadot/x-textencoder authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TextEncoder as NodeTextEncoder } from 'util';
|
||||
import util from 'util';
|
||||
|
||||
class NodeFallback {
|
||||
#encoder: NodeTextEncoder;
|
||||
#encoder: util.TextEncoder;
|
||||
|
||||
constructor () {
|
||||
this.#encoder = new NodeTextEncoder();
|
||||
this.#encoder = new util.TextEncoder();
|
||||
}
|
||||
|
||||
// For a Jest 26.0.1 environment, Buffer !== Uint8Array
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// Copyright 2017-2020 @polkadot/x-ws authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { w3cwebsocket } from 'websocket';
|
||||
import ws from 'websocket';
|
||||
|
||||
export const WebSocket = typeof global.WebSocket === 'undefined'
|
||||
? w3cwebsocket as unknown as typeof global.WebSocket
|
||||
? ws.w3cwebsocket as unknown as typeof global.WebSocket
|
||||
: global.WebSocket;
|
||||
|
||||
Reference in New Issue
Block a user