iOS Development: Log frame Cocoalumberjack

Source: Internet
Author: User
Tags compress archive

Cocoalumberjack is a fast, simple, powerful, and flexible log framework on Mac and iOS. Cocoalumberjack is similar to the popular log framework (such as log4j), but it is designed for objective-c, leveraging the dynamic characteristics of multithreading, GCD (if available), unlocked atomic operation Objective-c Runtime.

Fast

In most use cases, lumberjack is an order of magnitude faster than nslog.

Simple

When the application is started, you can configure lumberjack with just one line of code. The Ddlog statement is then used to simply replace the NSLog statement. and Ddlog macros and NSLog have exactly the same format and syntax, so it's super simple.

Powerful

A log statement can be sent to multiple logger, meaning that you can log files and consoles at the same time. In addition, you can create your own logger to send a log statement to a network, database, or Distributed file system. Without any restrictions.

Flexibility

Configure the log frame you want. Modify the log level of each file (especially when testing). Modify the log level for each logger (detailed console, but compact log file). Modify the log level for each Xcode configuration. Customize the number of log levels for your application. Add your own fine log. Dynamically modify the log level at run time. Choose how and when to roll back your log files. Upload the log file to the hub server. Compress archive log files to save hard disk space.

When you encounter a situation, you can choose the Lumberjack frame:

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

2. You are disappointed with the short log on the iphone;

3. You want to upgrade your application to the next level for support of system and stability needs;

4. Find an enterprise-class log solution for your application (Mac or iphone).

How to start using the Lumberjack framework

Begin

Three steps to start using the Cocoalumberjack framework:

1. Add the Lumberjack file to your project;

2. Configuration framework;

3. Convert NSLog instruction to use lumberjack macro instruction;

Add the Lumberjack frame to your project

There are four main files that you need to add:

1. @DDLog (Basis of the entire framework)

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

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

4. @DDFIleLoger (send the LOG statement to the file)

Ddlog is mandatory and the rest is optional, depending on how you intend to use the framework. For example, if you're not going to record a file, you can skip the Ddfilelogger, or you want to skip ASL so that the file records are faster, you can skip Ddaslloger.

Configuration framework

First, you want to configure this log framework in your application, which is typically configured in the Applicationdidfinishlaunching method.

To begin, you need the following two lines of code:

[Ddlog Addlogger:[ddasllogger Sharedinstance]];

[Ddlog Addlogger:[ddttylogger Sharedinstance]];

This will add two "logger" to your log frame. That means your log statements will be sent to the Console.app and Xcode console (like Standard nslog).

One of the benefits of this framework is its flexibility, and if you still want your log statements written to a file, you can add and configure a filename logger:

FileLogger = [[Ddfilelogger alloc] init];

filelogger.rollingfrequency = 60 * 60 * 24; Hour rolling

FileLogger.logFileManager.maximumNumberOfLogFiles = 7;

[Ddlog Addlogger:filelogger];

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

Replace NSLog statement with Ddlog

The Ddlog header file defines the macro you use to replace the NSLog statement, which essentially looks like this:

Convert from:

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 see that the syntax for Ddlog macros and NSLog is exactly the same.

So all you have to do is decide what kind of log level each NSLog statement belongs to. The Ddlog default has four levels of logging, respectively:

1. @DDlogError

2. @DDlogWarn

3. @DDlogInfo

4. @DDlogVerbose

(Note: You can also customize levels and level names or add finer controls to replace the four simple levels of the system.) )

Of course choosing which NSLog statement depends on the severity of your message.

Here are a few of the different log levels that you might 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 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 the ddlog statements.

5. If you set the log level to Log_level_off, you will not see any ddlog statements.

So where do I set the log level, I can only use one log level throughout the project?

Of course not, we all know just as debugging or adding new features, if you want to detail the part that is currently being done, the Lumberjack Framework provides debugging control over each file, and you can only modify the log level of the files in the edit.

(Note: Of course there are many other advanced options, such as the global log level, each configuration level for Xcode, each logger level, etc., which we'll cover in another article).

Here's 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 The log level is declared as a constant, which means that this means that the Ddlog statement above the log level threshold will be compiled into your project.

Automatic reference count (ARC)

The latest version of Lumberjack uses arc. If your project doesn't use arc, you can learn how to mark the Lumberjack file correctly in Xcode as ARC does in the Arc page.

More about Lumberjack:

Automatically use different log levels for your debug vs release builds

Tailor the log levels to suite your needs

Filter logs based on logger settings

Write your own custom formatters

Write your own custom loggers

And more ...

Original: Cocoalumberjack

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.