Mason G. Zhwiti
Mason G. Zhwiti

Reputation: 6540

"XCGLogger writing log to" repeated before every logged line

Previously XCGLogger was working fine for me, but I decided to try some of the more advanced functions. Now my logging output in the console view of Xcode is filled with:

XCGLogger writing log to: <my logfile name>

It's appearing before every logged message. Any ideas why?

My setup for 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
}

Upvotes: 1

Views: 410

Answers (1)

Mason G. Zhwiti
Mason G. Zhwiti

Reputation: 6540

Fixed! I'll walk you through what I did wrong. See the bottom for the corrected code block.

First, when setting up the log variable, I left out the = sign in the declaration:

let log : XCGLogger {

and neglected to add () to the end of the block.

I then received an error from the compiler indicating:

'let' declarations cannot be computed properties

I thought maybe something changed in Swift 3.0 that I wasn't aware of, so I changed it from let to var and continued, meaning to come back to this issue later. Of course, I forgot.

Then I experienced the issue explained above, and after posting here, walked through everything again and realized the only way that message could happen every time was if it was somehow being initialized every time I logged. D'oh! Now I remembered the warning about "computed property," and finally realized my code was running all my log initialization every time I accessed the log variable.

Once I went back and added the = and the () to the declaration for log, and switched it back to let, everything works as intended:

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
}()

Upvotes: 1

Related Questions