sentryco / Logger

🔍 Simple console logger
MIT License
5 stars 1 forks source link
console crash-reporting crashalytics firebase google-analytics ios log logger logging logging-library macos print swift swiftpackagemanager trace trace-level

mit platform Lang SPM compatible Tests codebeat badge

🔍 Logger

Simple console logger

Features

Why Logger?

Logging format:

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

Configure:

// 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

Add custom log end-point like GA or Firebase crashalytics

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

Tracing

class Test {
   func myFunction() {
      Trace.trace("This msg")
   }
}
Test().myFunction() // Prints "This msg is called from function: myFunction in class: Test on line: 13"

Trace + Logger

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

Gotchas

Installation

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"),
    ]
),

Todo: