@@ -38,10 +38,11 @@ addFormats(ajv)
38
38
const validate = ajv . compile ( require ( '../../../utils/schema.json' ) )
39
39
40
40
test ( 'Should produce valid ecs logs' , t => {
41
- const stream = split ( JSON . parse ) . once ( 'data' , line => {
42
- t . deepEqual ( line [ 'log.level' ] , 'info' )
43
- t . ok ( validate ( line ) )
44
- t . equal ( ecsLoggingValidate ( line ) , null )
41
+ const stream = split ( ) . once ( 'data' , line => {
42
+ const rec = JSON . parse ( line )
43
+ t . deepEqual ( rec [ 'log.level' ] , 'info' )
44
+ t . ok ( validate ( rec ) )
45
+ t . equal ( ecsLoggingValidate ( line , { ignoreIndex : true } ) , null )
45
46
t . end ( )
46
47
} )
47
48
@@ -50,10 +51,11 @@ test('Should produce valid ecs logs', t => {
50
51
} )
51
52
52
53
test ( 'Should map "name" to "log.logger"' , t => {
53
- const stream = split ( JSON . parse ) . once ( 'data' , line => {
54
- t . deepEqual ( line . log , { logger : 'myName' } )
55
- t . ok ( validate ( line ) )
56
- t . equal ( ecsLoggingValidate ( line ) , null )
54
+ const stream = split ( ) . once ( 'data' , line => {
55
+ const rec = JSON . parse ( line )
56
+ t . deepEqual ( rec . log , { logger : 'myName' } )
57
+ t . ok ( validate ( rec ) )
58
+ t . equal ( ecsLoggingValidate ( line , { ignoreIndex : true } ) , null )
57
59
t . end ( )
58
60
} )
59
61
@@ -63,10 +65,11 @@ test('Should map "name" to "log.logger"', t => {
63
65
} )
64
66
65
67
test ( 'Should append any additional property to the log message' , t => {
66
- const stream = split ( JSON . parse ) . once ( 'data' , line => {
67
- t . equal ( line . foo , 'bar' )
68
- t . ok ( validate ( line ) )
69
- t . equal ( ecsLoggingValidate ( line ) , null )
68
+ const stream = split ( ) . once ( 'data' , line => {
69
+ const rec = JSON . parse ( line )
70
+ t . equal ( rec . foo , 'bar' )
71
+ t . ok ( validate ( rec ) )
72
+ t . equal ( ecsLoggingValidate ( line , { ignoreIndex : true } ) , null )
70
73
t . end ( )
71
74
} )
72
75
@@ -87,9 +90,10 @@ test('can log non-HTTP res & req fields', t => {
87
90
test ( 'convertReqRes:true and HTTP req, res' , t => {
88
91
t . plan ( 7 )
89
92
90
- const stream = split ( JSON . parse ) . on ( 'data' , rec => {
93
+ const stream = split ( ) . on ( 'data' , line => {
94
+ const rec = JSON . parse ( line )
91
95
t . ok ( validate ( rec ) )
92
- t . equal ( ecsLoggingValidate ( rec ) , null )
96
+ t . equal ( ecsLoggingValidate ( line , { ignoreIndex : true } ) , null )
93
97
if ( rec . message === 'handled request' ) {
94
98
// Spot check that some of the ECS HTTP and User agent fields are there.
95
99
t . equal ( rec . http . request . method , 'get' , 'http.request.method' )
@@ -128,14 +132,14 @@ test('convertReqRes:true and HTTP req, res', t => {
128
132
} )
129
133
130
134
test ( 'convertErr is true by default' , t => {
131
- const recs = [ ]
132
- const stream = split ( JSON . parse ) . on ( 'data' , rec => { recs . push ( rec ) } )
135
+ const lines = [ ]
136
+ const stream = split ( ) . on ( 'data' , line => { lines . push ( line ) } )
133
137
const log = pino ( { ...ecsFormat ( ) } , stream )
134
138
135
139
log . info ( { err : new Error ( 'boom' ) } , 'hi' )
136
- const rec = recs [ 0 ]
140
+ const rec = JSON . parse ( lines [ 0 ] )
137
141
t . ok ( validate ( rec ) )
138
- t . equal ( ecsLoggingValidate ( rec ) , null )
142
+ t . equal ( ecsLoggingValidate ( lines [ 0 ] , { ignoreIndex : true } ) , null )
139
143
t . equal ( rec . error . type , 'Error' )
140
144
t . equal ( rec . error . message , 'boom' )
141
145
t . match ( rec . error . stack_trace , / ^ E r r o r : b o o m \n { 4 } a t / )
@@ -159,18 +163,18 @@ test('convertErr does not blow up on non-Errors', t => {
159
163
} )
160
164
161
165
test ( 'convertErr=false allows passing through err=<non-Error>' , t => {
162
- const recs = [ ]
163
- const stream = split ( JSON . parse ) . on ( 'data' , rec => { recs . push ( rec ) } )
166
+ const lines = [ ]
167
+ const stream = split ( ) . on ( 'data' , line => { lines . push ( line ) } )
164
168
// For *coverage* testing we also set `convertReqRes` to ensure
165
169
// createEcsPinoOptions includes a `formatters.log` function.
166
170
const log = pino (
167
171
{ ...ecsFormat ( { convertErr : false , convertReqRes : true } ) } ,
168
172
stream )
169
173
170
174
log . info ( { err : 42 } , 'hi' )
171
- const rec = recs [ 0 ]
175
+ const rec = JSON . parse ( lines [ 0 ] )
172
176
t . ok ( validate ( rec ) )
173
- t . equal ( ecsLoggingValidate ( rec ) , null )
177
+ t . equal ( ecsLoggingValidate ( lines [ 0 ] , { ignoreIndex : true } ) , null )
174
178
t . equal ( rec . err , 42 , 'rec.err is unchanged' )
175
179
t . equal ( rec . error , undefined , 'no rec.error is set' )
176
180
t . end ( )
@@ -180,14 +184,14 @@ test('createEcsPinoOptions with no formatters.log', t => {
180
184
// There is a supposed fast path in createEcsPinoOptions where formatters.log
181
185
// is excluded. Since convertErr is true by default, this case is rare.
182
186
// For coverage testing, we concoct that case here.
183
- const recs = [ ]
184
- const stream = split ( JSON . parse ) . on ( 'data' , rec => { recs . push ( rec ) } )
187
+ const lines = [ ]
188
+ const stream = split ( ) . on ( 'data' , line => { lines . push ( line ) } )
185
189
const log = pino ( { ...ecsFormat ( { convertErr : false } ) } , stream )
186
190
187
191
log . info ( { err : 42 , req : 'my req' , res : null } , 'hi' )
188
- const rec = recs [ 0 ]
192
+ const rec = JSON . parse ( lines [ 0 ] )
189
193
t . ok ( validate ( rec ) )
190
- t . equal ( ecsLoggingValidate ( rec ) , null )
194
+ t . equal ( ecsLoggingValidate ( lines [ 0 ] , { ignoreIndex : true } ) , null )
191
195
t . equal ( rec . err , 42 )
192
196
t . equal ( rec . req , 'my req' )
193
197
t . equal ( rec . res , null )
0 commit comments