CocoaLumberjack iOS - 我们可以更改日志文件名和目录吗? [英] CocoaLumberjack iOS - Can we change the logfile name and directory?

查看:1417
本文介绍了CocoaLumberjack iOS - 我们可以更改日志文件名和目录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用CocoaLumberjack。我需要将日志文件的名称更改为我的自定义文件名。

I am using CocoaLumberjack in my project. I need to change the name of the logfile to my custom file name.

NSString * applicationDocumentsDirectory = [[[[NSFileManager defaultManager]
                                              URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
DDLogFileManagerDefault *documentsFileManager = [[DDLogFileManagerDefault alloc]
                                                 initWithLogsDirectory:applicationDocumentsDirectory];

DDFileLogger *fileLogger = [[DDFileLogger alloc]
                            initWithLogFileManager:documentsFileManager];    
// Configure File Logger
[fileLogger setMaximumFileSize:(1024 * 1024)];
[fileLogger setRollingFrequency:(3600.0 * 24.0)];
[[fileLogger logFileManager] setMaximumNumberOfLogFiles:1];
[DDLog addLogger:fileLogger];

通过上面的代码,我已将目录更改为Documents。但是现在我还需要更改日志文件名。我怎样才能做到这一点?有可能吗?

By the above code i have changed the directory to the Documents. But now i need to change the logfile name also. How can i achieve this? Is it possible?

推荐答案

虽然我认为我的回复可能为时已晚,但请在下面找到我的解决方案:

Although I believe that my reply might be too late, please find below my solution:

1)继承DDLogFileManagerDefault并覆盖方法:newLogFileName和isLogFile

1) Inherit DDLogFileManagerDefault and override methods: newLogFileName and isLogFile

#import "DDFileLogger.h"

@interface BaseLogFileManager : DDLogFileManagerDefault

@end

#import "BaseLogFileManager.h"

@implementation BaseLogFileManager

-(NSString *)newLogFileName {
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
    NSString *timeStamp = [self getTimestamp];

    return [NSString stringWithFormat:@"%@%@.log", appName, timeStamp];
}

-(BOOL)isLogFile:(NSString *)fileName {
    return NO;
}

-(NSString *)getTimestamp {
    static dispatch_once_t onceToken;
    static NSDateFormatter *dateFormatter;
    dispatch_once(&onceToken, ^{
        dateFormatter = [NSDateFormatter new];
        [dateFormatter setDateFormat:@"YYYY.MM.dd-HH.mm.ss"];
    });

    return [dateFormatter stringFromDate:NSDate.date];
}

@end

2)在AppDelegate中,更改下列行:

2) In AppDelegate, change following line:

DDLogFileManagerDefault *documentsFileManager = [[DDLogFileManagerDefault alloc] initWithLogsDirectory:applicationDocumentsDirectory];

TO:

DDLogFileManagerDefault *documentsFileManager = [[BaseLogFileManager alloc] initWithLogsDirectory:applicationDocumentsDirectory];

这篇关于CocoaLumberjack iOS - 我们可以更改日志文件名和目录吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆