mmeylan / Logger

Logging method signature using Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Logger in Swift

Logger is a simple class that you can use to uniform logging in your projects. It is designed to print not only the user message but also the file, function name and line number at which the log was written. Logger differentiates:

  • Debug logs
  • Notice logs (visible also in release and distribution schemes)
  • Warnings
  • Errors

How to use

Logger.debug("This is only visible when running in debug")
Logger.warn("This is a warning")
Logger.error("This is an error")
Logger.notice("This is always visible")

This code will output:

2015-01-14 11:54:09.284 Logger[3500:58936] DEBUG [ViewController.viewDidLoad():16]: This is only visible when running in debug
2015-01-14 11:54:09.285 Logger[3500:58936] WARN [ViewController.viewDidLoad():17]: This is a warning
2015-01-14 11:54:09.285 Logger[3500:58936] ERROR [ViewController.viewDidLoad():18]: This is an error
2015-01-14 11:54:09.285 Logger[3500:58936] NOTICE [ViewController.viewDidLoad():19]: This is always visible

In order for the debug logging to work you need to set the DEBUG preprocessing flag. To do so, edit your target build setting: Swift Compiler - Custom flags to add -DDEBUG as shown below:

alt tag

Details

This class is based on NSLog which makes the logging very verbose in my opinion. The following 2015-01-14 11:24:52.227 Logger[3238:50509] could be simplified or changed. I chose to use NSLog because it is thread safe unlike Swift println (to my last test and this post). One improvement could be to use println and implement custom locking. This would allow to customize the whole "date - produt name" part.

Licence

Code released under the MIT license.

About

Logging method signature using Swift

License:MIT License


Languages

Language:Swift 100.0%