TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Apr 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Ayush Newatia.
Branch | Build Status |
---|---|
Develop | |
Master |
SwiftLogger is designed to be a simple, easy to use, no frills logging library. It allows you to log messages at 6 different levels and formats log messages so you know exactly where and when they have been logged from. This library has prioritised ease of use over extensibility.
SwiftLogger has 6 log levels that can be set or changed at any time.
The levels are:
Only logs less than or equal to the set log level will be printed to the console.
Make sure you have the below line at the top of every file in which you would like to use this library:
import SwiftLogger
The log level can be set using this method:
SwiftLogger.logLevel = .Verbose
The log level defaults to .Debug
if it has not been set. The SwiftLogger.LogLevel
enum contains values for all 6 log level described above.
I recommend you set the log level in your app delegate method application:didFinishLaunchingWithOptions
. You could also vary the log level via a debug settings screen if you wish.
Logs can be printed at the different log levels using these 5 methods
Log.error("Something has gone horribly wrong.")
Log.warning("Something might go wrong.")
Log.info("This is some useful information.")
Log.debug("This is a debug message.")
Log.verbose("This is some very specific information")
Log messages printed to the console will look like this:
28-09-2015 19:40:53.535 [Info] ViewController.swift:16 viewDidLoad(): Info message
SwiftLogger has built in functionality to log collections and will not use up resources looping over a collection if it does not need to be logged for the current log level.
To log objects in a collection, all the contained objects must conform to the Loggable
protocol. It only contains one method of the following signature: func log() -> String
. All NSObjects
conform to this protocol and return it’s description
property by default. Most swift primitives conform to Loggable
by default as well!
Dictionaries can be logged where the Key and Value are both Loggable
s.
The below code demonstrates how you can log collections to the console:
func collectionLoggingTest() {
let anArray = ["one","two","three","four"]
Log.debug(anArray)
}
You can optionally specify a prefix while logging a collection like:
func collectionLoggingTest() {
let anArray = ["one","two","three","four"]
Log.debug(anArray, prefix: "anArray")
}
Collection log messages look like this:
23-01-2016 23:35:15.528 [Debug] ViewController.swift:27 collectionLoggingTest():
one
two
three
four
Or with a prefix:
08-08-2016 23:05:37.420 [Debug] ViewController.swift:27 collectionLoggingTest(): anArray
one
two
three
four
SwiftLogger requires at least iOS 8 and ARC.
Docs are available on CocoaDocs
Migrated code to Swift 3.0.
Migrated code to Swift 2.3. There will be no more features of patches on Swift 2.3 after this release.
Fixing a documentation error
Added Dictionary
support.
Increased unit test coverage.
Added option for prefix on collection logging.
Fixed deprecation warnings in Swift 2.2. Added more unit tests.
Fixing a documentation error.
Added a new feature to log CollectionType
s. [ Except for Dictionaries
:( ]
Modified order of log levels.
Initial Release
I am looking to build a feature to log messages to a file in addition to the console.
Ayush Newatia, [email protected]
SwiftLogger is available under the MIT license. See the LICENSE.md file for more info.