logger - wildcard include/exclude with DEBUG env (#932)
This commit is contained in:
@@ -118,6 +118,8 @@ describe('logger', (): void => {
|
||||
});
|
||||
|
||||
it('does debug log when DEBUG partial specified', (): void => {
|
||||
process.env.DEBUG = 'test*';
|
||||
|
||||
l = logger('testing');
|
||||
l.debug('test');
|
||||
|
||||
@@ -160,4 +162,31 @@ describe('logger', (): void => {
|
||||
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not debug log when explicitly excluded', (): void => {
|
||||
process.env.DEBUG = '*,-test';
|
||||
|
||||
l = logger('test');
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not debug log when part of exclusion group', (): void => {
|
||||
process.env.DEBUG = '*,-test:*';
|
||||
|
||||
l = logger('test:sub');
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does debug log when not part of exclusion groups', (): void => {
|
||||
process.env.DEBUG = '*,-test:*,-tes,-a:*';
|
||||
|
||||
l = logger('test');
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -80,11 +80,23 @@ function parseEnv (type: string): [boolean, number] {
|
||||
const env = (typeof process === 'object' ? process : {}).env || {};
|
||||
const maxSize = parseInt(env.DEBUG_MAX || '-1', 10);
|
||||
|
||||
let isDebugOn = false;
|
||||
|
||||
(env.DEBUG || '')
|
||||
.toLowerCase()
|
||||
.split(',')
|
||||
.forEach((e) => {
|
||||
if (!!e && (e === '*' || type === e || (e.endsWith('*') && type.startsWith(e.slice(0, -1))))) {
|
||||
isDebugOn = true;
|
||||
}
|
||||
|
||||
if (!!e && e.startsWith('-') && (type === e.slice(1) || (e.endsWith('*') && type.startsWith(e.slice(1, -1))))) {
|
||||
isDebugOn = false;
|
||||
}
|
||||
});
|
||||
|
||||
return [
|
||||
(env.DEBUG || '')
|
||||
.toLowerCase()
|
||||
.split(',')
|
||||
.some((e) => !!e && (e === '*' || type.startsWith(e))),
|
||||
isDebugOn,
|
||||
isNaN(maxSize)
|
||||
? -1
|
||||
: maxSize
|
||||
|
||||
Reference in New Issue
Block a user