Simple console logger
- 4 levels of severity (π΄ error, π warning, π΅οΈ debug, π£ info)
- 9 tag types (π‘ network, π database, π₯ UI, πΎ file, π security, π payment, βοΈ system, π§° util, π other)
- Output to consol, file, or a custom end-point like Google analytics or Firebase crashalytics etc (Use Telemetry for GA)
- Debugging complex apps efficiently requires filtering to prevent console clutter.
- Fixing network bugs becomes easier when UI and DB logging can be turned off.
- Sending errors to endpoints like Google Analytics or Firebase Crashlytics is beneficial.
Logger.debug(text: "Network.connect - connection established successfully", type: .net)
// Output: [π΅οΈ Debug] [23-12-24 22:00:45] β π‘ Network.connect: connection established successfully
Logger.warning(text: "Network.connect \(error.localDescription)", type: .net)
// Output: [οΈπ Warning] [23-12-24 22:00:45] β π‘ Network.connect: Wifi not turned on
Logger.error(text: "Network.process-data \(error.localDescription)", type: .net)
// Output: [π΄ Error] [23-12-24 22:00:45] β π‘ Network.process-data: Decoding was unsuccessful. Nothing was saved
// Print text format
Logger.config = .plain // .full
// Output transport
Logger.type = .console // .file(filePath), .custom({ level, tag, msg in })
// Levels and tags
Logger.mode = .everything // .nothing, .essential
// Or use this convenient one-liner:
Logger.setup(config: .plain, mode: .everything, type: .console)
Note
Since iOS14+ Target apples own Logger class, write: os.Logger
let onLog: LogType.OnLog = { msg, level, _ in
if [LogLevel.error, .warning].contains(where: { $0 == level }) {
Swift.print(msg) // Only prints warning and error, replace w/ call to GA etc
}
}
Logger.type = .custom(onLog) // Add the custom output closure to the logger
Logger.warn("Uh-Oh something went wrong") // Prints
Logger.error("Unsupported format, bail") // Prints
Logger.debug("Entered backround") // Does not print
class Test {
func myFunction() {
Trace.trace("This msg")
}
}
Test().myFunction() // Prints "This msg is called from function: myFunction in class: Test on line: 13"
Logger.warn("\(Trace.trace() - error occured", tag: .net) - error occured") // Called inside NetManager.connect
// Prints: [οΈπ Warning] [23-12-24 22:00:45] β π‘ NetManager.connect - error occured
- Print only works when debugging an app. When the app is built for running, Swift.print doesn't work anymore. Use file logging in release if needed.
- Use the Telemetry for GA hook.
Add the following line to your Package.swift
file:
.package(url: "https://github.com/sentryco/Logger.git", branch: "main")
Then add Logger
as a dependency for your targets:
.target(
name: "MyTarget",
dependencies: [
.product(name: "Logger", package: "Logger"),
]
),
- Consider including the
Trace.trace()
call in log call so it can be toggled on and off - Add the tag-type emoji to output just before the message
- Research how to log fatal crashes, if possible. Exception handling needs to be explored
- Conduct more research on logging best practices
- Add terminal color to formatting text: https://github.com/sushichop/Puppy/blob/main/Sources/Puppy/LogColor.swift
- Add native OS support: https://www.avanderlee.com/debugging/oslog-unified-logging/
- Test Firebase Crashlytics in a demo project
- Add support for oslog in the framework. We currently support it in the ad-hoc callback. Add this to unit test as well as instructions on Console.app usage and limitations.
- Consider adding another log type called "important"
- Add usage gif exploring system console, google-analytics, xcode consol