@@ -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} ) ;
0 commit comments