swift-log-SwiftyBeaver

0.1.0

A logging backend for SwiftLog that sends log messages to SwiftyBeaver.
ShivaHuang/swift-log-SwiftyBeaver

What's New

LoggingSwiftyBeaver v0.1.0

2021-07-30T17:11:00Z

Initial release! 🎉

LoggingSwiftyBeaver

Swift Release Platforms License Twitter

A logging backend for SwiftLog that sends log messages to SwiftyBeaver.

Installation 📦

Add the LoggingSwiftyBeaver package as a dependency to your Package.swift file.

.package(url: "https://github.com/ShivaHuang/swift-log-SwiftyBeaver.git", from: "0.1.0")

Add LoggingSwiftyBeaver to your target's dependencies.

.target(
    name: "Example",
    dependencies: [
        .product(name: "LoggingSwiftyBeaver", package: "swift-log-SwiftyBeaver")
    ])

Usage 📝

1. Let's import the logging API package:

import Logging
import LoggingSwiftyBeaver

2. Create a logger, the label works similarly to a DispatchQueue label:

let logger = Logger(label: "Example") { (label) in
    SwiftyBeaver.LogHandler(label, destinations: [
        ConsoleDestination()
    ])
}

Alternatively, you can use SwiftyBeaver only in DEBUG build and don't print anything in RELEASE build:

let logger: Logger = {
    Logger(label: "Example") { (label) in
        #if DEBUG
        return SwiftyBeaver.LogHandler(label, destinations: [
            ConsoleDestination()
        ])
        #else
        return Logging.SwiftLogNoOpLogHandler()
        #endif
    }
}()

Futher, you can custom format and set console output to short time, log level & message:

let logger: Logger = {
    Logger(label: "Example") { (label) in
        #if DEBUG
        let console: ConsoleDestination = {
            let destination = ConsoleDestination()

            destination.format = "$DHH:mm:ss$d $L $M"

            return destination
        }()

        return SwiftyBeaver.LogHandler(label, destinations: [
            console
        ])
        #else
        return Logging.SwiftLogNoOpLogHandler()
        #endif
    }
}()

3. We're now ready to use it:

// logging an informational message
logger.info("Hello World!")

// ouch, something went wrong
logger.error("Houston, we have a problem: \(problem)")

Log Levels 💜💚💙💛❤️

It's a good habit to distinguish logs by levels. However, SwiftLog defines 7 levels while SwiftyBeaver has only 5. So an 1-to-1 mapping between SwiftLog and SwiftyBeaver is not possible. Following is a table for the mapping:

SwiftLog SwiftyBeaver
trace verbose
debug debug
info info
notice warning
warning warning
error error
critical error

Origin 🗄

This program was developed by @shivahuang as part of Taiwan Social Distancing.

Description

  • Swift Tools 5.2.0
View More Packages from this Author

Dependencies

Last updated: Wed Nov 06 2024 08:39:34 GMT-1000 (Hawaii-Aleutian Standard Time)