8

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

At present I am using logLevel = .None.

Is this the recommended way ?

JOM
  • 8,139
  • 6
  • 78
  • 111
Kaunteya
  • 3,107
  • 1
  • 35
  • 66

1 Answers1

9

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.

Dave Wood
  • 13,143
  • 2
  • 59
  • 67
  • 1
    Btw. How can the severe and error logs in production reach back to me if I do not completely remove them – Kaunteya Sep 27 '15 at 06:15
  • With the current version, you'd need to talk to the user having the issue and they may be able to see them in their console log. Or you add a method to have the app email you the log file, or even report it to a remote server. I plan to add some remote reporting stuff by the end of the year (if time permits). – Dave Wood Sep 27 '15 at 17:06