Simple console logger
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
Add the following line to your Package.swift
file:
.package(url: "https://github.com/sentryco/Logger", branch: "main")
Then add Logger
as a dependency for your targets:
.target(
name: "MyTarget",
dependencies: [
.product(name: "Logger", package: "Logger"),
]
),
Trace.trace()
call in log call so it can be toggled on and off