Swiftpack.co - ShivaHuang/swift-log-SwiftyBeaver as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ShivaHuang.
ShivaHuang/swift-log-SwiftyBeaver v0.1.0
A logging backend for SwiftLog that sends log messages to SwiftyBeaver.
⭐️ 4
🕓 2 years ago
.package(url: "https://github.com/ShivaHuang/swift-log-SwiftyBeaver.git", from: "v0.1.0")

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.

GitHub

link
Stars: 4
Last commit: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

LoggingSwiftyBeaver v0.1.0
2 years ago

Initial release! 🎉

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics