可可校友杰克iOS -我们可以更改日志文件名和目录吗?

时间:2022-12-12 22:00:14

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];

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?

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

2 个解决方案

#1


4  

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

虽然我认为我的回复可能太迟了,但请找到以下我的解决方案:

1) Inherit DDLogFileManagerDefault and override methods: newLogFileName and isLogFile

1)继承DDLogFileManagerDefault和override方法:newLogFileName和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) In AppDelegate, change following line:

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

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

TO:

:

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

#2


0  

newLogFileName and isLogFile methods are available to achieve the task

可以使用newLogFileName和isLogFile方法实现此任务

#1


4  

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

虽然我认为我的回复可能太迟了,但请找到以下我的解决方案:

1) Inherit DDLogFileManagerDefault and override methods: newLogFileName and isLogFile

1)继承DDLogFileManagerDefault和override方法:newLogFileName和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) In AppDelegate, change following line:

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

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

TO:

:

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

#2


0  

newLogFileName and isLogFile methods are available to achieve the task

可以使用newLogFileName和isLogFile方法实现此任务