Complete logging solution with CocoaLumberJack for iOS applications - I
Client device logs can be a great asset to make your iOS app bug-free. Apart from helping you debug by seeing the console logs, ability to get client device logs should be part of your logging solution.
There are other capabilities that are expected to be present in your logging framework. For example, performance, ability to perform logging asynchronously and ability to customize each log format. If you want to read about these in detail, you should go through Ditching NSLog.
After using a couple of third party logging libraries, I have ended up using CocoaLumberjack with my own customizations.
Getting started - Replacement for
This brief guide is meant to help you quickly get started. To learn more, you should definitely check out the documentation.
CocoaLumberjack in place of
NSLog will make logging more performant, asynchronous and will open it for further customizations.
- Update Podfile
- CocoaLumberjack allows you to declare
ddLogLevelin each file to have file based logging level. Global level logging should work fine for most people. Create a
Constants.hequivalent file to define global
// // Constants.h #import <CocoaLumberjack/DDLog.h> static const int ddLogLevel = LOG_LEVEL_VERBOSE;
<solution-name>-Prefix.pch to import
Constants.h in all files
- Initializing logger
Set up the logger in
[DDLog addLogger:[DDASLLogger sharedInstance]]; [DDLog addLogger:[DDTTYLogger sharedInstance]];
- At this point, you should be ready to replace
NSLog. The replacements for
#define DDLogError(frmt, ...) #define DDLogWarn(frmt, ...) #define DDLogInfo(frmt, ...) #define DDLogDebug(frmt, ...) #define DDLogVerbose(frmt, ...)
DDLog macros have the same exact syntax as
NSLog so, you could just find and replace
Next posts in this segment will cover
- Logging with custom formatting to include
- Getting client device logs