SwiftDebugLog
Simple Swift Debug Logger inspired by this medium post
Purpose:
-
Global wrapping of the print function. All print statements will be removed at compile time unless your code is compiled in Debug. Debug printing in production can be done using:
Log.logProduction(error)
-
Allows you to easily log errors that include the file name, line, and function name without additional typing.
Sample Output:
2019-02-17 15:29:25.627-0500
2019-02-17 15:29:25.633-0500
2019-02-17 15:29:25.633-0500 🕵 [ViewController.swift] line: 43 makeApiCall() -> ["userId": 1, "id": 1, "title": "delectus aut autem"]
2019-02-17 15:29:25.634-0500
2019-02-17 15:29:25.634-0500
Code to create the above logging:
func logInProduction() {
// This should only be used when debugging in production!
Log.logProduction("This log always prints!")
}
func trySomething() {
do {
// Something that might throw an error
let _ = try String(contentsOfFile: "")
} catch let error {
Log.logError(error.localizedDescription)
}
}
func makeApiCall() {
let json: [String: Any] = [
"userId": 1,
"id": 1,
"title": "delectus aut autem"
]
Log.logDebug(json)
}
func doSomethingWith(optional: String?) {
guard let string = optional else {
Log.logWarning("Optional is nil!")
return
}
}
func doSomethingReallyImportant(shouldCrash: Bool) {
if shouldCrash {
Log.logSevere("Crashing for some reason")
fatalError()
}
}
Logging Levels
- Error =
😡 - Debug = 🕵
- Warning =
⚠️ - Severe =
🚨 🚨 - Production =
🐛 🐛 🐛 🐛
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Installation
SwiftDebugLog is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftDebugLog'
or copy and paste Log.swift into your project! Using this method prevents you from having to import SwiftDebugLog
in your files and it also globally wraps print
into debug only.
License
SwiftDebugLog is available under the MIT license. See the LICENSE file for more info.