我正在使用CocoaLumberjack V2.4将日志保存到文件中。
这是将保存日志设置为文件的默认代码:
DDFileLogger *fileLogger = [[DDFileLogger alloc] init]; // File Logger
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLog addLogger:fileLogger];
日志保存在默认位置:/ AppData / Library / Caches / Logs /
我想实现一个API来手动删除保存的日志。我检查他们有Manually clear logs作为公开问题。有人有建议吗?
答案 0 :(得分:6)
这应该适合你:
[fileLogger rollLogFileWithCompletionBlock: ^{
for (NSString *filename in fileLogger.logFileManager.sortedLogFilePaths) {
[[NSFileManager defaultManager] removeItemAtPath:filename error:nil];
}
}];
答案 1 :(得分:1)
快速答案:
fileLogger.rollLogFile(withCompletion: {
for filename: String in self.fileLogger.logFileManager.sortedLogFilePaths {
do {
try FileManager.default.removeItem(atPath: filename)
} catch {
print(error.localizedDescription)
}
}
})
答案 2 :(得分:0)
在Swift中尝试这个。
func clearLogs(){
let logHelper = LogHelper(subsystem: "Logs", category: "Clear")
logHelper.i("Clearing logs...")
for logger in DDLog.allLoggers {
if let fileLogger = logger as? DDFileLogger {
fileLogger.rollLogFile{
for path in fileLogger.logFileManager.sortedLogFilePaths {
let fileURL = URL(fileURLWithPath: path)
logHelper.i("\(fileURL)")
do {
try FileManager.default.removeItem(at: fileURL)
logHelper.i("Done!")
}
catch {
logHelper.e(error.localizedDescription)
}
}
}
}
}
}