A simple logging library for Swift 6, providing easy-to-use logging functionalities with support for different log levels and backends.
- Log Levels: Supports
.debug
,.info
,.warning
, and.error
levels. - Custom Backends: Easily create custom log backends by conforming to
LoggerBackend
. - Built-in Backends: Includes
ConsoleLogBackend
andOSLogBackend
. - Thread Safety: Utilizes
DispatchQueue
for thread-safe logging. - Environment Configurable: Control logging output via environment variables.
- Swift 6
Add the package to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/fatbobman/SimpleLogger.git", from: "0.0.1")
]
Then import the module in your code:
import SimpleLogger
let logger: LoggerManagerProtocol = .default(subsystem: "com.yourapp", category: "networking")
let logger: LoggerManagerProtocol = .console()
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
Conform to LoggerBackend
to create a custom backend:
public protocol LoggerBackend: Sendable {
var subsystem: String { get }
var category: String { get }
func log(level: LogLevel, message: String, metadata: [String: String]?)
}
Example:
struct CustomLoggerBackend: LoggerBackend {
let subsystem: String = "Custom Logger"
func log(level: LogLevel, message: String, metadata: [String: String]?) {
// Custom logging implementation
}
}
Set the DisableLogger
environment variable to disable logging:
ProcessInfo.processInfo.environment["DisableLogger"] = "true"
import SimpleLogger
let logger: LoggerManagerProtocol = .default(subsystem: "com.example.app", category: "general")
logger.info("App started")
import SimpleLogger
let logger: LoggerManagerProtocol = .console()
logger.debug("Debugging information")
struct CustomLogger: LoggerManagerProtocol {
let expect: @Sendable (String, LogLevel) -> Void
func log(_ message: String, level: LogLevel, file: String, function: String, line: Int) {
expect(message, level)
}
}
let logger: LoggerManagerProtocol = CustomLogger(expect: { msg, level in
#expect(msg == "Hello, World!")
#expect(level == .info)
})
logger.info("Hello, World!")
This project is licensed under the MIT License. See the LICENSE file for details.