Skip to content

Commit

Permalink
NR-318119 added a way to extract type of OSLog and fixed timestamp fo…
Browse files Browse the repository at this point in the history
…rmat
  • Loading branch information
mbruin-NR committed Oct 17, 2024
1 parent 49e5243 commit 7ed9b57
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Agent/Public/NRLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ withAttributes:(NSDictionary *)attributes;
withMessage:(NSString *)message
withAgentLogsOn:(BOOL)agentLogsOn;

+ (void) logMessage:(NSString *) message
withTimestamp:(NSNumber *) timestamp;
+ (void) log:(unsigned int)level
withMessage:(NSString *)message
withTimestamp:(NSNumber *)timestamp;

/*!
Configure the amount of information the New Relic agent outputs about its internal operation.
Expand Down
32 changes: 30 additions & 2 deletions Agent/Utilities/NRAutoLogCollector.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ + (void) readAndParseLogFile {
// Process each log entry
for (NSString *logEntry in newLogEntries) {
if ([logEntry length] > 0) {
[NRLogger logMessage:logEntry withTimestamp:[NRAutoLogCollector extractTimestamp:logEntry]];
[NRLogger log:[NRAutoLogCollector extractType:logEntry] withMessage:logEntry withTimestamp:[NRAutoLogCollector extractTimestamp:logEntry]];
}
}
}
Expand Down Expand Up @@ -107,11 +107,39 @@ + (NSNumber *) extractTimestamp:(NSString *) inputString {
if ([NRAutoLogCollector isValidTimestamp:(timestampString)]) {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
return [formatter numberFromString:timestampString];
NSNumber* originalTimestamp = [formatter numberFromString:timestampString];
double timestampInSeconds = [originalTimestamp doubleValue];
long long timestampInMilliseconds = (long long)(timestampInSeconds * 1000);
return [NSNumber numberWithLongLong:timestampInMilliseconds];
}
}

return nil;
}

+ (unsigned int) extractType:(NSString *) inputString {
// Define the regular expression pattern to match the type: value
NSString *pattern = @"type:\"([^\"]+)\"";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];

// Find matches in the input string
NSTextCheckingResult *match = [regex firstMatchInString:inputString options:0 range:NSMakeRange(0, [inputString length])];

if (match) {
// Extract the matched type value
NSRange typeRange = [match rangeAtIndex:1];
NSString *typeString = [inputString substringWithRange:typeRange];
if([typeString compare:@"Info"] == NSOrderedSame || [typeString compare:@"Default"] == NSOrderedSame){
return NRLogLevelInfo;
} else if([typeString compare:@"Debug"] == NSOrderedSame){
return NRLogLevelDebug;
} else if([typeString compare:@"Error"] == NSOrderedSame || [typeString compare:@"Fault"] == NSOrderedSame){
return NRLogLevelError;
}
}

return NRLogLevelNone;
}


@end
20 changes: 15 additions & 5 deletions Agent/Utilities/NRLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,26 @@ + (void)log:(unsigned int)level
}
}

+ (void) logMessage:(NSString *) message withTimestamp:(NSNumber *) timestamp {
+ (void) log:(unsigned int)level
withMessage:(NSString *) message
withTimestamp:(NSNumber *) timestamp {
NRLogger *logger = [NRLogger logger];

if((timestamp <= 0) || (timestamp == nil)){
timestamp = [NSNumber numberWithLongLong: (long long)([[NSDate date] timeIntervalSince1970] * 1000.0)];
}
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
timestamp, NRLogMessageTimestampKey,
message, NRLogMessageMessageKey,
nil] :TRUE];
if (level > 0){
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
[self levelToString:level], NRLogMessageLevelKey,
timestamp, NRLogMessageTimestampKey,
message, NRLogMessageMessageKey,
nil] :TRUE];
} else {
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
timestamp, NRLogMessageTimestampKey,
message, NRLogMessageMessageKey,
nil] :TRUE];
}
}

+ (NRLogLevels) logLevels {
Expand Down

0 comments on commit 7ed9b57

Please sign in to comment.