Skip to content

Commit 0e95b3b

Browse files
refactor(common): move logger utils to dedicated files
1 parent 9d270a2 commit 0e95b3b

File tree

8 files changed

+77
-77
lines changed

8 files changed

+77
-77
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: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { isObject } from '../utils/shared.utils';
33
import { ConsoleLogger } from './console-logger.service';
44
import { isLogLevelEnabled } from './utils';
55

6-
const LOG_LEVELS = [
6+
export const LOG_LEVELS = [
77
'verbose',
88
'debug',
99
'log',
@@ -17,38 +17,6 @@ const LOG_LEVELS = [
1717
*/
1818
export type LogLevel = (typeof LOG_LEVELS)[number];
1919

20-
/**
21-
* @publicApi
22-
*/
23-
export function isLogLevel(maybeLogLevel: any): maybeLogLevel is LogLevel {
24-
return LOG_LEVELS.includes(maybeLogLevel);
25-
}
26-
27-
/**
28-
* @publicApi
29-
*/
30-
export function filterLogLevels(parseableString = ''): LogLevel[] {
31-
const sanitizedSring = parseableString.replaceAll(' ', '').toLowerCase();
32-
33-
if (sanitizedSring[0] === '>') {
34-
const orEqual = sanitizedSring[1] === '=';
35-
36-
const logLevelIndex = (LOG_LEVELS as string[]).indexOf(
37-
sanitizedSring.substring(orEqual ? 2 : 1),
38-
);
39-
40-
if (logLevelIndex === -1) {
41-
throw new Error(`parse error (unknown log level): ${sanitizedSring}`);
42-
}
43-
44-
return LOG_LEVELS.slice(orEqual ? logLevelIndex : logLevelIndex + 1);
45-
} else if (sanitizedSring.includes(',')) {
46-
return sanitizedSring.split(',').filter(isLogLevel);
47-
}
48-
49-
return isLogLevel(sanitizedSring) ? [sanitizedSring] : LOG_LEVELS;
50-
}
51-
5220
/**
5321
* @publicApi
5422
*/
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+
}

packages/common/test/services/logger.service.spec.ts

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,9 @@
11
import { expect } from 'chai';
22
import 'reflect-metadata';
33
import * as sinon from 'sinon';
4-
import {
5-
ConsoleLogger,
6-
filterLogLevels,
7-
Logger,
8-
LoggerService,
9-
LogLevel,
10-
} from '../../services';
4+
import { ConsoleLogger, Logger, LoggerService, LogLevel } from '../../services';
115

126
describe('Logger', () => {
13-
describe('[log helpers]', () => {
14-
describe('when using filterLogLevels', () => {
15-
it('should correctly parse an exclusive range', () => {
16-
const returned = filterLogLevels('>warn');
17-
expect(returned).to.deep.equal(['error', 'fatal']);
18-
});
19-
20-
it('should correctly parse an inclusive range', () => {
21-
const returned = filterLogLevels('>=warn');
22-
expect(returned).to.deep.equal(['warn', 'error', 'fatal']);
23-
});
24-
25-
it('should correctly parse a string list', () => {
26-
const returned = filterLogLevels('verbose,warn,fatal');
27-
expect(returned).to.deep.equal(['verbose', 'warn', 'fatal']);
28-
});
29-
30-
it('should correctly parse a single log level', () => {
31-
const returned = filterLogLevels('debug');
32-
expect(returned).to.deep.equal(['debug']);
33-
});
34-
35-
it('should return all otherwise', () => {
36-
const returned = filterLogLevels();
37-
expect(returned).to.deep.equal([
38-
'verbose',
39-
'debug',
40-
'log',
41-
'warn',
42-
'error',
43-
'fatal',
44-
]);
45-
});
46-
});
47-
});
48-
497
describe('[static methods]', () => {
508
describe('when the default logger is used', () => {
519
let processStdoutWriteSpy: sinon.SinonSpy;
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)