2016-12-15 54 views
1

以前,XCGLogger對我來說工作正常,但我決定嘗試一些更高級的功能。現在,在Xcode的控制檯視圖我的日誌輸出填充:「XCGLogger寫入日誌」在每個記錄行前重複

XCGLogger writing log to: <my logfile name>

它出現的每個記錄消息之前。任何想法爲什麼?

我對XCGLogger設置:

var log : XCGLogger { 
    let log = XCGLogger(identifier: "advancedLogger", includeDefaultDestinations: false) 

    let fileDestination = FileDestination(writeToFile: Constants.file.debugging, identifier: "advancedLogger.fileDestination") 

    fileDestination.showLogIdentifier = false 
    fileDestination.showFunctionName = true 
    fileDestination.showThreadName = false 
    fileDestination.showLevel = false 
    fileDestination.showFileName = true 
    fileDestination.showLineNumber = true 
    fileDestination.showDate = true 

    #if DEBUG 

     fileDestination.outputLevel = .verbose 

    #else 

     fileDestination.outputLevel = .debug 

     // don't log on main thread in production 
     fileDestination.logQueue = XCGLogger.logQueue 

    #endif 

    // Add the destination to the logger 
    log.add(destination: fileDestination) 

    return log 
} 

回答

1

固定!我會告訴你我做錯了什麼。查看修正的代碼塊的底部。

首先,建立log變量時,我離開了在聲明等號(=):

let log : XCGLogger {

和忽略添加()到塊的末尾。

然後我從編譯器收到一個錯誤,指出:

'let' declarations cannot be computed properties

我想,也許東西雨燕3.0,我是不知道的變化,所以我改變了它從letvar和持續,這意味着稍後再回到這個問題。當然,我忘了。

然後我遇到了上面解釋的問題,在這裏發佈之後,再次走過一遍,意識到每次發送消息的唯一方式就是每次登錄時它都以某種方式被初始化。 D'哦!現在我想起了關於「計算屬性」的警告,並最終意識到我的代碼每次訪問變量時都運行了所有日誌初始化。

一旦我回去,並添加了=()的聲明log,並切換回let,一切都會按計劃:

let log : XCGLogger = { 
    let log = XCGLogger(identifier: "advancedLogger", includeDefaultDestinations: false) 

    let fileDestination = FileDestination(writeToFile: Constants.file.debugging, identifier: "advancedLogger.fileDestination") 

    fileDestination.showLogIdentifier = false 
    fileDestination.showFunctionName = true 
    fileDestination.showThreadName = false 
    fileDestination.showLevel = false 
    fileDestination.showFileName = true 
    fileDestination.showLineNumber = true 
    fileDestination.showDate = true 

    #if DEBUG 

     fileDestination.outputLevel = .verbose 

    #else 

     fileDestination.outputLevel = .debug 

     // don't log on main thread in production 
     fileDestination.logQueue = XCGLogger.logQueue 

    #endif 

    // Add the destination to the logger 
    log.add(destination: fileDestination) 

    return log 
}()