Skip to content

A simple logging library for Swift 6, providing easy-to-use logging functionalities with support for different log levels and backends.

License

Notifications You must be signed in to change notification settings

fatbobman/SimpleLogger

Repository files navigation

SimpleLogger

A simple logging library for Swift 6, providing easy-to-use logging functionalities with support for different log levels and backends.

Features

  • 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 and OSLogBackend.
  • Thread Safety: Utilizes DispatchQueue for thread-safe logging.
  • Environment Configurable: Control logging output via environment variables.

Requirements

  • Swift 6

Installation

Swift Package Manager

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

Usage

Creating a Logger

Default OS Logger

let logger: LoggerManagerProtocol = .default(subsystem: "com.yourapp", category: "networking")

Console Logger

let logger: LoggerManagerProtocol = .console()

Logging Messages

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

Custom Logger Backend

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

Disabling Logs

Set the DisableLogger environment variable to disable logging:

ProcessInfo.processInfo.environment["DisableLogger"] = "true"

Examples

Using the Default Logger

import SimpleLogger

let logger: LoggerManagerProtocol = .default(subsystem: "com.example.app", category: "general")
logger.info("App started")

Using the Console Logger

import SimpleLogger

let logger: LoggerManagerProtocol = .console()
logger.debug("Debugging information")

Custom Logger Test

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!")

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A simple logging library for Swift 6, providing easy-to-use logging functionalities with support for different log levels and backends.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages