[SNLog Project Page Link]
A simple task that every developer runs into when creating software is proper logging and tracing. What I have found is the simplest solution for logging is always the best.
Requirements are almost always the same:
- Need to log to the console when debugging
- Need to write to a file/database for runtime tracing and diagnostics
There are lots of frameworks for just about every platform out there but logging on the iPhone seemed to be lacking so I decided to create my own as an exercise. Something that would be applicable to my current projects as well as to help out anyone else that might need something like this.
NSLog is the de-facto standard for logging during debugging in Xcode but is primarily temporary scaffolding in your code that needs removed prior to deploying the application. I wanted something as simple to use as NSLog that could be configured to log somewhere more persistent than the console.
The plan of attack on this was straightforward:
- Minimize the files that need included in the project (just two, the .h and .m)
- Make use of it as easy as NSLog
- Re-define NSLog to use the framework instead of the built-in function
- Use a strategy pattern to allow multiple logging mechanisms (console and file built-in)
In addition, I ran into the following additions while I was working on it:
- Don’t let the file logging grow unbounded (especially on a mobile device)
- Capturing the log level/importance can allow different mechanisms to capture the level that is required
So that’s it … in two simple files I was able to create a simple logging mechanism that is a drop-in replacement for NSLog. I can keep all those frivolous NSLog statements in my code and choose where to redirect the log.
Check out the Project Page for more details and the download.