Ios 使用CocoaLumberjack手动创建新日志文件

Ios 使用CocoaLumberjack手动创建新日志文件,ios,swift,cocoalumberjack,Ios,Swift,Cocoalumberjack,我已将CocoaLumberjack配置为: // CocoaLumberjack DDLog.add(DDASLLogger.sharedInstance, with: DDLogLevel.debug) DDLog.add(DDTTYLogger.sharedInstance, with: DDLogLevel.debug) DDTTYLogger.sharedInstance.colorsEnabled = true fileLogger = DDFileLogger.init() fil

我已将CocoaLumberjack配置为:

// CocoaLumberjack
DDLog.add(DDASLLogger.sharedInstance, with: DDLogLevel.debug)
DDLog.add(DDTTYLogger.sharedInstance, with: DDLogLevel.debug)
DDTTYLogger.sharedInstance.colorsEnabled = true
fileLogger = DDFileLogger.init()
fileLogger?.doNotReuseLogFiles = true // Always create a new log file when apps starts
fileLogger?.rollingFrequency = 86400 // 24 Hours
fileLogger?.maximumFileSize = 0 // Force log to only roll after 24 hours
fileLogger?.logFileManager.maximumNumberOfLogFiles = 1 // Keeps 1 log file plus active log file
DDLog.add(fileLogger!, with: DDLogLevel.debug)
在我的应用程序中,我需要以下日志系统:

我的应用程序的入口点是登录视图控制器。我想在这里写日志条目,这样我就可以查看是否一切正常。如果用户登录正确,我希望滚动/归档该日志,并为该用户创建一个新的日志。在这个新日志中,我将保留用户会话期间发生的错误。如果用户注销,我想再次滚动/归档日志并创建一个新的日志。在滚动/归档日志之前,我总是将其发送到服务器,以便从设备中删除它

我正在尝试以下操作以滚动/归档日志,但未成功:

Server().sendUserLog(filePath: DDFileLogger().currentLogFileInfo.filePath, onSuccess: { // This function send the log to the server, if all goes good, I want to roll it. 
          print(">>>>>>>>>>>>>>>>>>>>> \(DDFileLogger().currentLogFileInfo.filePath)")
          DDFileLogger().rollLogFile(withCompletion: { 
            print("Log rolled")
            print(">>>>>>>>>>>>>>>>>>>>> \(DDFileLogger().currentLogFileInfo.filePath)")
          })
        }, onError: { (error) in
          DDLogError("LoginVC - sendUserLog Error: \(error)")
        })
打印、前滚动功能和后滚动功能都打印相同的路径和文件名。所以我不会创建新的日志文件


如何创建它?

问题是您正在使用
DDFileLogger()
创建一个新的
DDFileLogger
。您应该将文件记录器存储在某个位置,并在同一实例上调用rollLogFile。 大概是这样的:

let fileLogger = DDFileLogger()
Server().sendUserLog(
    filePath: fileLogger.currentLogFileInfo.filePath, 
    onSuccess: { // This function send the log to the server, if all goes good, I want to roll it. 
        print(">>>>>>>>>>>>>>>>>>>>> \(fileLogger.currentLogFileInfo.filePath)")
        fileLogger.rollLogFile(withCompletion: { 
            print("Log rolled")
            print(">>>>>>>>>>>>>>>>>>>>> \(fileLogger.currentLogFileInfo.filePath)")
        })
    }, 
    onError: { (error) in
      DDLogError("LoginVC - sendUserLog Error: \(error)")
    })

解决了的。读了你的答案后,我觉得自己很愚蠢,但现在问题解决了。谢谢@marosoaie