问题
I am developing mac application in that application I need to log to folder, where already some other application is also logging,so need to create only one file in that folder, when file rolling occurs the whole contents in that log folder are deleting .this code I am using .I don't want delete contents in log folder and is it possible to use only file with constant name .Please help me.
// Configure CocoaLumberjack DDLog.addLogger(DDASLLogger.sharedInstance()) DDLog.addLogger(DDTTYLogger.sharedInstance()) // Initialize File Logger let manager : BaseLogFileManager = BaseLogFileManager(logsDirectory:K.LogFileDir) let fileLogger: DDFileLogger = DDFileLogger(logFileManager: manager) // File Logger fileLogger.maximumFileSize = 1024*1024*20 fileLogger.doNotReuseLogFiles = false fileLogger.logFileManager.maximumNumberOfLogFiles = 1 DDLog.addLogger(fileLogger)
class BaseLogFileManager : DDLogFileManagerDefault
{
override var newLogFileName: String! { get {
return K.LogFileName
}}
override func isLogFile(fileName: String!) -> Bool
{
return true
}
}
回答1:
Work around is to disable rolling frequency, don't assign maximum size or rollingFrequency and check size using NSFileManager. If file size is greater than specific limit, remove and create new file.
// Configure CocoaLumberjack
DDLog.addLogger(DDASLLogger.sharedInstance())
DDLog.addLogger(DDTTYLogger.sharedInstance())
// Initialize File Logger
let manager : BaseLogFileManager = BaseLogFileManager(logsDirectory:K.LogFileDir)
let fileLogger: DDFileLogger = DDFileLogger(logFileManager: manager) // File Logger
do {
let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(K.LogFileDir+"/"+K.LogFileName)
if let _attr = attr {
if _attr.fileSize() > 1024*1024*10
{
NSFileManager.defaultManager().createFileAtPath(K.LogFileDir+"/"+K.LogFileName, contents: NSData(), attributes: nil)
}
}
} catch {
print("Error: \(error)")
}
fileLogger.doNotReuseLogFiles = false
fileLogger.logFileManager.maximumNumberOfLogFiles = 1
DDLog.addLogger(fileLogger)
来源:https://stackoverflow.com/questions/35991208/cocoalumberjack-log-to-one-file