Skip to content

Instantly share code, notes, and snippets.

@iamamused
Created April 13, 2016 13:55
Show Gist options
  • Save iamamused/4656ee84ef2fde592d5f746cf942480a to your computer and use it in GitHub Desktop.
Save iamamused/4656ee84ef2fde592d5f746cf942480a to your computer and use it in GitHub Desktop.
CocoaLumberjack with colours and line numbers, common config
#import "AppDelegate.h"
// install XcodeColors plugin https://github.com/robbiehanson/XcodeColors
// install KZLinkedConsole plugin https://github.com/krzysztofzablocki/KZLinkedConsole
// Include cocoalumberjack in your pods
// pod 'CocoaLumberjack', '~> 1'
// NOTE this will work for version 2 as well, just need to tweak it.
#import <CocoaLumberjack/DDASLLogger.h>
#import <CocoaLumberjack/DDTTYLogger.h>
#import <CocoaLumberjack/DDFileLogger.h>
// Formatter to add file/line numbers
@interface LineNumberLogFormatter : NSObject<DDLogFormatter>
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage;
@end
@implementation LineNumberLogFormatter
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage
{
NSString *path = [NSString stringWithCString:logMessage->file encoding:NSASCIIStringEncoding];
NSString *fileName = [path lastPathComponent];
return [NSString stringWithFormat:@"%@:%d %@", fileName, logMessage->lineNumber, logMessage->logMsg];
}
@end
@interface AppDelegate()
@end
@implementation GCAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
#ifdef DEBUG
[DDTTYLogger sharedInstance].logFormatter = [[LineNumberLogFormatter alloc] init];
[[DDTTYLogger sharedInstance] setColorsEnabled:YES];
UIColor *pink = [UIColor colorWithRed:(255 / 255.0)green:(58 / 255.0)blue:(159 / 255.0)alpha:1.0];
[[DDTTYLogger sharedInstance] setForegroundColor:pink backgroundColor:nil forFlag:LOG_FLAG_INFO];
UIColor *black = [UIColor colorWithWhite:0.404 alpha:1.000];
[[DDTTYLogger sharedInstance] setForegroundColor:black backgroundColor:nil forFlag:LOG_FLAG_VERBOSE];
#endif
DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 5;
[DDLog addLogger:fileLogger];
// your code
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment