https://github.com/dtroupe18/swiftdebuglog
Lightweight Swift Logging
https://github.com/dtroupe18/swiftdebuglog
cocoapods debug ios logging open-source swift swift4
Last synced: about 1 year ago
JSON representation
Lightweight Swift Logging
- Host: GitHub
- URL: https://github.com/dtroupe18/swiftdebuglog
- Owner: dtroupe18
- License: mit
- Created: 2019-02-17T21:01:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-25T20:26:03.000Z (over 7 years ago)
- Last Synced: 2025-01-30T18:38:23.400Z (over 1 year ago)
- Topics: cocoapods, debug, ios, logging, open-source, swift, swift4
- Language: Swift
- Size: 16 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftDebugLog
[](https://travis-ci.org/Dtroupe18/SwiftDebugLog)
[](https://cocoapods.org/pods/SwiftDebugLog)
[](https://cocoapods.org/pods/SwiftDebugLog)
[](https://cocoapods.org/pods/SwiftDebugLog)
Simple Swift Debug Logger inspired by [this medium post](https://medium.com/@sauvik_dolui/developing-a-tiny-logger-in-swift-7221751628e6)
## Purpose:
1. 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)```
2. 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 🐛🐛🐛🐛 [ViewController.swift] line: 25 logInProduction() -> This log always prints!
2019-02-17 15:29:25.633-0500 😡 [ViewController.swift] line: 33 trySomething() -> The item couldn’t be opened because the file name “” is invalid.
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 ⚠️ [ViewController.swift] line: 48 doSomethingWith(optional:) -> Optional is nil!
2019-02-17 15:29:25.634-0500 🚨🚨 [ViewController.swift] line: 55 doSomethingReallyImportant(shouldCrash:) -> Crashing for some reason
## Code to create the above logging:
```swift
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
1. Error = 😡
2. Debug = 🕵
3. Warning = ⚠️
4. Severe = 🚨🚨
5. 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](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'SwiftDebugLog'
```
or copy and paste [Log.swift](https://github.com/dtroupe18/SwiftDebugLog/blob/master/SwiftDebugLog/Classes/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.