Skip to content

Commit 64f8038

Browse files
Merge branch 'y-nk-julien/log-level-filtering'
2 parents 3c6c9ea + 0e95b3b commit 64f8038

File tree

7 files changed

+85
-2
lines changed

7 files changed

+85
-2
lines changed

packages/common/services/console-logger.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isUndefined,
99
} from '../utils/shared.utils';
1010
import { LoggerService, LogLevel } from './logger.service';
11-
import { isLogLevelEnabled } from './utils';
11+
import { isLogLevelEnabled } from './utils/is-log-level-enabled.util';
1212

1313
const DEFAULT_DEPTH = 5;
1414

packages/common/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './console-logger.service';
22
export * from './logger.service';
3+
export * from './utils/filter-log-levels.util';

packages/common/services/logger.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ import { isObject } from '../utils/shared.utils';
33
import { ConsoleLogger } from './console-logger.service';
44
import { isLogLevelEnabled } from './utils';
55

6+
export const LOG_LEVELS = [
7+
'verbose',
8+
'debug',
9+
'log',
10+
'warn',
11+
'error',
12+
'fatal',
13+
] as const satisfies string[];
14+
615
/**
716
* @publicApi
817
*/
9-
export type LogLevel = 'log' | 'error' | 'warn' | 'debug' | 'verbose' | 'fatal';
18+
export type LogLevel = (typeof LOG_LEVELS)[number];
1019

1120
/**
1221
* @publicApi
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { LOG_LEVELS, LogLevel } from '../logger.service';
2+
import { isLogLevel } from './is-log-level.util';
3+
4+
/**
5+
* @publicApi
6+
*/
7+
export function filterLogLevels(parseableString = ''): LogLevel[] {
8+
const sanitizedSring = parseableString.replaceAll(' ', '').toLowerCase();
9+
10+
if (sanitizedSring[0] === '>') {
11+
const orEqual = sanitizedSring[1] === '=';
12+
13+
const logLevelIndex = (LOG_LEVELS as string[]).indexOf(
14+
sanitizedSring.substring(orEqual ? 2 : 1),
15+
);
16+
17+
if (logLevelIndex === -1) {
18+
throw new Error(`parse error (unknown log level): ${sanitizedSring}`);
19+
}
20+
21+
return LOG_LEVELS.slice(orEqual ? logLevelIndex : logLevelIndex + 1);
22+
} else if (sanitizedSring.includes(',')) {
23+
return sanitizedSring.split(',').filter(isLogLevel);
24+
}
25+
26+
return isLogLevel(sanitizedSring) ? [sanitizedSring] : LOG_LEVELS;
27+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from './filter-log-levels.util';
12
export * from './is-log-level-enabled.util';
3+
export * from './is-log-level.util';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { LOG_LEVELS, LogLevel } from '../logger.service';
2+
3+
/**
4+
* @publicApi
5+
*/
6+
export function isLogLevel(maybeLogLevel: any): maybeLogLevel is LogLevel {
7+
return LOG_LEVELS.includes(maybeLogLevel);
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { expect } from 'chai';
2+
import { filterLogLevels } from '../../../services/utils/filter-log-levels.util';
3+
4+
describe('filterLogLevels', () => {
5+
it('should correctly parse an exclusive range', () => {
6+
const returned = filterLogLevels('>warn');
7+
expect(returned).to.deep.equal(['error', 'fatal']);
8+
});
9+
10+
it('should correctly parse an inclusive range', () => {
11+
const returned = filterLogLevels('>=warn');
12+
expect(returned).to.deep.equal(['warn', 'error', 'fatal']);
13+
});
14+
15+
it('should correctly parse a string list', () => {
16+
const returned = filterLogLevels('verbose,warn,fatal');
17+
expect(returned).to.deep.equal(['verbose', 'warn', 'fatal']);
18+
});
19+
20+
it('should correctly parse a single log level', () => {
21+
const returned = filterLogLevels('debug');
22+
expect(returned).to.deep.equal(['debug']);
23+
});
24+
25+
it('should return all otherwise', () => {
26+
const returned = filterLogLevels();
27+
expect(returned).to.deep.equal([
28+
'verbose',
29+
'debug',
30+
'log',
31+
'warn',
32+
'error',
33+
'fatal',
34+
]);
35+
});
36+
});

0 commit comments

Comments
 (0)