Search code examples
iosswiftloggingxcglogger

Completely disable logs from XCGLogger when in production


How do I completely disable logs from XCGLogger when in production environment?

At present I am using logLevel = .None.

Is this the recommended way ?


Solution

  • That's one possible way, but not ideal.

    First, I'd wonder if you really want to completely disable logs in production. using error and severe logs can be useful diagnostic tools for released apps.

    If you do however want to completely eliminate logs in production, I would recommend altering the way you set up and use the logger than what I have in the official docs.

    Change the global log object to be an optional instead:

    let log: XCGLogger? = {
        #if DEBUG
            let log = XCGLogger.defaultInstance()
            log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil, fileLogLevel: .Debug)
            return log
        #else
            return nil
        #endif
    }
    

    Then change your log calls to:

    log?.debug("whatever")
    

    This will eliminate any overhead of the logger since log will be nil in production and no logging calls will ever be made.