CocoaLumberjack
Hi!
I’m
Kristian
@ksmandersen
CocoaLumberjack
Why?
•

NSLog is inflexible

•

NSLog is slow

•

NSLog has no levels
Performance
“[…] I switched out all the logging statements
from using NSLog to use CocoaLumberjacks DDLog
instead, and saw...
Performance
“[…] I switched out all the logging statements
from using NSLog to use CocoaLumberjacks DDLog
instead, and saw...
Performance
1. Open a new connection to ASL daemon
2. Send message to ASL daemon
3. Close that connection
4. Write the sam...
CocoaLumberjack
•

Multiple outputs

•

Different logging levels

•

Much faster!

•

Very flexible
Getting started

[DDLog addLogger:[DDTTYLogger sharedInstance]];
LEVELS
DDLogError(@"Some error %@, %@", error, error.userInfo);
DDLogWarn(@"Unexpected thing");
DDLogInfo(@"Heads up here"...
Replacing NSLOG
// Making NSLog use Cocoa Lumberjack
#define NSLog DDLogInfo
Replacing NSLOG
// Making NSLog use Cocoa Lumberjack
#define NSLog DDLogInfo
Colors

https://github.com/robbiehanson/XcodeColors
AFNetworking
AFNetworkActivityLogger *logger =
[AFNetworkActivityLogger sharedLogger];
!

[logger setLevel:AFLoggerLevelIn...
AFNetworking
GET http://example.com/foo/bar.json
200 http://example.com/foo/bar.json
!

POST http://example.com/other/url....
Crashlytics

[DDLog addLogger:[CrashlyticsLogger sharedInstance]];
Formatters
DDLogError(@"Paper Jam!");
DDLogWarn(@"Low toner.");
DDLogInfo(@"Doc printed.");
DDLogDebug(@"Debugging");
DDLo...
Formatters
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage
{
NSString *logLevel;
switch (logMessage->logFlag)
{
...
Per logger levels

[DDLog addLogger:[DDTTYLogger sharedInstance]
withLogLevel:LOG_LEVEL_INFO];
PER CLASS LEVEL
+ (NSArray *)registeredClasses;
+ (NSArray *)registeredClassNames;
!

+ (int)logLevelForClass:(Class)aClas...
App broken w/ no
crash
App broken w/ no
crash
Rolling Logs
DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
!

[fileLogger setRollingFrequency:60 * 60 * 24];
[fi...
DEMO
Questions?
@ksmandersen
jobs.robo.cat
Introduction to CocoaLumberjack
Introduction to CocoaLumberjack
Upcoming SlideShare
Loading in...5
×

Introduction to CocoaLumberjack

2,934

Published on

Presented at Cocoaheads Copenhagen on 13 Februrary 2014

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,934
On Slideshare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
15
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Introduction to CocoaLumberjack

  1. 1. CocoaLumberjack
  2. 2. Hi! I’m Kristian @ksmandersen
  3. 3. CocoaLumberjack
  4. 4. Why? • NSLog is inflexible • NSLog is slow • NSLog has no levels
  5. 5. Performance “[…] I switched out all the logging statements from using NSLog to use CocoaLumberjacks DDLog instead, and saw an increase in performance of almost 6x […]”
  6. 6. Performance “[…] I switched out all the logging statements from using NSLog to use CocoaLumberjacks DDLog instead, and saw an increase in performance of almost 6x […]”
  7. 7. Performance 1. Open a new connection to ASL daemon 2. Send message to ASL daemon 3. Close that connection 4. Write the same message to STDERR 5. for each NSLog statement: GOTO 1;
  8. 8. CocoaLumberjack • Multiple outputs • Different logging levels • Much faster! • Very flexible
  9. 9. Getting started [DDLog addLogger:[DDTTYLogger sharedInstance]];
  10. 10. LEVELS DDLogError(@"Some error %@, %@", error, error.userInfo); DDLogWarn(@"Unexpected thing"); DDLogInfo(@"Heads up here"); DDLogDebug(@"Bug bug debug"); DDLogVerbose(@"Extreme verbosity");
  11. 11. Replacing NSLOG // Making NSLog use Cocoa Lumberjack #define NSLog DDLogInfo
  12. 12. Replacing NSLOG // Making NSLog use Cocoa Lumberjack #define NSLog DDLogInfo
  13. 13. Colors https://github.com/robbiehanson/XcodeColors
  14. 14. AFNetworking AFNetworkActivityLogger *logger = [AFNetworkActivityLogger sharedLogger]; ! [logger setLevel:AFLoggerLevelInfo]]; [logger startLogging];
  15. 15. AFNetworking GET http://example.com/foo/bar.json 200 http://example.com/foo/bar.json ! POST http://example.com/other/url.json 503 http://example.com/other/url.json
  16. 16. Crashlytics [DDLog addLogger:[CrashlyticsLogger sharedInstance]];
  17. 17. Formatters DDLogError(@"Paper Jam!"); DDLogWarn(@"Low toner."); DDLogInfo(@"Doc printed."); DDLogDebug(@"Debugging"); DDLogVerbose(@"Init doc_parse"); // // // // // E W I D V | | | | | Paper Jam! Low toner. Doc printed. Debugging Init doc_parse.
  18. 18. Formatters - (NSString *)formatLogMessage:(DDLogMessage *)logMessage { NSString *logLevel; switch (logMessage->logFlag) { case LOG_FLAG_ERROR : logLevel = @"E"; break; case LOG_FLAG_WARN : logLevel = @"W"; break; case LOG_FLAG_INFO : logLevel = @"I"; break; case LOG_FLAG_DEBUG : logLevel = @"D"; break; default : logLevel = @"V"; break; } return [NSString stringWithFormat:@"%@ | %@n", logLevel, logMessage->logMsg]; }
  19. 19. Per logger levels [DDLog addLogger:[DDTTYLogger sharedInstance] withLogLevel:LOG_LEVEL_INFO];
  20. 20. PER CLASS LEVEL + (NSArray *)registeredClasses; + (NSArray *)registeredClassNames; ! + (int)logLevelForClass:(Class)aClass; + (int)logLevelForClassWithName:(NSString *)aClassName; ! + (void)setLogLevel:(int)logLevel forClass:(Class)aClass; + (void)setLogLevel:(int)logLevel forClassWithName:(NSString *)aClassName;
  21. 21. App broken w/ no crash
  22. 22. App broken w/ no crash
  23. 23. Rolling Logs DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; ! [fileLogger setRollingFrequency:60 * 60 * 24]; [fileLogger setMaximumFileSize:1024 * 1024 * 2]; [fileLogger.logFileManager setMaximumNumberOfLogFiles:7]; [DDLog addLogger:fileLogger];
  24. 24. DEMO
  25. 25. Questions? @ksmandersen
  26. 26. jobs.robo.cat
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×