IOS development: log framework CocoaLumberjack

Source: Internet
Author: User

IOS development: log framework CocoaLumberjack

CocoaLumberjack is a fast, simple, powerful, and flexible log framework on Mac and iOS. CocoaLumberjack is similar to a popular log framework (such as log4j), but it is designed for Objective-C and utilizes multithreading, GCD (if available) the dynamic characteristics of the lockless atomic operation Objective-C runtime.

Fast

In most cases, Lumberjack is an order of magnitude faster than NSLog.

Simple

When the application starts, you can configure Lumberjack with only one line of code. Then, replace the NSLog statement with the DDLog statement. In addition, the DDLog macro and NSLog have the same format and syntax, so it is super simple.

Powerful

A log statement can be sent to multiple logger, which means you can record files and the console at the same time. In addition, you can create your own logger to send log statements to the network, database, or distributed file system. There are no restrictions.

Flexibility

Configure the log framework you want. Modify the log level of each file (especially during testing ). Modify the log level of each logger (detailed console, but concise log file ). Modify the log level of each Xcode configuration. Customizes the number of log levels for your applications. Add your own detailed logs. Modify the Log Level dynamically during running. Select how and when to roll back your log files. Upload the log file to the central server. Compress archived log files to save hard disk space.

When you encounter a problem, you can choose the Lumberjack framework:

1. You want to find a way to track bugs that are not reproducible in the program;

2. You are very disappointed with the brief log on the iPhone;

3. To support the system and stability, you want to upgrade your application to the next level;

4. Search for enterprise-level log solutions for your applications (Mac or iPhone.

How to start using the Lumberjack framework

Start

Start using the CocoaLumberjack framework in three steps:

1. Add the Lumberjack file to your project;

2. Configuration framework;

3. Convert the NSLog command to the Lumberjack macro command;

Add the Lumberjack framework to your project

There are four main files to be added:

1. @ DDLog (the basis of the entire framework)

2. @ DDASLLogger (send log statements to Apple's log system so that they are displayed on the Console. app)

3. @ DDTTYLoyger (send the log statement to the Xcode console, if available)

4. @ DDFIleLoger (send log statements to files)

DDLog is mandatory, and the rest are optional, depending on how you plan to use this framework. For example, if you do not want to record a file, you can skip DDFileLogger, or you want to skip ASL for faster file record, you can skip DDASLLoger.

Configuration framework

First, you want to configure this log framework in your application, usually in the applicationDidFinishLaunching method.

At the beginning, you need the following two lines of code:

[DDLog addLogger: [DDASLLogger sharedInstance];

[DDLog addLogger: [DDTTYLogger sharedInstance];

This will add two "logger" in your log framework ". That is to say, your log statement will be sent to the Console. app and Xcode Console (like the standard NSLog)

One of the advantages of this framework is its flexibility. If you want to write your log statements into a file, you can add and configure a file logger:

FileLogger = [[DDFileLogger alloc] init];

FileLogger. rollingFrequency = 60*60*24; // 24 hour rolling

FileLogger. logFileManager. maximumNumberOfLogFiles = 7;

[DDLog addLogger: fileLogger];

The above code tells the application to keep the log file for one week on the system.

Replace NSLog statements with DDLog

The header file of DDLog defines the macro you used to replace the NSLog statement. In essence, it looks like the following:

// Convert from this:

NSLog (@ "Broken sprocket detected! ");

NSLog (@ "User selected file: % @ withSize: % u", filePath, fileSize );

// To this:

DDLogError (@ "Broken sprocket detected! ");

DDLogVerbose (@ "User selected file: % @ withSize: % u", filePath, fileSize );

We can see that the syntax of the DDLog macro and NSLog is exactly the same.

So all you need to do is determine the log level of each NSlog statement. By default, DDLog has four levels of logs:

1. @ DDlogError

2. @ DDlogWarn

3. @ DDlogInfo

4. @ DDlogVerbose

(Note: You can also customize the level and level name or add more precise control to replace the four simple levels of the system .)

Of course, the NSLog statement you choose depends on the severity of your message.

The following different log levels may be what you need:

1. If you set the log level to LOG_LEVEL_ERROR, you will only see the DDlogError statement.

2. If you set the log level to LOG_LEVEL_WARN, you will only see the DDLogError and DDLogWarn statements.

3. If you set the log level to LOG_LEVEL_INFO, you will see the error, Warn, and Info statements.

4. If you set the log level to LOG_LEVEL_VERBOSE, you will see all DDLog statements.

5. If you set the log level to LOG_LEVEL_OFF, you will not see any DDLog statements.

Where can I set the log level? Can I use only one log level for the entire project?

Of course not. We all know that, just like debugging or adding new features, if you want to record the part that is currently being done in detail, the Lumberjack frame provides debugging Control for each file, you can only modify the Log Level of the edited file.

(Note: of course there are many other advanced options, such as the Global log level, every configuration level of Xcode, and every logger level, which we will talk about in another article ).

Here is how to convert your log statements:

// CONVERT FROM THIS

# Import "Sprocket. h"

@ Implementation Sprocket

-(Void) someMethod

{

NSLog (@ "Meet George Jetson ");

}

@ End

// TO THIS

# Import "Sprocket. h"

# Import "DDLog. h"

Static const int ddLogLevel = LOG_LEVEL_VERBOSE;

@ Implementation Sprocket

-(Void) someMethod

{

DDLogVerbose (@ "Meet George Jetson ");

}

@ End

Note that the log level is declared as a constant, which means that all the DDLog statements above the log level threshold will be compiled into your project.

Automatic Reference count (ARC)

Lumberjack of the latest version uses ARC. If your project does not use ARC, you can learn on the ARC page how to correctly mark the Lumberjack file like ARC in Xcode.

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.