25
25
#import " NRAutoLogCollector.h"
26
26
#import < os/log.h>
27
27
28
+ @interface NRLogger ()
29
+ + (NRLogger *)logger ;
30
+ - (NSMutableDictionary *) commonBlockDict ;
31
+ @end
32
+
28
33
@implementation NRLoggerTests
29
34
- (void ) setUp
30
35
{
@@ -69,8 +74,6 @@ - (void) setUp
69
74
weakSelf.fileDescriptor = 0 ;
70
75
}
71
76
});
72
-
73
-
74
77
}
75
78
- (void ) tearDown
76
79
{
@@ -108,20 +111,40 @@ - (void) testNRLogger {
108
111
109
112
sleep (5 );
110
113
111
- NSError * error;
114
+ NSError * error = nil ;
112
115
NSData * logData = [NRLogger logFileData: &error];
113
116
if (error){
114
117
NSLog (@" %@ " , error.localizedDescription );
115
118
}
116
119
117
- NSString * logMessagesJson = [NSString stringWithFormat: @" [ %@ ]" , [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
120
+ NSMutableDictionary *commonBlock = [[NRLogger logger ] commonBlockDict ];
121
+
122
+ NSData *json = [NRMAJSON dataWithJSONObject: commonBlock
123
+ options: 0
124
+ error: &error];
125
+
126
+ if (error) {
127
+ NRLOG_AGENT_ERROR (@" Failed to create log payload w error = %@ " , error);
128
+ XCTAssertNil (error, @" Error creating log payload" );
129
+ return ;
130
+ }
131
+
132
+ error = nil ;
133
+ // New version of the line
134
+ NSString * logMessagesJson = [NSString stringWithFormat: @" [{ \" common\" : { \" attributes\" : %@ }, \" logs\" : [ %@ ] }]" ,
135
+ [[NSString alloc ] initWithData: json encoding: NSUTF8StringEncoding],
136
+ [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
137
+
118
138
NSData * formattedData = [logMessagesJson dataUsingEncoding: NSUTF8StringEncoding];
119
139
120
- NSArray * decode = [NSJSONSerialization JSONObjectWithData: formattedData
140
+ NSDictionary * decode = [NSJSONSerialization JSONObjectWithData: formattedData
121
141
options: 0
122
- error: nil ];
142
+ error: &error ];
123
143
NSLog (@" decode=%@ " , decode);
124
144
145
+ NSArray *decodedArray = [[decode valueForKey: @" logs" ] objectAtIndex: 0 ];
146
+ NSDictionary *decodedCommonBlock = [[[decode valueForKey: @" common" ] objectAtIndex: 0 ] valueForKey: @" attributes" ];
147
+
125
148
NSArray * expectedValues = @[
126
149
@{@" message" : @" Info Log..." },
127
150
@{@" message" : @" Error Log..." },
@@ -131,31 +154,19 @@ - (void) testNRLogger {
131
154
@{@" message" : @" Debug Log..." },
132
155
@{@" message" : @" This is a test message for the New Relic logging system." },
133
156
];
157
+
134
158
// check for existence of 6 logs.
135
159
int foundCount = 0 ;
136
160
// For each expected message.
137
161
for (NSDictionary *dict in expectedValues) {
138
162
// Iterate through the collected message logs.
139
- for (NSDictionary *dict2 in decode ) {
163
+ for (NSDictionary *dict2 in decodedArray ) {
140
164
//
141
165
NSString * currentMessage = [dict objectForKey: @" message" ];
166
+
167
+ // Check the logs entries
142
168
if ([[dict2 objectForKey: @" message" ] isEqualToString: currentMessage]) {
143
169
foundCount += 1 ;
144
- XCTAssertTrue ([[dict2 objectForKey: @" entity.guid" ] isEqualToString: @" Entity-Guid-XXXX" ],@" entity.guid set incorrectly" );
145
- XCTAssertTrue ([[dict2 objectForKey: NRLogMessageInstrumentationProviderKey] isEqualToString: NRLogMessageMobileValue],@" instrumentation provider set incorrectly" );
146
- XCTAssertTrue ([[dict2 objectForKey: NRLogMessageInstrumentationVersionKey] isEqualToString: @" DEV" ],@" instrumentation name set incorrectly" );
147
-
148
- #if TARGET_OS_WATCH
149
- XCTAssertTrue ([[dict2 objectForKey: NRLogMessageInstrumentationNameKey] isEqualToString: @" watchOSAgent" ],@" instrumentation name set incorrectly" );
150
- #else
151
- if ([[[UIDevice currentDevice ] systemName ] isEqualToString: @" tvOS" ]) {
152
- XCTAssertTrue ([[dict2 objectForKey: NRLogMessageInstrumentationNameKey] isEqualToString: @" tvOSAgent" ],@" instrumentation name set incorrectly" );
153
-
154
- }
155
- else {
156
- XCTAssertTrue ([[dict2 objectForKey: NRLogMessageInstrumentationNameKey] isEqualToString: @" iOSAgent" ],@" instrumentation name set incorrectly" );
157
- }
158
- #endif
159
170
}
160
171
// Verify added attributes with logAttributes.
161
172
if ([[dict2 objectForKey: @" message" ] isEqualToString: @" This is a test message for the New Relic logging system." ]) {
@@ -166,6 +177,22 @@ - (void) testNRLogger {
166
177
}
167
178
168
179
XCTAssertEqual (foundCount, 7 , @" Seven messages should be found." );
180
+
181
+ // Verify Common Block
182
+ XCTAssertTrue ([[decodedCommonBlock objectForKey: @" entity.guid" ] isEqualToString: @" Entity-Guid-XXXX" ],@" entity.guid set incorrectly" );
183
+ XCTAssertTrue ([[decodedCommonBlock objectForKey: NRLogMessageInstrumentationProviderKey] isEqualToString: NRLogMessageMobileValue],@" instrumentation provider set incorrectly" );
184
+ XCTAssertTrue ([[decodedCommonBlock objectForKey: NRLogMessageInstrumentationVersionKey] isEqualToString: @" DEV" ],@" instrumentation name set incorrectly" );
185
+
186
+ #if TARGET_OS_WATCH
187
+ XCTAssertTrue ([[decodedCommonBlock objectForKey: NRLogMessageInstrumentationNameKey] isEqualToString: @" watchOSAgent" ],@" instrumentation name set incorrectly" );
188
+ #else
189
+ if ([[[UIDevice currentDevice ] systemName ] isEqualToString: @" tvOS" ]) {
190
+ XCTAssertTrue ([[decodedCommonBlock objectForKey: NRLogMessageInstrumentationNameKey] isEqualToString: @" tvOSAgent" ],@" instrumentation name set incorrectly" );
191
+ }
192
+ else {
193
+ XCTAssertTrue ([[decodedCommonBlock objectForKey: NRLogMessageInstrumentationNameKey] isEqualToString: @" iOSAgent" ],@" instrumentation name set incorrectly" );
194
+ }
195
+ #endif
169
196
}
170
197
171
198
@@ -223,13 +250,34 @@ - (void) testRemoteLogLevels {
223
250
if (error){
224
251
NSLog (@" %@ " , error.localizedDescription );
225
252
}
226
- NSString * logMessagesJson = [NSString stringWithFormat: @" [ %@ ]" , [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
253
+ NSMutableDictionary *commonBlock = [[NRLogger logger ] commonBlockDict ];
254
+
255
+ NSData *json = [NRMAJSON dataWithJSONObject: commonBlock
256
+ options: 0
257
+ error: &error];
258
+
259
+ if (error) {
260
+ NRLOG_AGENT_ERROR (@" Failed to create log payload w error = %@ " , error);
261
+ XCTAssertNil (error, @" Error creating log payload" );
262
+ return ;
263
+ }
264
+
265
+ error = nil ;
266
+ // New version of the line
267
+ NSString * logMessagesJson = [NSString stringWithFormat: @" [{ \" common\" : { \" attributes\" : %@ }, \" logs\" : [ %@ ] }]" ,
268
+ [[NSString alloc ] initWithData: json encoding: NSUTF8StringEncoding],
269
+ [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
270
+
227
271
NSData * formattedData = [logMessagesJson dataUsingEncoding: NSUTF8StringEncoding];
228
- NSArray * decode = [NSJSONSerialization JSONObjectWithData: formattedData
272
+
273
+ NSDictionary * decode = [NSJSONSerialization JSONObjectWithData: formattedData
229
274
options: 0
230
- error: nil ];
275
+ error: &error ];
231
276
NSLog (@" decode=%@ " , decode);
232
277
278
+ NSArray *decodedArray = [[decode valueForKey: @" logs" ] objectAtIndex: 0 ];
279
+ NSDictionary *decodedCommonBlock = [[[decode valueForKey: @" common" ] objectAtIndex: 0 ] valueForKey: @" attributes" ];
280
+
233
281
NSArray * expectedValues = @[
234
282
@{@" message" : @" Info Log..." },
235
283
@{@" message" : @" Error Log..." },
@@ -244,12 +292,11 @@ - (void) testRemoteLogLevels {
244
292
// For each expected message.
245
293
for (NSDictionary *dict in expectedValues) {
246
294
// Iterate through the collected message logs.
247
- for (NSDictionary *dict2 in decode ) {
295
+ for (NSDictionary *dict2 in decodedArray ) {
248
296
//
249
297
NSString * currentMessage = [dict objectForKey: @" message" ];
250
298
if ([[dict2 objectForKey: @" message" ] isEqualToString: currentMessage]) {
251
299
foundCount += 1 ;
252
- XCTAssertTrue ([[dict2 objectForKey: @" entity.guid" ] isEqualToString: @" Entity-Guid-XXXX" ],@" entity.guid set incorrectly" );
253
300
}
254
301
// Verify added attributes with logAttributes.
255
302
if ([[dict2 objectForKey: @" message" ] isEqualToString: @" This is a test message for the New Relic logging system." ]) {
@@ -314,13 +361,34 @@ - (void) testLocalLogLevels {
314
361
if (error){
315
362
NSLog (@" %@ " , error.localizedDescription );
316
363
}
317
- NSString * logMessagesJson = [NSString stringWithFormat: @" [ %@ ]" , [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
364
+ NSMutableDictionary *commonBlock = [[NRLogger logger ] commonBlockDict ];
365
+
366
+ NSData *json = [NRMAJSON dataWithJSONObject: commonBlock
367
+ options: 0
368
+ error: &error];
369
+
370
+ if (error) {
371
+ NRLOG_AGENT_ERROR (@" Failed to create log payload w error = %@ " , error);
372
+ XCTAssertNil (error, @" Error creating log payload" );
373
+ return ;
374
+ }
375
+
376
+ error = nil ;
377
+ // New version of the line
378
+ NSString * logMessagesJson = [NSString stringWithFormat: @" [{ \" common\" : { \" attributes\" : %@ }, \" logs\" : [ %@ ] }]" ,
379
+ [[NSString alloc ] initWithData: json encoding: NSUTF8StringEncoding],
380
+ [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
381
+
318
382
NSData * formattedData = [logMessagesJson dataUsingEncoding: NSUTF8StringEncoding];
319
- NSArray * decode = [NSJSONSerialization JSONObjectWithData: formattedData
383
+
384
+ NSDictionary * decode = [NSJSONSerialization JSONObjectWithData: formattedData
320
385
options: 0
321
- error: nil ];
386
+ error: &error ];
322
387
NSLog (@" decode=%@ " , decode);
323
388
389
+ NSArray *decodedArray = [[decode valueForKey: @" logs" ] objectAtIndex: 0 ];
390
+ NSDictionary *decodedCommonBlock = [[[decode valueForKey: @" common" ] objectAtIndex: 0 ] valueForKey: @" attributes" ];
391
+
324
392
NSArray * expectedValues = @[
325
393
@{@" message" : @" Info Log..." },
326
394
@{@" message" : @" Error Log..." },
@@ -335,12 +403,11 @@ - (void) testLocalLogLevels {
335
403
// For each expected message.
336
404
for (NSDictionary *dict in expectedValues) {
337
405
// Iterate through the collected message logs.
338
- for (NSDictionary *dict2 in decode ) {
406
+ for (NSDictionary *dict2 in decodedArray ) {
339
407
//
340
408
NSString * currentMessage = [dict objectForKey: @" message" ];
341
409
if ([[dict2 objectForKey: @" message" ] isEqualToString: currentMessage]) {
342
410
foundCount += 1 ;
343
- XCTAssertTrue ([[dict2 objectForKey: @" entity.guid" ] isEqualToString: @" Entity-Guid-XXXX" ],@" entity.guid set incorrectly" );
344
411
}
345
412
// Verify added attributes with logAttributes.
346
413
if ([[dict2 objectForKey: @" message" ] isEqualToString: @" This is a test message for the New Relic logging system." ]) {
@@ -400,13 +467,35 @@ - (void) testAutoCollectedLogs {
400
467
if (error){
401
468
NSLog (@" %@ " , error.localizedDescription );
402
469
}
403
- NSString * logMessagesJson = [NSString stringWithFormat: @" [ %@ ]" , [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
470
+ NSMutableDictionary *commonBlock = [[NRLogger logger ] commonBlockDict ];
471
+
472
+ NSData *json = [NRMAJSON dataWithJSONObject: commonBlock
473
+ options: 0
474
+ error: &error];
475
+
476
+ if (error) {
477
+ NRLOG_AGENT_ERROR (@" Failed to create log payload w error = %@ " , error);
478
+ XCTAssertNil (error, @" Error creating log payload" );
479
+ return ;
480
+ }
481
+
482
+ error = nil ;
483
+ // New version of the line
484
+ NSString * logMessagesJson = [NSString stringWithFormat: @" [{ \" common\" : { \" attributes\" : %@ }, \" logs\" : [ %@ ] }]" ,
485
+ [[NSString alloc ] initWithData: json encoding: NSUTF8StringEncoding],
486
+ [[NSString alloc ] initWithData: logData encoding: NSUTF8StringEncoding]];
487
+
404
488
NSData * formattedData = [logMessagesJson dataUsingEncoding: NSUTF8StringEncoding];
405
- NSArray * decode = [NSJSONSerialization JSONObjectWithData: formattedData
489
+
490
+ NSDictionary * decode = [NSJSONSerialization JSONObjectWithData: formattedData
406
491
options: 0
407
- error: nil ];
492
+ error: &error ];
408
493
NSLog (@" decode=%@ " , decode);
409
494
495
+ NSArray *decodedArray = [[decode valueForKey: @" logs" ] objectAtIndex: 0 ];
496
+ NSDictionary *decodedCommonBlock = [[[decode valueForKey: @" common" ] objectAtIndex: 0 ] valueForKey: @" attributes" ];
497
+
498
+
410
499
NSArray * expectedValues = @[
411
500
@{@" message" : @" NSLog Test" },
412
501
@{@" message" : @" This is a default os_log message." },
@@ -419,12 +508,11 @@ - (void) testAutoCollectedLogs {
419
508
// For each expected message.
420
509
for (NSDictionary *dict in expectedValues) {
421
510
// Iterate through the collected message logs.
422
- for (NSDictionary *dict2 in decode ) {
511
+ for (NSDictionary *dict2 in decodedArray ) {
423
512
//
424
513
NSString * currentMessage = [dict objectForKey: @" message" ];
425
514
if ([[dict2 objectForKey: @" message" ] containsString: currentMessage]) {
426
515
foundCount += 1 ;
427
- XCTAssertTrue ([[dict2 objectForKey: @" entity.guid" ] isEqualToString: @" Entity-Guid-XXXX" ],@" entity.guid set incorrectly" );
428
516
}
429
517
// Verify added attributes with logAttributes.
430
518
if ([[dict2 objectForKey: @" message" ] isEqualToString: @" This is a test message for the New Relic logging system." ]) {
0 commit comments