1

我已经像这样配置 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)")
        })

打印,前卷功能和后卷功能,打印相同的路径和文件名。所以我没有创建一个新的日志文件。

我怎样才能创建它?

4

1 回答 1

1

问题是您正在DDFileLogger使用DDFileLogger(). 您应该将 fileLogger 存储在某处并在同一实例上调用 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)")
    })
于 2018-03-28T07:20:05.263 回答