開発
Using Cocoalumberjack for Log
will
Xcode provides nice debug environments, so that developers can insert some breakpoints to tracking the running app. However, sometimes an app couldn’t test under the debug environment, for instance, the location based function, or serial communication function, which needs the exclusive USB to connect the serial cable.
There is a library called “Coacoalumberjack”[1], available in the Github. It is a fast and connivent tool to provides log for different level of message and different phases of development.
How to use it:
1. Import from Cocoapods
Cocoapods[2] provides the convenience to manage the 3rd-party library inside our projects.
Add the following text in the Podfile.
pod 'CocoaLumberjack', '~> 1.6'
Secondly, import the library by hitting in the terminal pod install.
Then, rebuild the Pods subproject.
2. Create a shared header to include the necessary library files.
To create a shared header can be convenient to use the function, naming “LoggerSharedHeader.h” .
#ifndef LoggerSharedHeader_h #define LoggerSharedHeader_h #import <CocoaLumberjack/DDLog.h> #import <CocoaLumberjack/DDFileLogger.h> #import <CocoaLumberjack/DDTTYLogger.h> #ifdef DEBUG static const int ddLogLevel = LOG_LEVEL_VERBOSE; #else static const int ddLogLevel = LOG_LEVEL_WARN; #endif #endif
3. Init the output source in the beginning of application start.
Include the shared header in the “AppDelegate.m”.
#import "GSLoggerSharedHeader.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //Init the Looger [DDLog addLogger:[DDTTYLogger sharedInstance]]; DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling fileLogger.logFileManager.maximumNumberOfLogFiles = 7; [DDLog addLogger:fileLogger]; DDLogVerbose(@"The application started"); //some other codes return YES; }
4. Log the message
Include the header file to the code where to output the log file.
Then using the log function to output the log message similar as NSLog. By using the following 4 option depends on the level of the log message.
- DDLogError
- DDLogWarn
- DDLogInfo
- DDLogVerbose
These tie into the log level just as you would expect
- If you set the log level to LOG_LEVEL_ERROR, then you will only see DDLogError statements.
- If you set the log level to LOG_LEVEL_WARN, then you will only see DDLogError and DDLogWarn statements.
- If you set the log level to LOG_LEVEL_INFO, you’ll see Error, Warn and Info statements.
- If you set the log level to LOG_LEVEL_VERBOSE, you’ll see all DDLog statements.
- If you set the log level to LOG_LEVEL_OFF, you won’t see any DDLog statements.
5. Check the log file
To check the log file, you need to open the organizer of Xcode, from “Device” >> “{you development device}” >> Application >> “{you development app}” >> download the application files
Then open the content of application files, from “AppData” >> “Library” >> “Caches” >> “Logs”, you can find the log text file.
Reference:
[1] The manual in the Github
[2] The information of Cocoapods in the Github