Skip to content

Commit 2fb2fd6

Browse files
NR-335442: NRLogger: fix the log:inFile methods to check for remote log level. (#325)
* NRLogger: fix the log:inFile methods to check for remote log level. Fix up NRLoggerTests * Remove unused form of log:inFile
1 parent 1617259 commit 2fb2fd6

File tree

3 files changed

+113
-58
lines changed

3 files changed

+113
-58
lines changed

Agent/Public/NRLogger.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,6 @@ typedef enum _NRLogTargets {
117117

118118
}
119119

120-
+ (void)log:(unsigned int)level
121-
inFile:(NSString *)file
122-
atLine:(unsigned int)line
123-
inMethod:(NSString *)method
124-
withMessage:(NSString *)message;
125-
126120
+ (void)log:(unsigned int)level
127121
inFile:(NSString *)file
128122
atLine:(unsigned int)line

Agent/Utilities/NRLogger.m

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#define kNRMAMaxLogUploadRetry 3
2020

2121
@interface NRLogger()
22-
- (void)addLogMessage:(NSDictionary *)message;
22+
- (void)addLogMessage:(NSDictionary *)message : (BOOL) agentLogsOn;
2323
- (void)setLogLevels:(unsigned int)levels;
2424
- (void)setRemoteLogLevel:(unsigned int)level;
2525

@@ -37,32 +37,6 @@ + (NRLogger *)logger {
3737
return _nr_logger;
3838
}
3939

40-
+ (void)log:(unsigned int)level
41-
inFile:(NSString *)file
42-
atLine:(unsigned int)line
43-
inMethod:(NSString *)method
44-
withMessage:(NSString *)message {
45-
46-
NRLogger *logger = [NRLogger logger];
47-
BOOL shouldLog = NO;
48-
49-
// This shouldLog BOOL was previously set within a @synchronized block but I was seeing a deadlock. Trying some tests without
50-
// @synchronized(logger) {
51-
shouldLog = (logger->logLevels & level) != 0;
52-
// }
53-
54-
if (shouldLog) {
55-
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
56-
[self levelToString:level], NRLogMessageLevelKey,
57-
file, NRLogMessageFileKey,
58-
[NSNumber numberWithUnsignedInt:line], NRLogMessageLineNumberKey,
59-
method, NRLogMessageMethodKey,
60-
[NSNumber numberWithLongLong: (long long)([[NSDate date] timeIntervalSince1970] * 1000.0)], NRLogMessageTimestampKey,
61-
message, NRLogMessageMessageKey,
62-
nil]: NO];
63-
}
64-
}
65-
6640
+ (void)log:(unsigned int)level
6741
inFile:(NSString *)file
6842
atLine:(unsigned int)line
@@ -71,14 +45,11 @@ + (void)log:(unsigned int)level
7145
withAttributes:(NSDictionary *)attributes {
7246

7347
NRLogger *logger = [NRLogger logger];
74-
BOOL shouldLog = NO;
75-
76-
// This shouldLog BOOL was previously set within a @synchronized block but I was seeing a deadlock. Trying some tests without
77-
// @synchronized(logger) {
78-
shouldLog = (logger->logLevels & level) != 0;
79-
// }
80-
81-
if (shouldLog) {
48+
49+
BOOL shouldRemoteLog = (logger->remoteLogLevel & level) != 0;
50+
BOOL shouldLog =(logger->logLevels & level) != 0;
51+
52+
if (shouldLog || shouldRemoteLog) {
8253
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
8354
[self levelToString:level], NRLogMessageLevelKey,
8455
file, NRLogMessageFileKey,
@@ -99,18 +70,12 @@ + (void)log:(unsigned int)level
9970
withAgentLogsOn:(BOOL)agentLogsOn {
10071

10172
NRLogger *logger = [NRLogger logger];
102-
BOOL shouldLog = NO;
10373

74+
BOOL shouldRemoteLog = (logger->remoteLogLevel & level) != 0;
10475
// Filter passed logs by log level.
105-
shouldLog = (logger->logLevels & level) != 0;
106-
107-
// Filtering of Console logs is performed based on logLevel.
108-
// // If this is an agentLog, only print it if we are currently including the debug level.
109-
// if (agentLogsOn) {
110-
// shouldLog = (logger->logLevels & NRLogLevelDebug) != 0;
111-
// }
112-
113-
if (shouldLog) {
76+
BOOL shouldLog = (logger->logLevels & level) != 0;
77+
78+
if (shouldLog || shouldRemoteLog) {
11479
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
11580
[self levelToString:level], NRLogMessageLevelKey,
11681
file, NRLogMessageFileKey,
@@ -253,7 +218,13 @@ - (void)dealloc {
253218
- (void)addLogMessage:(NSDictionary *)message : (BOOL) agentLogsOn {
254219
// The static method checks the log level before we get here.
255220
dispatch_async(logQueue, ^{
256-
if (self->logTargets & NRLogTargetConsole) {
221+
222+
// Only enter this first block if local log level includes this level enabled.
223+
NSString *levelString = [message objectForKey:NRLogMessageLevelKey];
224+
NRLogLevels level = [NRLogger stringToLevel:levelString];
225+
BOOL shouldLog = (self->logLevels & level) != 0;
226+
227+
if ((self->logTargets & NRLogTargetConsole) && shouldLog) {
257228
NSLog(@"NewRelic(%@,%p):\t%@:%@\t%@\n\t%@",
258229
[NewRelicInternalUtils agentVersion],
259230
[NSThread currentThread],
@@ -264,8 +235,6 @@ - (void)addLogMessage:(NSDictionary *)message : (BOOL) agentLogsOn {
264235

265236
}
266237
// Only enter this block if remote logging is including this messages level.
267-
NSString *levelString = [message objectForKey:NRLogMessageLevelKey];
268-
NRLogLevels level = [NRLogger stringToLevel:levelString];
269238

270239
BOOL shouldRemoteLog = (self->remoteLogLevel & level) != 0;
271240

Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,100 @@ - (void) testNRLogger {
167167

168168
- (void) testRemoteLogLevels {
169169

170-
// Set the remote log level to warning.
171-
[NRLogger setRemoteLogLevel:NRLogLevelWarning];
170+
[NRLogger setLogLevels:NRLogLevelInfo];
171+
172+
// Set the remote log level to Debug.
173+
[NRLogger setRemoteLogLevel:NRLogLevelDebug];
174+
175+
XCTestExpectation *delayExpectation1 = [self expectationWithDescription:@"Waiting for Log Queue"];
176+
177+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
178+
[delayExpectation1 fulfill];
179+
});
180+
181+
[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
182+
if (error) {
183+
XCTFail(@"Timeout error");
184+
}
185+
}];
186+
187+
// Seven messages should reach the remote log file for upload.
188+
189+
[NewRelic logInfo: @"Info Log..."];
190+
[NewRelic logError: @"Error Log..."];
191+
[NewRelic logVerbose:@"Verbose Log..."];
192+
[NewRelic logWarning:@"Warning Log..."];
193+
[NewRelic logAudit: @"Audit Log..."];
194+
[NewRelic logDebug: @"Debug Log..."];
195+
[NewRelic logAttributes:@{
196+
@"logLevel": @"WARN",
197+
@"message": @"This is a test message for the New Relic logging system.",
198+
@"additionalAttribute1": @"attribute1",
199+
@"additionalAttribute2": @"attribute2"
200+
}];
201+
202+
XCTestExpectation *delayExpectation2 = [self expectationWithDescription:@"Waiting for Log Queue"];
203+
204+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
205+
[delayExpectation2 fulfill];
206+
});
207+
208+
[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
209+
if (error) {
210+
XCTFail(@"Timeout error");
211+
}
212+
}];
213+
214+
NSError* error;
215+
NSData* logData = [NRLogger logFileData:&error];
216+
if(error){
217+
NSLog(@"%@", error.localizedDescription);
218+
}
219+
NSString* logMessagesJson = [NSString stringWithFormat:@"[ %@ ]", [[NSString alloc] initWithData:logData encoding:NSUTF8StringEncoding]];
220+
NSData* formattedData = [logMessagesJson dataUsingEncoding:NSUTF8StringEncoding];
221+
NSArray* decode = [NSJSONSerialization JSONObjectWithData:formattedData
222+
options:0
223+
error:nil];
224+
NSLog(@"decode=%@", decode);
225+
226+
NSArray * expectedValues = @[
227+
@{@"message": @"Info Log..."},
228+
@{@"message": @"Error Log..."},
229+
@{@"message": @"Verbose Log..."},
230+
@{@"message": @"Warning Log..."},
231+
@{@"message": @"Audit Log..."},
232+
@{@"message": @"Debug Log..."},
233+
@{@"message": @"This is a test message for the New Relic logging system."},
234+
];
235+
// check for existence of 6 logs.
236+
int foundCount = 0;
237+
// For each expected message.
238+
for (NSDictionary *dict in expectedValues) {
239+
// Iterate through the collected message logs.
240+
for (NSDictionary *dict2 in decode) {
241+
//
242+
NSString* currentMessage = [dict objectForKey:@"message"];
243+
if ([[dict2 objectForKey:@"message"] isEqualToString: currentMessage]) {
244+
foundCount += 1;
245+
XCTAssertTrue([[dict2 objectForKey:@"entity.guid"] isEqualToString:@"Entity-Guid-XXXX"],@"entity.guid set incorrectly");
246+
}
247+
// Verify added attributes with logAttributes.
248+
if ([[dict2 objectForKey:@"message"] isEqualToString:@"This is a test message for the New Relic logging system."]) {
249+
XCTAssertTrue([[dict2 objectForKey:@"additionalAttribute1"] isEqualToString:@"attribute1"],@"additionalAttribute1 set incorrectly");
250+
XCTAssertTrue([[dict2 objectForKey:@"additionalAttribute2"] isEqualToString:@"attribute2"],@"additionalAttribute2 set incorrectly");
251+
}
252+
}
253+
}
254+
255+
XCTAssertEqual(foundCount, 7, @"Seven remote messages should be found.");
256+
}
257+
258+
- (void) testLocalLogLevels {
259+
260+
// Set the local log level to Debug
261+
[NRLogger setLogLevels:NRLogLevelDebug];
262+
// Set the remote log level to Info.
263+
[NRLogger setRemoteLogLevel:NRLogLevelInfo];
172264

173265
XCTestExpectation *delayExpectation1 = [self expectationWithDescription:@"Waiting for Log Queue"];
174266

@@ -182,7 +274,7 @@ - (void) testRemoteLogLevels {
182274
}
183275
}];
184276

185-
// Three messages should reach the remote log file for upload.
277+
// Seven messages should reach the remote log file for upload.
186278

187279
[NewRelic logInfo: @"Info Log..."];
188280
[NewRelic logError: @"Error Log..."];
@@ -250,7 +342,7 @@ - (void) testRemoteLogLevels {
250342
}
251343
}
252344

253-
XCTAssertEqual(foundCount, 3, @"Three remote messages should be found.");
345+
XCTAssertEqual(foundCount, 4, @"Four remote messages should be found.");
254346
}
255347

256348
@end

0 commit comments

Comments
 (0)