Skip to content

Commit

Permalink
RollbarCocoaLumberjackLogger: prototype is code complete
Browse files Browse the repository at this point in the history
  • Loading branch information
akornich committed Mar 2, 2022
1 parent a8b1988 commit 4f2998c
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ - (void)logMessage:(DDLogMessage *)logMessage {

if (logMsg) {
// let's send it to Rollbar:
[self->_rollbarLogger log:[RollbarCocoaLumberjackLogger convert:logMessage]
[self->_rollbarLogger log:[RollbarCocoaLumberjackLogger convertDDLogFlagToRollbarLevel:logMessage.flag]
message:logMsg
data:nil
data:[RollbarCocoaLumberjackLogger dataWithDDLogMessage:logMessage]
context:@"RollbarCocoaLumberjackLogger"
];
}
Expand All @@ -53,11 +53,106 @@ - (void)logMessage:(DDLogMessage *)logMessage {
+ (NSDictionary<NSString *, id> *)dataWithDDLogMessage:(DDLogMessage *)logMessage {

return @{
//TODO: complete the data mapping...
@"DDLogMessage.message":
logMessage.message,
@"DDLogMessage.level":
[RollbarCocoaLumberjackLogger convertDDLogLevelToString:logMessage.level],
@"DDLogMessage.flag":
[RollbarCocoaLumberjackLogger convertDDLogFlagToString:logMessage.flag],
@"DDLogMessage.context":
[NSNumber numberWithInteger:logMessage.context],
@"DDLogMessage.file":
logMessage.file,
@"DDLogMessage.fileName":
logMessage.fileName,
@"DDLogMessage.function":
(nil != logMessage.function) ? logMessage.function : @"<nil>",
@"DDLogMessage.line":
[NSNumber numberWithUnsignedInteger:logMessage.line],
@"DDLogMessage.representedObject":
(nil != logMessage.representedObject) ? [logMessage.representedObject description] : @"<nil>",
@"DDLogMessage.options":
[RollbarCocoaLumberjackLogger convertDDLogMessageOptionsToString:logMessage.options],
@"DDLogMessage.timestamp":
logMessage.timestamp.description, // !!!!!!!!!!!! *********
@"DDLogMessage.threadID":
logMessage.threadID,
@"DDLogMessage.threadName":
(nil != logMessage.threadName) ? logMessage.threadName : @"<nil>",
@"DDLogMessage.queueLabel":
logMessage.queueLabel,
@"DDLogMessage.qos":
[NSNumber numberWithUnsignedInteger:logMessage.qos],
// @"DDLogMessage.tag":
// (nil != logMessage.tag) ? [logMessage.tag description] : @"<nil>",
};
}

+ (RollbarLevel) convert:(DDLogFlag) ddLogFlag {
+ (NSString *)convertDDLogMessageOptionsToString:(DDLogMessageOptions) options {

NSString *stringValue = [NSString string];
if (options & DDLogMessageCopyFile) {
if (stringValue.length > 0) {
stringValue = [stringValue stringByAppendingString:@" | "];
}
stringValue = [stringValue stringByAppendingString:@"DDLogMessageCopyFile"];
}
if (options & DDLogMessageCopyFunction) {
if (stringValue.length > 0) {
stringValue = [stringValue stringByAppendingString:@" | "];
}
stringValue = [stringValue stringByAppendingString:@"DDLogMessageCopyFunction"];
}
if (options & DDLogMessageDontCopyMessage) {
if (stringValue.length > 0) {
stringValue = [stringValue stringByAppendingString:@" | "];
}
stringValue = [stringValue stringByAppendingString:@"DDLogMessageDontCopyMessage"];
}
return stringValue;
}

+ (NSString *)convertDDLogLevelToString:(DDLogLevel) logLevel {

switch(logLevel) {
case DDLogLevelOff:
return @"DDLogLevelOff";
case DDLogLevelAll:
return @"DDLogLevelAll";
case DDLogLevelInfo:
return @"DDLogLevelError";
case DDLogLevelError:
return @"DDLogLevelError";
case DDLogLevelDebug:
return @"DDLogLevelDebug";
case DDLogLevelWarning:
return @"DDLogLevelWarning";
case DDLogLevelVerbose:
return @"DDLogLevelVerbose";
default:
return @"Unexpected_DDLogLevel_value";
}
}

+ (NSString *)convertDDLogFlagToString:(DDLogFlag) ddLogFlag {

switch(ddLogFlag) {
case DDLogFlagError:
return @"DDLogFlagError";
case DDLogFlagWarning:
return @"DDLogFlagWarning";
case DDLogFlagInfo:
return @"DDLogFlagInfo";
case DDLogFlagDebug:
return @"DDLogFlagDebug";
case DDLogFlagVerbose:
return @"DDLogFlagVerbose";
default:
return @"Unexpected_DDLogFlag_value";
}
}

+ (RollbarLevel) convertDDLogFlagToRollbarLevel:(DDLogFlag) ddLogFlag {

switch(ddLogFlag) {
case DDLogFlagError:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
// Copyright (c) 2018 Rollbar, Inc. All rights reserved.

#import <XCTest/XCTest.h>
#import "../../../UnitTests/RollbarUnitTestSettings.h"
#import "RollbarTestUtil.h"

#define LOG_LEVEL_DEF ddLogLevel
//#import <CocoaLumberjack/CocoaLumberjack.h>
@import CocoaLumberjack;

@import RollbarCocoaLumberjack;

static const DDLogLevel ddLogLevel = DDLogLevelDebug;

@interface RollbarCocoaLumberjackTests : XCTestCase
@end

@implementation RollbarCocoaLumberjackTests

- (void)testBasics {
- (void)setUp {

[super setUp];

// Put setup code here. This method is called before the invocation of each test method in the class.
NSLog(@"Set to go...");

RollbarClearLogFile();

[DDLog addLogger:[DDOSLogger sharedInstance]];

DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 1;
// he above code tells the application to keep a day worth of log files on the system.

[DDLog addLogger:fileLogger];
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
NSLog(@"Teared down.");
}

- (void)testLiveRollbarCocoaLumberjackBasics {


DDLogError(@"Broken sprocket detected!");
DDLogVerbose(@"User selected file:%@ withSize:%u", @"somewhere/file.ext", 100);

RollbarConfig *config = [[RollbarConfig alloc] init];
config.destination.accessToken = ROLLBAR_UNIT_TEST_PAYLOADS_ACCESS_TOKEN;
config.destination.environment = ROLLBAR_UNIT_TEST_ENVIRONMENT;
config.developerOptions.transmit = YES;
config.developerOptions.logPayload = YES;
config.loggingOptions.maximumReportsPerMinute = 5000;

[DDLog addLogger:[RollbarCocoaLumberjackLogger createWithRollbarConfig:config]];

DDLogDebug(@"*** Via CocoaLumberjack!!!");

[NSThread sleepForTimeInterval:5.0f];
}

- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}

@end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2018 Rollbar, Inc. All rights reserved.

#import <Foundation/Foundation.h>

@import RollbarNotifier;

void RollbarClearLogFile(void);
NSArray* RollbarReadLogItemFromFile(void);
void RollbarFlushFileThread(RollbarLogger *logger);

@interface RollbarLogger (Tests)

- (NSThread *)_rollbarThread;
- (void)_test_doNothing;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2018 Rollbar, Inc. All rights reserved.

#import "RollbarTestUtil.h"
#import "RollbarFileReader.h"
#import "RollbarCachesDirectory.h"

static NSString *QUEUED_ITEMS_FILE_NAME = @"rollbar.items";
static NSString *QUEUED_ITEMS_STATE_FILE_NAME = @"rollbar.state";
static NSString *QUEUED_TELEMETRY_ITEMS_FILE_NAME = @"rollbar.telemetry";

NSString* _logFilePath() {

NSString *cachesDirectory = [RollbarCachesDirectory directory];
return [cachesDirectory stringByAppendingPathComponent:QUEUED_ITEMS_FILE_NAME];
}

void RollbarClearLogFile() {

NSString *filePath = _logFilePath();
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:filePath];

if (fileExists) {
BOOL success = [fileManager removeItemAtPath:filePath
error:&error];
if (!success) {
NSLog(@"Error: %@", [error localizedDescription]);
}
[[NSFileManager defaultManager] createFileAtPath:filePath
contents:nil
attributes:nil];
}
}

NSArray* RollbarReadLogItemFromFile() {

NSString *filePath = _logFilePath();
RollbarFileReader *reader = [[RollbarFileReader alloc] initWithFilePath:filePath
andOffset:0];

NSMutableArray *items = [NSMutableArray array];
[reader enumerateLinesUsingBlock:^(NSString *line, NSUInteger nextOffset, BOOL *stop) {
NSMutableDictionary *payload =
[NSJSONSerialization JSONObjectWithData:[line dataUsingEncoding:NSUTF8StringEncoding]
options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves)
error:nil
];


if (!payload) {
return;
}

NSMutableDictionary *data = payload[@"data"];
[items addObject:data];
}];

return items;
}

void RollbarFlushFileThread(RollbarLogger *logger) {

[logger performSelector:@selector(_test_doNothing)
onThread:[logger _rollbarThread] withObject:nil waitUntilDone:YES];
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
//
// DTOsTests.m
// Rollbar
//
// Created by Andrey Kornich on 2019-10-10.
// Copyright © 2019 Rollbar. All rights reserved.
//


@import Foundation;

#if !TARGET_OS_WATCH
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// Test.m
//
//
// Created by Andrey Kornich on 2021-01-04.
//

#import <XCTest/XCTest.h>

@import Foundation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// Test.m
//
//
// Created by Andrey Kornich on 2021-05-06.
//

#import <XCTest/XCTest.h>
@import RollbarCommon;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// RollbarPredicateBuilderTests.m
//
//
// Created by Andrey Kornich on 2021-04-29.
//

#import <XCTest/XCTest.h>

@import Foundation;
Expand Down
2 changes: 2 additions & 0 deletions RollbarNotifier/Sources/RollbarNotifier/RollbarLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,8 @@ - (BOOL)checkPayloadResponse:(NSURLResponse *)response
error:(NSError *)error
data:(NSData *)data {

NSLog(@"HTTP response from Rollbar: %@", response);

// Lookup rate limiting headers and adjust reporting rate accordingly:
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *httpHeaders = [httpResponse allHeaderFields];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#import "RollbarCachesDirectory.h"

static NSString *QUEUED_ITEMS_FILE_NAME = @"rollbar.items";
static NSString *QUEUED_ITEMS_STATE_FILE_NAME = @"rollbar.state";
static NSString *QUEUED_TELEMETRY_ITEMS_FILE_NAME = @"rollbar.telemetry";

NSString* _logFilePath() {

Expand Down

0 comments on commit 4f2998c

Please sign in to comment.