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 filelogger. 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 forDDLogFormatteris 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
DDLogMessagewhich 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)]; }
相关文章推荐
- Activiti工作总结(二)
- 移动用户体验设计:iOS APP体验设计
- Spring实战 第一章 1.4 Spring 中的新内容
- Android中Volley的Request的自定义
- 【poj2125】Destroying The Graph 二分图最小点权覆盖集
- jedis调用redis之分页
- 进程控制开发
- JSON 之FastJson解析
- java中float和double的区别
- 编程之美---代理模式
- 为什么要在linux命令前加上 ./ ?
- Hash 表算法解析
- linux shell 管道命令(pipe)使用及与shell重定向区别
- 防止mysql注入
- swift表情emoji的转换
- Flex 布局
- 全局大喇叭,详解广播机制
- libsvm-mat在MATLAB平台下的安装
- Android异步任务之AsyncTask源码解析
- android中textview字数过长解决方法