Skip to content

Commit a64be51

Browse files
feat: add log level
1 parent 4263b5d commit a64be51

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

.run/Helpers.run.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Run" type="mocha-javascript-test-runner" folderName="tests">
3+
<node-interpreter>project</node-interpreter>
4+
<node-options>--no-warnings</node-options>
5+
<mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
6+
<working-directory>$PROJECT_DIR$</working-directory>
7+
<pass-parent-env>true</pass-parent-env>
8+
<envs>
9+
<env name="TS_NODE_COMPILER_OPTIONS" value="{&quot;target&quot;:&quot;es6&quot;}" />
10+
<env name="NODE_ENV" value="tests" />
11+
</envs>
12+
<ui>bdd</ui>
13+
<extra-mocha-options>--require ts-node/register --watch</extra-mocha-options>
14+
<test-kind>PATTERN</test-kind>
15+
<test-pattern>__tests__/**/*.ts</test-pattern>
16+
<method v="2" />
17+
</configuration>
18+
</component>

__tests__/drivers/console-log-test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { expect } from 'chai';
22
import sinon from 'sinon';
33
import {
4-
LOG_INTERNAL_COLOR,
5-
LOG_INFO_COLOR,
6-
LOG_EXTERNAL_COLOR,
74
LOG_ERROR_COLOR,
5+
LOG_EXTERNAL_COLOR,
6+
LOG_INFO_COLOR,
7+
LOG_INTERNAL_COLOR,
88
} from '@constants/index';
99
import ConsoleLog from '@drivers/console-log';
1010
import { LogType } from '@interfaces/drivers/console-log';
@@ -77,4 +77,10 @@ describe('drivers/console-log', () => {
7777
reqTime: '',
7878
});
7979
});
80+
81+
it('should correct working log level output', () => {
82+
ConsoleLog(undefined, LogType.ERROR)(getMessage, LogType.INFO, 1);
83+
84+
expect(consoleInfoStub).not.called;
85+
});
8086
});

src/drivers/console-log.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ const reqIds = new Map();
1313
* @constructor
1414
*/
1515
const ConsoleLog: ConsoleInfoType =
16-
(log) =>
16+
(log, logLevel = LogType.REQ_INTERNAL) =>
1717
(getMessage, type = LogType.INFO, id = 0) => {
1818
let color = '';
1919
let reqTime = '';
2020

21+
if (type < logLevel) {
22+
return;
23+
}
24+
2125
switch (type) {
2226
case LogType.INFO:
2327
color = LOG_INFO_COLOR;

src/interfaces/drivers/console-log.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Log messages type
33
*/
44
enum LogType {
5-
REQ_INTERNAL,
6-
RES_INTERNAL,
7-
REQ_EXTERNAL,
8-
RES_EXTERNAL,
9-
INFO,
10-
ERROR,
5+
REQ_INTERNAL = 0,
6+
RES_INTERNAL = 1,
7+
REQ_EXTERNAL = 2,
8+
RES_EXTERNAL = 3,
9+
INFO = 4,
10+
ERROR = 5,
1111
}
1212

1313
/**
@@ -16,10 +16,11 @@ enum LogType {
1616
type LogDriverType = (getMessage: () => string, type?: LogType, id?: number | string) => void;
1717

1818
type ConsoleInfoType = (
19-
log?: (
19+
logFunc?: (
2020
message: string,
2121
params: { type: LogType; color: string; reqTime: string; id?: number | string },
2222
) => void,
23+
logLevel?: LogType,
2324
) => LogDriverType;
2425

2526
export { LogDriverType, LogType, ConsoleInfoType };

src/services/gateway.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class Gateway extends AbstractMicroservice {
316316

317317
try {
318318
this.logDriver(
319-
() => `client request --> ${request.getMethod()} / ${request.getId()!}`,
319+
() => `request --> ${request.getMethod()} / ${request.getId()!}`,
320320
LogType.REQ_EXTERNAL,
321321
`${request.getId()!}-gateway`,
322322
);

0 commit comments

Comments
 (0)