Skip to content

Commit 78d84b3

Browse files
feat: change console log driver call signature
- allow change console.info to another log - fix file naming
1 parent 17d8865 commit 78d84b3

File tree

11 files changed

+76
-73
lines changed

11 files changed

+76
-73
lines changed

.idea/microservice-nodejs.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__tests__/drivers/console-log-test.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,62 @@ import {
66
LOG_EXTERNAL_COLOR,
77
LOG_ERROR_COLOR,
88
} from '@constants/index';
9-
import ConsoleLogDriver from '@drivers/console-log';
10-
import { LogType } from '@interfaces/drivers/log-driver';
9+
import ConsoleLog from '@drivers/console-log';
10+
import { LogType } from '@interfaces/drivers/console-log';
1111

1212
describe('drivers/console-log', () => {
13+
let consoleInfoStub = sinon.stub();
14+
1315
const message = 'hello world';
1416
const getMessage = () => message;
1517

1618
beforeEach(() => {
17-
sinon.stub(console, 'info');
19+
consoleInfoStub = sinon.stub(console, 'info');
1820
});
1921

2022
afterEach(() => {
2123
sinon.restore();
2224
});
2325

2426
it('should correct log default type output', () => {
25-
ConsoleLogDriver(getMessage);
27+
ConsoleLog()(getMessage);
2628

27-
expect(console.info).calledOnceWith(LOG_INFO_COLOR, sinon.match(message));
29+
expect(consoleInfoStub).calledOnceWith(LOG_INFO_COLOR, sinon.match(message));
2830
});
2931

3032
it('should correct log IN INTERNAL output', () => {
31-
ConsoleLogDriver(getMessage, LogType.REQ_INTERNAL, 1);
33+
ConsoleLog()(getMessage, LogType.REQ_INTERNAL, 1);
3234

33-
expect(console.info).calledOnceWith(LOG_INTERNAL_COLOR, sinon.match(message));
35+
expect(consoleInfoStub).calledOnceWith(LOG_INTERNAL_COLOR, sinon.match(message));
3436
});
3537

3638
it('should correct log OUT INTERNAL output', () => {
37-
ConsoleLogDriver(getMessage, LogType.RES_INTERNAL, 1);
39+
ConsoleLog()(getMessage, LogType.RES_INTERNAL, 1);
3840

39-
expect(console.info).calledOnceWith(LOG_INTERNAL_COLOR, sinon.match(message));
41+
expect(consoleInfoStub).calledOnceWith(LOG_INTERNAL_COLOR, sinon.match(message));
4042
});
4143

4244
it('should correct log IN EXTERNAL output', () => {
43-
ConsoleLogDriver(getMessage, LogType.REQ_EXTERNAL, 1);
45+
ConsoleLog()(getMessage, LogType.REQ_EXTERNAL, 1);
4446

45-
expect(console.info).calledOnceWith(LOG_EXTERNAL_COLOR, sinon.match(message));
47+
expect(consoleInfoStub).calledOnceWith(LOG_EXTERNAL_COLOR, sinon.match(message));
4648
});
4749

4850
it('should correct log OUT EXTERNAL output', () => {
49-
ConsoleLogDriver(getMessage, LogType.RES_EXTERNAL, 1);
51+
ConsoleLog()(getMessage, LogType.RES_EXTERNAL, 1);
5052

51-
expect(console.info).calledOnceWith(LOG_EXTERNAL_COLOR, sinon.match(message));
53+
expect(consoleInfoStub).calledOnceWith(LOG_EXTERNAL_COLOR, sinon.match(message));
5254
});
5355

5456
it('should correct log INFO output', () => {
55-
ConsoleLogDriver(getMessage, LogType.INFO, 1);
57+
ConsoleLog()(getMessage, LogType.INFO, 1);
5658

57-
expect(console.info).calledOnceWith(LOG_INFO_COLOR, sinon.match(message));
59+
expect(consoleInfoStub).calledOnceWith(LOG_INFO_COLOR, sinon.match(message));
5860
});
5961

6062
it('should correct log ERROR output', () => {
61-
ConsoleLogDriver(getMessage, LogType.ERROR, 1);
63+
ConsoleLog()(getMessage, LogType.ERROR, 1);
6264

63-
expect(console.info).calledOnceWith(LOG_ERROR_COLOR, sinon.match(message));
65+
expect(consoleInfoStub).calledOnceWith(LOG_ERROR_COLOR, sinon.match(message));
6466
});
6567
});

__tests__/services/abstract-microservice-test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import sinon from 'sinon';
55
import BaseException from '@core/base-exception';
66
import MicroserviceRequest from '@core/microservice-request';
77
import MicroserviceResponse from '@core/microservice-response';
8-
import ConsoleLogDriver from '@drivers/console-log';
8+
import ConsoleLog from '@drivers/console-log';
99
import { MiddlewareHandler, MiddlewareType } from '@interfaces/services/i-abstract-microservice';
1010
import AbstractMicroservice from '@services/abstract-microservice';
1111
import Microservice from '@services/microservice';
@@ -60,12 +60,12 @@ describe('services/abstract-microservice', () => {
6060
const testEndpoint = 'endpoint';
6161
const endpointHandler = () => ({ hello: 'world' });
6262

63-
before(() => {
63+
beforeEach(() => {
6464
sinon.stub(process, 'exit');
6565
sinon.stub(console, 'info');
6666
});
6767

68-
after(() => {
68+
afterEach(() => {
6969
sinon.restore();
7070
});
7171

@@ -87,10 +87,6 @@ describe('services/abstract-microservice', () => {
8787
expect(ms).to.have.property('options').property('version').equal('1.0.0');
8888
});
8989

90-
it('should correct set default log driver', () => {
91-
expect(ms).to.have.property('logDriver').equal(ConsoleLogDriver);
92-
});
93-
9490
it('should correct instantiate microservice without log driver', () => {
9591
const sandbox = sinon.createSandbox();
9692

@@ -99,7 +95,7 @@ describe('services/abstract-microservice', () => {
9995
const localMs = Microservice.create({}, { logDriver: false });
10096
const driver = localMs['logDriver'];
10197

102-
expect(driver).not.equal(ConsoleLogDriver);
98+
expect(driver).not.equal(ConsoleLog);
10399
// noinspection JSVoidFunctionReturnValueUsed
104100
expect(driver(() => '')).to.undefined;
105101

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lomray/microservice-nodejs-lib",
3-
"version": "2.1.1",
3+
"version": "2.2.1",
44
"description": "Package for create microservice architecture based on NodeJS & inverted json.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/drivers/console-log.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,60 @@ import {
55
LOG_INFO_COLOR,
66
LOG_INTERNAL_COLOR,
77
} from '@constants/index';
8-
import { LogDriverType, LogType } from '@interfaces/drivers/log-driver';
8+
import { LogType, ConsoleInfoType } from '@interfaces/drivers/console-log';
99

1010
const reqIds = new Map();
1111

1212
/**
1313
* @constructor
1414
*/
15-
const ConsoleLogDriver: LogDriverType = (getMessage, type = LogType.INFO, id = 0) => {
16-
let color;
17-
let reqTime = '';
18-
19-
switch (type) {
20-
case LogType.INFO:
21-
color = LOG_INFO_COLOR;
22-
break;
23-
24-
case LogType.ERROR:
25-
color = LOG_ERROR_COLOR;
26-
break;
27-
28-
case LogType.REQ_INTERNAL:
29-
case LogType.RES_INTERNAL:
30-
color = LOG_INTERNAL_COLOR;
31-
break;
32-
33-
case LogType.REQ_EXTERNAL:
34-
case LogType.RES_EXTERNAL:
35-
color = LOG_EXTERNAL_COLOR;
36-
break;
37-
}
38-
39-
if (id) {
15+
const ConsoleLog: ConsoleInfoType =
16+
(log = console.info) =>
17+
(getMessage, type = LogType.INFO, id = 0) => {
18+
let color = '';
19+
let reqTime = '';
20+
4021
switch (type) {
41-
case LogType.REQ_INTERNAL:
42-
case LogType.REQ_EXTERNAL:
43-
reqIds.set(`${type}|${id}`, performance.now());
22+
case LogType.INFO:
23+
color = LOG_INFO_COLOR;
24+
break;
25+
26+
case LogType.ERROR:
27+
color = LOG_ERROR_COLOR;
4428
break;
4529

30+
case LogType.REQ_INTERNAL:
4631
case LogType.RES_INTERNAL:
47-
case LogType.RES_EXTERNAL:
48-
const key = type === LogType.RES_INTERNAL ? LogType.REQ_INTERNAL : LogType.REQ_EXTERNAL;
32+
color = LOG_INTERNAL_COLOR;
33+
break;
4934

50-
reqTime = `+${(performance.now() - reqIds.get(`${key}|${id}`)).toFixed(2)} ms \n`;
51-
reqIds.delete(key);
35+
case LogType.REQ_EXTERNAL:
36+
case LogType.RES_EXTERNAL:
37+
color = LOG_EXTERNAL_COLOR;
5238
break;
39+
}
5340

54-
case LogType.ERROR:
55-
reqIds.clear();
41+
if (id) {
42+
switch (type) {
43+
case LogType.REQ_INTERNAL:
44+
case LogType.REQ_EXTERNAL:
45+
reqIds.set(`${type}|${id}`, performance.now());
46+
break;
47+
48+
case LogType.RES_INTERNAL:
49+
case LogType.RES_EXTERNAL:
50+
const key = type === LogType.RES_INTERNAL ? LogType.REQ_INTERNAL : LogType.REQ_EXTERNAL;
51+
52+
reqTime = `+${(performance.now() - reqIds.get(`${key}|${id}`)).toFixed(2)} ms \n`;
53+
reqIds.delete(key);
54+
break;
55+
56+
case LogType.ERROR:
57+
reqIds.clear();
58+
}
5659
}
57-
}
5860

59-
console.info(color, `${getMessage()} ${reqTime}`);
60-
};
61+
log(color, `${getMessage()} ${reqTime}`);
62+
};
6163

62-
export default ConsoleLogDriver;
64+
export default ConsoleLog;

src/interfaces/drivers/log-driver.ts renamed to src/interfaces/drivers/console-log.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ enum LogType {
1515
*/
1616
type LogDriverType = (getMessage: () => string, type?: LogType, id?: number | string) => void;
1717

18-
export { LogDriverType, LogType };
18+
type ConsoleInfoType = (log?: (color: string, message: string) => void) => LogDriverType;
19+
20+
export { LogDriverType, LogType, ConsoleInfoType };

src/interfaces/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import IBaseException from './core/i-base-exception';
22

3-
export * from './drivers/log-driver';
3+
export * from './drivers/console-log';
44

55
export * from './core/i-microservice-response';
66

src/interfaces/services/i-abstract-microservice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import MicroserviceRequest from '@core/microservice-request';
44
import MicroserviceResponse from '@core/microservice-response';
55
import type { IMicroserviceRequest } from '@interfaces/core/i-microservice-request';
66
import type { IMicroserviceResponseResult } from '@interfaces/core/i-microservice-response';
7-
import type { LogDriverType } from '@interfaces/drivers/log-driver';
7+
import type { LogDriverType } from '@interfaces/drivers/console-log';
88
import AbstractMicroservice from '@services/abstract-microservice';
99

1010
interface IAbstractMicroserviceOptions {

src/services/abstract-microservice.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { EXCEPTION_CODE, PROCESS_EXIT_EVENT_TYPES } from '@constants/index';
77
import BaseException from '@core/base-exception';
88
import MicroserviceRequest from '@core/microservice-request';
99
import MicroserviceResponse from '@core/microservice-response';
10-
import ConsoleLogDriver from '@drivers/console-log';
10+
import ConsoleLog from '@drivers/console-log';
1111
import ResolveSrv from '@helpers/resolve-srv';
1212
import type IBaseException from '@interfaces/core/i-base-exception';
1313
import type { IMicroserviceRequest } from '@interfaces/core/i-microservice-request';
14-
import { LogDriverType, LogType } from '@interfaces/drivers/log-driver';
14+
import { LogDriverType, LogType } from '@interfaces/drivers/console-log';
1515
import type {
1616
IAbstractMicroserviceOptions,
1717
IAbstractMicroserviceParams,
@@ -49,7 +49,7 @@ abstract class AbstractMicroservice {
4949
* Microservice log driver
5050
* @private
5151
*/
52-
protected logDriver: LogDriverType = ConsoleLogDriver;
52+
protected logDriver: LogDriverType = ConsoleLog();
5353

5454
/**
5555
* Cache connection if it SRV record

src/services/gateway.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EXCEPTION_CODE } from '@constants/index';
66
import BaseException from '@core/base-exception';
77
import MicroserviceRequest from '@core/microservice-request';
88
import MicroserviceResponse from '@core/microservice-response';
9-
import { LogType } from '@interfaces/drivers/log-driver';
9+
import { LogType } from '@interfaces/drivers/console-log';
1010
import { MiddlewareType } from '@interfaces/services/i-abstract-microservice';
1111
import {
1212
GatewayEndpointHandler,

0 commit comments

Comments
 (0)