Skip to content

Commit 7ed9b57

Browse files
committed
NR-318119 added a way to extract type of OSLog and fixed timestamp format
1 parent 49e5243 commit 7ed9b57

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

Agent/Public/NRLogger.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ withAttributes:(NSDictionary *)attributes;
138138
withMessage:(NSString *)message
139139
withAgentLogsOn:(BOOL)agentLogsOn;
140140

141-
+ (void) logMessage:(NSString *) message
142-
withTimestamp:(NSNumber *) timestamp;
141+
+ (void) log:(unsigned int)level
142+
withMessage:(NSString *)message
143+
withTimestamp:(NSNumber *)timestamp;
143144

144145
/*!
145146
Configure the amount of information the New Relic agent outputs about its internal operation.

Agent/Utilities/NRAutoLogCollector.m

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ + (void) readAndParseLogFile {
7979
// Process each log entry
8080
for (NSString *logEntry in newLogEntries) {
8181
if ([logEntry length] > 0) {
82-
[NRLogger logMessage:logEntry withTimestamp:[NRAutoLogCollector extractTimestamp:logEntry]];
82+
[NRLogger log:[NRAutoLogCollector extractType:logEntry] withMessage:logEntry withTimestamp:[NRAutoLogCollector extractTimestamp:logEntry]];
8383
}
8484
}
8585
}
@@ -107,11 +107,39 @@ + (NSNumber *) extractTimestamp:(NSString *) inputString {
107107
if ([NRAutoLogCollector isValidTimestamp:(timestampString)]) {
108108
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
109109
formatter.numberStyle = NSNumberFormatterDecimalStyle;
110-
return [formatter numberFromString:timestampString];
110+
NSNumber* originalTimestamp = [formatter numberFromString:timestampString];
111+
double timestampInSeconds = [originalTimestamp doubleValue];
112+
long long timestampInMilliseconds = (long long)(timestampInSeconds * 1000);
113+
return [NSNumber numberWithLongLong:timestampInMilliseconds];
111114
}
112115
}
113116

114117
return nil;
115118
}
116119

120+
+ (unsigned int) extractType:(NSString *) inputString {
121+
// Define the regular expression pattern to match the type: value
122+
NSString *pattern = @"type:\"([^\"]+)\"";
123+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
124+
125+
// Find matches in the input string
126+
NSTextCheckingResult *match = [regex firstMatchInString:inputString options:0 range:NSMakeRange(0, [inputString length])];
127+
128+
if (match) {
129+
// Extract the matched type value
130+
NSRange typeRange = [match rangeAtIndex:1];
131+
NSString *typeString = [inputString substringWithRange:typeRange];
132+
if([typeString compare:@"Info"] == NSOrderedSame || [typeString compare:@"Default"] == NSOrderedSame){
133+
return NRLogLevelInfo;
134+
} else if([typeString compare:@"Debug"] == NSOrderedSame){
135+
return NRLogLevelDebug;
136+
} else if([typeString compare:@"Error"] == NSOrderedSame || [typeString compare:@"Fault"] == NSOrderedSame){
137+
return NRLogLevelError;
138+
}
139+
}
140+
141+
return NRLogLevelNone;
142+
}
143+
144+
117145
@end

Agent/Utilities/NRLogger.m

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,26 @@ + (void)log:(unsigned int)level
124124
}
125125
}
126126

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

130132
if((timestamp <= 0) || (timestamp == nil)){
131133
timestamp = [NSNumber numberWithLongLong: (long long)([[NSDate date] timeIntervalSince1970] * 1000.0)];
132134
}
133-
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
134-
timestamp, NRLogMessageTimestampKey,
135-
message, NRLogMessageMessageKey,
136-
nil] :TRUE];
135+
if (level > 0){
136+
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
137+
[self levelToString:level], NRLogMessageLevelKey,
138+
timestamp, NRLogMessageTimestampKey,
139+
message, NRLogMessageMessageKey,
140+
nil] :TRUE];
141+
} else {
142+
[logger addLogMessage:[NSDictionary dictionaryWithObjectsAndKeys:
143+
timestamp, NRLogMessageTimestampKey,
144+
message, NRLogMessageMessageKey,
145+
nil] :TRUE];
146+
}
137147
}
138148

139149
+ (NRLogLevels) logLevels {

0 commit comments

Comments
 (0)