bits about life, coding and stuff
I left NSLog a long time ago, using my own written wrappers (that compile out on release), then using wrappers from three20/facebook (not that different, just more log levels)
Now I’ll switch again, this time to a proper logging system. Coming from Java, you know log4j and the concepts there. GTMLogger is the next best thing to it in ObjectiveC-Land. It’s also open source and free, from the google-toolbox-for-mac. (Apache Licence 2)
Check out this guide for a step-by-step integration, complete with user level logging.
From the google framework you need:
Add them to your project (copy the files) and add GTMLogger.h to your precompiled headers. You’re mostly done! To add fance two-way logging (system AND filesystem) add this initializer to your AppDelegate:
This code manages your logfile and trunicates it to kDebugFileMaxSize (byte). It also sets two writers (console, file) and sets up the default LogLevelFilter.
Just replace your NSLog-calls with GTMLoggerDebug(…), GTMLoggerInfo(…), GTMLoggerError(…)
GTMLogDebug will be compiled out, other calls will be available in your release*. Beware that too much logging can decrease performance.
What you probably want is a setting like “enable logging” that the user can turn on if he’s some strange bug that you can’t reproduce. That’s possible with Log Filters:
* Add GTMVerboseLogging (BOOL) to your settings, call it “Logging” – that’s it! If enabled, Info-Class logs are logged, if disabled, only errors are logged.
Files in the tmp-directory are not backed up and may be deleted at any time. But as it’s just logging data, we don’t really care.
Your application should remove files from this directory when it determines they are no longer needed. (The system may also purge lingering files from this directory when your application is not running.)
If you really need your logs a bit more secure, change “tmp” to “Library\Caches”, files there won’t be backed up, but also won’t be deleted randomly by the system.
For more information, just check out the GTMLogger.h-Header. Google has excellent documentation.
If you use this code, feel free to drop a comment here!
Related posts: