A simple, lightweight and easy-to-use logging library to spice up your logs and make finding things easier.
You are right by asking yourself this question. You do not necessarily need another library, just to make your logs prettier. The reason, however, why I made this library is to make your logs more searchable.
- I always wanted to make my own framework, so this was a great opportunity.
- At work I recently worked on a huge project, that involved a lot of network calls. Additionally it connected to a lot of devices via bluetooth. Everything was printed to the console, which made using it impossible. Whenever I wanted to
print
something I had to use a prefix.
And so I thought: why not give every log a Group
and make logs more organized.
Bonus: with Group
s, LogLevel
s and Time
you can now easily search/filter your logs, to find everything related to it.
The project includes an Example target.
Create a global instance of GroupedLogger
like this:
import GroupedLogger
// setup logger
let log = GroupedLogger.shared
Now you are ready to setup your first log π
log.debug("Hello, World!") // prints: π’ Hello, World!
log.print("Hello, World!") // prints: π’ Hello, World!
GroupedLogger has 9 different log levels to make your logs more useful. They are constructed like the following:
public enum LogLevel: String {
case debug = "π’"
case info = "βΉοΈ"
case notice = "πΆ"
case warning = "β οΈ"
case error = "β"
case critical = "βοΈ"
case alert = "π¨"
case emergency = "π"
case success = "β
"
}
You can access each log level either by using log.[LogLevel](String)
or by using log.print(String, logLevel: .[LogLevel])
.
You can find more examples at the end of this section.
My favorite feature of this framework is the ability to add groups to your logs. Groups make it easier to find everything related to a topic. If you have a networking library for example and you want to log all events, the console can get clustered really fast. Good luck finding anything. Giving all your networking prints a Group
however, will make it easy to search for everything related to it.
For now GroupedLogger
comes with 5 predefined groups. Feel free to suggest more.
public struct Group {
public static let system: Group = Group("System")
public static let network: Group = Group("Network")
public static let api: Group = Group("API")
public static let bluetooth: Group = Group("Bluetooth")
public static let coordinator: Group = Group("Coordinator")
}
You can also add your custom Group
by extending GroupedLogger.Group
like this:
// custom logging groups
extension GroupedLogger.Group {
static let sync = GroupedLogger.Group("Sync")
static let moya = GroupedLogger.Group("Moya")
}
Now you can just call it like this:
log.debug("Test", group: .system) // prints: π’[System] Test
log.print("Another Test", group: .network) // prints: π’[Network] Another Test
Finding all Network
related logs is now as easy as searching for [Network]
in your console. Nice π
Note: Group
s are optional, but probably the best feature of GroupedLogger
(hence the name π)
I have implemented settings to make your logger as customizable as possible. Simply call log.setSettings()
to adjust them to your needs.
Settings
have two options:
- show timestamps (yes or no)
- show emojis (yes or no) To disable Emojis and print the time, simply write:
// set settings
log.setSettings(to: GroupedLogger.Settings(useTimestamp: .yes(dateFormatter: standardDateFormatter), useEmoji: .no))
log.print("Test") // prints: [Debug][11:43:12] Test
As you can see above, if you turn on useTimestamp
to .yes
, you have to pass a DateFormatter
with it.
In the example above i used the following DateFormatter
:
lazy var standardDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
return dateFormatter
}()
Note: if you donβt change the settings, they are set to useTimestamp: .no
and useEmojis: .yes
.
Another Note: changing the settings with log.setSettings(Settings)
will change the appearance for all future logs.
log.debug("Test") // prints: π’ Test
log.info("Test") // prints: βΉοΈ Test
log.notice("Test") // prints: πΆ Test
log.warning("Test") // prints: β οΈ Test
log.error("Test") // prints: β Test
log.critical("Test") // prints: βοΈ Test
log.alert("Test") // prints: π¨ Test
log.emergency("Test") // prints: π Test
log.success("Test") // prints: β
Test
log.print("Test", logLevel: .debug) // prints: π’ Test
log.print("Test", logLevel: .info) // prints: βΉοΈ Test
log.print("Test", logLevel: .notice) // prints: πΆ Test
log.print("Test", logLevel: .warning) // prints: β οΈ Test
log.print("Test", logLevel: .error) // prints: β Test
log.print("Test", logLevel: .critical) // prints: βοΈ Test
log.print("Test", logLevel: .alert) // prints: π¨ Test
log.print("Test", logLevel: .emergency) // prints: π Test
log.print("Test", logLevel: .success) // prints: β
Test
Add the following dependency to your Package.swift
file:
.package(url: "https://github.com/fgeistert/GroupedLogger.git", from: "2.0.1")
Add the following line to your Cartfile.
github "fgeistert/GroupedLogger" ~> 2.0.1
Then run carthage update
.
Just drag and drop the .swift
files from the GroupedLogger
folder into your project.
GroupedLogger is built with Swift 5
- Open an issue
- Fork it
- create new branch
- commit all your changes to your branch
- create a pull request
You can check out my website or follow me on twitter.