Skip to content

Commit f6cf603

Browse files
akapagergaczd
andcommitted
fix(timer): also works with ecs fields
AUT-2249 Co-authored-by: Daniel Gergacz <[email protected]>
1 parent 3a5da9e commit f6cf603

File tree

4 files changed

+103
-35
lines changed

4 files changed

+103
-35
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
],
2929
"devDependencies": {
3030
"@types/chai": "4.3.3",
31+
"@types/lodash": "4.14.198",
3132
"@types/mocha": "10.0.0",
3233
"@types/node": "18.7.23",
3334
"@types/sinon": "10.0.13",
@@ -59,5 +60,8 @@
5960
"bugs": {
6061
"url": "https://github.com/emartech/json-logger-js/issues"
6162
},
62-
"homepage": "https://github.com/emartech/json-logger-js#readme"
63+
"homepage": "https://github.com/emartech/json-logger-js#readme",
64+
"dependencies": {
65+
"lodash": "4.17.21"
66+
}
6367
}

src/logger/logger.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ describe('Logger', () => {
6565
expect(Logger.config.output).not.to.have.been.called;
6666
});
6767

68-
it('should not call log info method when disabled', () => {
68+
it('should not call log info method when disabled (timer)', () => {
6969
logger = new Logger('mongo', false);
7070
const timer = logger.timer();
7171
const infoStub = sinon.stub(logger, 'info');
7272

7373
clock.tick(100);
7474
timer.info('hi');
7575

76-
expect(infoStub).to.have.been.calledWith('hi', { duration: 100 });
76+
expect(infoStub).to.have.been.calledWith('hi', { event: { duration: 100 } });
7777
});
7878

7979
it('should log error with action (legacy format)', () => {

src/timer/timer.spec.ts

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,95 @@ describe('Timer', () => {
1212

1313
afterEach(() => {
1414
clock.restore();
15+
Logger.configure({ outputFormat: 'ecs' });
1516
});
1617

17-
it('should log elapsed time', () => {
18-
const logger = new Logger('test', false);
19-
const infoStub = stub(logger, 'info');
20-
const timer = new Timer(logger);
18+
describe('legacy format', () => {
19+
beforeEach(() => {
20+
Logger.configure({ outputFormat: 'legacy' });
21+
});
2122

22-
clock.tick(100);
23-
timer.info('time', { customer_id: 10 });
23+
it('should log elapsed time (legacy format)', () => {
24+
const logger = new Logger('test', false);
25+
const infoStub = stub(logger, 'info');
26+
const timer = new Timer(logger);
2427

25-
expect(infoStub).to.have.been.calledWith('time', { customer_id: 10, duration: 100 });
26-
});
28+
clock.tick(100);
29+
timer.info('time', { customer_id: 10 });
30+
31+
expect(infoStub).to.have.been.calledWith('time', { customer_id: 10, duration: 100 });
32+
});
33+
34+
it('should log elapsed time with error', () => {
35+
const logger = new Logger('test', false);
36+
const errorStub = stub(logger, 'fromError');
37+
const timer = new Timer(logger);
38+
const error = new Error('intended');
39+
40+
clock.tick(100);
41+
timer.fromError('time', error, { customer_id: 10 });
2742

28-
it('should log elapsed time with error', () => {
29-
const logger = new Logger('test', false);
30-
const errorStub = stub(logger, 'fromError');
31-
const timer = new Timer(logger);
32-
const error = new Error('intended');
43+
expect(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, duration: 100 });
44+
});
3345

34-
clock.tick(100);
35-
timer.fromError('time', error, { customer_id: 10 });
46+
it('should log elapsed time with error', () => {
47+
const logger = new Logger('test', false);
48+
const errorStub = stub(logger, 'warnFromError');
49+
const timer = new Timer(logger);
50+
const error = new Error('intended');
3651

37-
expect(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, duration: 100 });
52+
clock.tick(100);
53+
timer.warnFromError('time', error, { customer_id: 10 });
54+
55+
expect(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, duration: 100 });
56+
});
3857
});
3958

40-
it('should log elapsed time with error', () => {
41-
const logger = new Logger('test', false);
42-
const errorStub = stub(logger, 'warnFromError');
43-
const timer = new Timer(logger);
44-
const error = new Error('intended');
59+
describe('ecs format', () => {
60+
it('should log elapsed time (legacy format)', () => {
61+
const logger = new Logger('test', false);
62+
const infoStub = stub(logger, 'info');
63+
const timer = new Timer(logger);
64+
65+
clock.tick(100);
66+
timer.info('time', { customer_id: 10 });
67+
68+
expect(infoStub).to.have.been.calledWith('time', { customer_id: 10, event: { duration: 100 } });
69+
});
70+
71+
it('should log elapsed time with error', () => {
72+
const logger = new Logger('test', false);
73+
const errorStub = stub(logger, 'fromError');
74+
const timer = new Timer(logger);
75+
const error = new Error('intended');
76+
77+
clock.tick(100);
78+
timer.fromError('time', error, { customer_id: 10 });
79+
80+
expect(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, event: { duration: 100 } });
81+
});
82+
83+
it('should log elapsed time with error', () => {
84+
const logger = new Logger('test', false);
85+
const errorStub = stub(logger, 'warnFromError');
86+
const timer = new Timer(logger);
87+
const error = new Error('intended');
88+
89+
clock.tick(100);
90+
timer.warnFromError('time', error, { customer_id: 10 });
91+
92+
expect(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, event: { duration: 100 } });
93+
});
94+
95+
it('should not overwrite ecs fields', () => {
96+
const logger = new Logger('test', false);
97+
const logStub = stub(logger, 'warn');
98+
const timer = new Timer(logger);
4599

46-
clock.tick(100);
47-
timer.warnFromError('time', error, { customer_id: 10 });
100+
clock.tick(100);
101+
timer.warn('time', { event: { majomkutya: 1 } });
48102

49-
expect(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, duration: 100 });
103+
expect(logStub).to.have.been.calledWith('time', { event: { duration: 100, majomkutya: 1 } });
104+
});
50105
});
51106
});

src/timer/timer.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Logger } from '../logger/logger';
2+
import { merge } from 'lodash';
23

34
export class Timer {
45
private readonly start: number;
@@ -10,35 +11,43 @@ export class Timer {
1011
}
1112

1213
trace(action: string, data: unknown = {}) {
13-
this.logger.trace(action, Object.assign({ duration: this.duration() }, data));
14+
this.logger.trace(action, this.getData(data));
1415
}
1516

1617
debug(action: string, data: unknown = {}) {
17-
this.logger.debug(action, Object.assign({ duration: this.duration() }, data));
18+
this.logger.debug(action, this.getData(data));
1819
}
1920

2021
info(action: string, data: unknown = {}) {
21-
this.logger.info(action, Object.assign({ duration: this.duration() }, data));
22+
this.logger.info(action, this.getData(data));
2223
}
2324

2425
warn(action: string, data: unknown = {}) {
25-
this.logger.warn(action, Object.assign({ duration: this.duration() }, data));
26+
this.logger.warn(action, this.getData(data));
2627
}
2728

2829
error(action: string, data: unknown = {}) {
29-
this.logger.error(action, Object.assign({ duration: this.duration() }, data));
30+
this.logger.error(action, this.getData(data));
3031
}
3132

3233
fatal(action: string, data: unknown = {}) {
33-
this.logger.fatal(action, Object.assign({ duration: this.duration() }, data));
34+
this.logger.fatal(action, this.getData(data));
3435
}
3536

3637
fromError(action: string, error: unknown, data: unknown = {}) {
37-
this.logger.fromError(action, error, Object.assign({ duration: this.duration() }, data));
38+
this.logger.fromError(action, error, this.getData(data));
3839
}
3940

4041
warnFromError(action: string, error: unknown, data: unknown = {}) {
41-
this.logger.warnFromError(action, error, Object.assign({ duration: this.duration() }, data));
42+
this.logger.warnFromError(action, error, this.getData(data));
43+
}
44+
45+
private getData(data: unknown): Record<string, any> {
46+
if (Logger.config.outputFormat === 'legacy') {
47+
return Object.assign({ duration: this.duration() }, data);
48+
}
49+
50+
return merge({ event: { duration: this.duration() } }, data);
4251
}
4352

4453
private duration(): number {

0 commit comments

Comments
 (0)