#import "CocoaLumberjack.h"
pch中添加代码
static const NSUInteger ddLogLevel = DDLogLevelAll;//多个宏可供选择
[DDLogaddLogger:[DDASLLoggersharedInstance]];
//发送日志语句到Xcode控制台
[DDLogaddLogger:[DDTTYLoggersharedInstance]];
// Enable Colors 使用带颜色的打印
[[DDTTYLoggersharedInstance]setColorsEnabled:YES];
//自定义打印颜色
[[DDTTYLoggersharedInstance]setForegroundColor:[UIColorblueColor]backgroundColor:[UIColorgreenColor]forFlag:DDLogFlagVerbose];
[[DDTTYLoggersharedInstance]setForegroundColor:[UIColorwhiteColor]backgroundColor:[UIColorpurpleColor]forFlag:DDLogFlagInfo];
/**
把日志语句发送至文件 默认路径为Library/Caches/Logs
获取文件路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * logPath = [docPath stringByAppendingPathComponent:@"Caches"];
logPath = [logPath stringByAppendingPathComponent:@"Logs"];
*/
DDFileLogger *fileLogger = [[DDFileLoggeralloc] init];
//设置保存一个星期
fileLogger.rollingFrequency =60 * 60 *24; // 24 hour rolling;
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLogaddLogger:fileLogger];
DDLogError(<#frmt, ...#>) 错误
DDLogInfo(<#frmt, ...#>) 警告
DDLogInfo(<#frmt, ...#>) 提示
DDLogVerbose(<#frmt, ...#>) 详细信息
就会执行打印,但是这事打印是没有颜色的.需要安装XcodeColors插件来配合使用.成功安装之后如果打印还是没有颜色,就要配置一下xcode的环境.参考文章:http://www.cnblogs.com/bomo/p/4671516.html?utm_source=tuicool.这里不再详细说明了.
4.自定义增加打印信息内容可以在pch文件中这样写:
#define LCLogError(错误信息, ...) DDLogError((@"错误信息 : \n%s [Line %d] \n" 错误信息), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LCLogWarn(警告, ...) DDLogWarn((@"警告 : \n%s [Line %d] \n" 警告), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LCLogInfo(提示信息, ...) DDLogInfo((@"提示信息 :\n %s [Line %d] \n" 提示信息), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LCLogVerbose(详细信息, ...) DDLogVerbose((@"详细信息 : \n%s [Line %d]\n" 详细信息), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
- (NSArray*)getLogFileMessage
{
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES)objectAtIndex:0];
NSString * logPath = [docPath stringByAppendingPathComponent:@"Caches"];
logPath = [logPathstringByAppendingPathComponent:@"Logs"];
NSFileManager * fileManger = [NSFileManagerdefaultManager];
NSError * error = nil;
NSArray * fileList = [[NSArrayalloc]init];
fileList = [fileMangercontentsOfDirectoryAtPath:logPath error:&error];
NSMutableArray * listArray = [[NSMutableArrayalloc]init];
for (NSString * oneLogPathin fileList)
{
if([oneLogPath hasSuffix:@".log"])
{
NSString * truePath = [logPath stringByAppendingPathComponent:oneLogPath];
NSLog(@"%@",truePath);
NSString *logFileMessage= [[NSStringalloc]initWithContentsOfFile:truePathencoding:NSUTF8StringEncodingerror:nil];
[listArrayaddObject:exceptionInfo];
}
}
NSLog(@"%@",listArray);
return listArray;
}