您的位置:首页 > 其它

CocoaLumberjack 学习总结(五)

2016-03-09 17:14 381 查看

Introduction

Formatters may optionally be added to any logger. For example, a formatter which prepends the log level (error, warn, info, etc) to log messages being written to a file. To achieve this you may simply create and add a formatter to a file
logger. Using formatters gives you the ability to customize the log message appearance without having to rewrite your log messages (or any component of the logging framework).

Formatters can also be used to filter log messages. That is, you can determine that certain log messages be excluded from a particular logger. For example, you could have a verbose console log, but a concise log file by filtering all but
errors and warnings going to a file logger. The criteria for filtering is entirely up to you.

And remember that formatters are applied individually to loggers. So you can format and/or filter on a per-logger basis.

Details

It is incredibly simple to create your own custom formatter. The protocol for
DDLogFormatter
is defined in
DDLog.h
, and there is only a single required method:

@protocol DDLogFormatter <NSObject>
@required

/**
* Formatters may optionally be added to any logger.
* This allows for increased flexibility in the logging environment.
* For example, log messages for log files may be formatted differently than log messages for the console.
*
* For more information about formatters, see the "Custom Formatters" page:
* Documentation/CustomFormatters.md
*
* The formatter may also optionally filter the log message by returning nil,
* in which case the logger will not log the message.
**/
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage;

@optional
// ...
@end


It's pretty straight-forward. The single method takes, as a parameter, an instance of
DDLogMessage
which contains all the information related to the log message including:

message
- original log message
file
- full path to the file the log message came from
fileName
- name of the file the log message came from (without extension)
function
- method the log message came from
line
- line number in file where the log message came from
timestamp
- when the log message was executed
level
- log level of message (bitmask of flags, e.g. 0111)
flag
- log flag of message (individual flag that allowed log message to fire, e.g. 0010)
threadID
- which thread issued the log message
queueLabel
- name of gcd queue (if applicable)
Let's write a simple formatter that automatically simply prepends the log level before every log message.

The idea is to get log messages like this:

DDLogError(@"Paper Jam!");       // E | Paper Jam!
DDLogWarn(@"Low toner.");        // W | Low toner.
DDLogInfo(@"Doc printed.");      // I | Doc printed.
DDLogDebug(@"Debugging");        // D | Debugging
DDLogVerbose(@"Init doc_parse"); // V | Init doc_parse.


MyCustomFormatter.h

#import <Foundation/Foundation.h>
#import "DDLog.h"

@interface MyCustomFormatter : NSObject <DDLogFormatter>

@end


MyCustomFormatter.m

#import "MyCustomFormatter.h"

@implementation MyCustomFormatter

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
NSString *logLevel;
switch (logMessage->_flag) {
case DDLogFlagError    : logLevel = @"E"; break;
case DDLogFlagWarning  : logLevel = @"W"; break;
case DDLogFlagInfo     : logLevel = @"I"; break;
case DDLogFlagDebug    : logLevel = @"D"; break;
default                : logLevel = @"V"; break;
}

return [NSString stringWithFormat:@"%@ | %@\n", logLevel, logMessage->_message];
}

@end


Now, just add the custom formatter to your logger:

[DDTTYLogger sharedInstance].logFormatter = [[MyCustomFormatter alloc] init];

注意这里的
timestamp
后面需要使用formatter格式化,否则显示时间不是本地精确时间。
- (id)init {
    if((self = [super init])) {
        self.formatter = [[NSDateFormatter alloc] init];
        [self.formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    }
    return self;
}

- (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
    self.timestamp = [self.formatter stringFromDate:(logMessage.timestamp)];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: