基于第三方CocoaLumberjack(DDLog)作保存不一样分类的日志

简介

  CocoaLumberjack(如下简称:DDLog)是一个能够在iOS和Mac开发中使用的日志库,强大又不失灵活。集成进项目后,配置下,而后用DDLog语句简单地取代NSLog语句( DDLog的使用方法和NSLog同样)。
并且DDLog拥有基本的DDTTYLogger输出控制台、DDASLLogger输出到Console.app、DDFileLogger输出到文件、实现DDAbstractDatabaseLogger保存到数据库。git

问题

  当时公司有个需求,就是把不一样的日志上传到后台,而且须要分类上传。这时候我发现DDLog是能够自定义日志和自定义路径。而后就写了一个根据不一样flag来分别保存不一样路径的日志模块Demo来使用。
github

上一段关键代码例子:

CSFileLogger.h数据库

@interface CSFileLogger : DDFileLogger
@property (nonatomic, assign) NSUInteger flag;
- (instancetype)initWithFlag:(NSUInteger)flag;
@end

@interface CSFileManagerDefault : DDLogFileManagerDefault
- (instancetype)initWithLogsDirectory:(NSString *)logsDirectory
                             fileName:(NSString *)name;
@end

复制代码

CSFileLogger.mbash

@implementation CSFileLogger
- (NSString *)loggerName
{
    return [NSString stringWithFormat:@"cs.ddlog.fileLogger"];
}

- (instancetype)initWithFlag:(NSUInteger)flag{    
    //新建一个文件夹去保存
    _flag = flag;
    NSString *logsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Library/Caches/%ld",(long)flag]];
    CSFileManagerDefault *defaultLogFileManager = [[CSFileManagerDefault alloc] initWithLogsDirectory:logsDirectory fileName:@"cslogger"];
    return [self initWithLogFileManager:defaultLogFileManager];
}
@end

@interface CSFileManagerDefault()
@property (nonatomic, strong) NSString *fileName;
@end
@implementation CSFileManagerDefault

- (instancetype)initWithLogsDirectory:(NSString *)logsDirectory
                             fileName:(NSString *)name {
    //logsDirectory日志自定义路径
    self = [super initWithLogsDirectory:logsDirectory];
    if (self) {
        self.fileName = name;
    }
    return self;
}

#pragma mark - Override methods
- (NSString *)newLogFileName {
    //重写文件名称
    NSDateFormatter *dateFormatter = [self logFileDateFormatter];
    NSString *formattedDate = [dateFormatter stringFromDate:[NSDate date]];
    return [NSString stringWithFormat:@"%@-%@.log", self.fileName, formattedDate];
}

- (NSDateFormatter *)logFileDateFormatter {
    
    //获取当前线程的字典
    NSMutableDictionary *dictionary = [[NSThread currentThread]
                                       threadDictionary];
    //设置日期格式
    NSString *dateFormat = @"yyyy'-'MM'-'dd'";
    NSString *key = [NSString stringWithFormat:@"logFileDateFormatter.%@", dateFormat];
    NSDateFormatter *dateFormatter = dictionary[key];
    
    if (dateFormatter == nil) {
        //设置日期格式
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zh_CN"]];
        [dateFormatter setDateFormat:dateFormat];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        dictionary[key] = dateFormatter;
    }
    
    return dateFormatter;
}

@end
复制代码

最后将自定义的Logger加到DDLog里面作保存文件app

+ (id<DDLogger>)createCSFileLogger:(NSInteger)flag
{
    //添加DDLog 容许自定义的log类型白名单
    CSContextWhitelistFilterLogFormatter *contextFilter = [[CSContextWhitelistFilterLogFormatter alloc] init];
    [contextFilter addToWhitelist:flag];
    CSFileLogger *customLogger = [[CSFileLogger alloc] initWithFlag:flag];
    [customLogger setLogFormatter:contextFilter];
    customLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
    customLogger.logFileManager.maximumNumberOfLogFiles = 10;
    return customLogger;
}

[DDLog addLogger:[CSLoggerAssembler createCSFileLogger:1000]];
[DDLog addLogger:[CSLoggerAssembler createCSFileLogger:1001]];
复制代码

   这样就会根据外面注册的flag来分别保存到"Library/Caches/1000/"和"Library/Caches/1001/"的文件路径之下了。
  好简单地就作好了一个demo了。但愿你们能够有用吧。
Demo连接: https://github.com/KoonChaoSo/CSLogger ide

相关文章
相关标签/搜索