Swiftpack.co -  BlueEventHorizon/BwLogger as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
BlueEventHorizon/BwLogger
Quite easy logger, you just paste file on your project!
.package(url: "https://github.com/BlueEventHorizon/BwLogger.git", from: "4.0.12")

Logger

Simple and customizable Logging API package for Swift

You can customize and use BwLogger. You can inject by describing the output format and output destination.

The standard output destination is

  1. print statement
  2. For iOS14 or above, os.Logger
  3. If it is less than iOS14, os_log It has become.

APIs are


/// Appropriate for messages that contain information only when debugging a program.
func entered(_ instance: Any = "", message: String = "")

/// Appropriate for informational messages.
func info(_ message: Any, instance: Any = "")

/// Appropriate for messages that contain information normally of use only when
/// debugging a program.
func debug(_ message: Any, instance: Any = "")

/// Appropriate for conditions that are not error conditions, but that may require
/// special handling.
func notice(_ message: Any, instance: Any = "")

/// Appropriate for messages that are not error conditions, but more severe than
func warning(_ message: Any, instance: Any = "")

/// Appropriate for error conditions.
func error(_ message: Any, instance: Any = "")

/// Appropriate for critical error conditions that usually require immediate
/// attention.
func fatal(_ message: Any, instance: Any = "")

func `deinit`(_ instance: Any = "", message: Any = "")

Usage


// 1) import the logging API package
import BwLogger

// 2) create a logger
let log = Logger.default

// 3) use it
logger.degub("Hello World!")

Output

[🟠 DEBG] [2020/07/26 00:53:26.938 GMT+9] [main] Hello World! -- ExampleViewController:showLog(title:) ExampleViewController.swift:45

Customization

If you use declare below, Logger uses os_log provided Apple.


log.setDependency(OsLogger())

You could make dependency code implements LoggerDependency protocol for Logger like class OsLogger below


public class OsLogger: LoggerDependency {

    public init() {}

    public func log(_ context: LogContext) {

        var formattedMessage = ""

        switch context.level {
            case .trace:
                formattedMessage = "\("➡️") \(context.methodName())\(context.addSpacer(" -- ", to: context.message))"
            case .debug:
                formattedMessage = "\("🟠") [\(context.threadName())]\(context.message) -- \(context.lineInfo())"
            case .info:
                formattedMessage = "\("🔵")\(context.addSpacer(" ", to: context.message)) -- \(context.lineInfo())"
            case .notice:
                formattedMessage = "\("🟢")\(context.addSpacer(" ", to: context.message)) -- \(context.lineInfo())"
            case .warning:
                formattedMessage = "\("⚠️") [\(context.threadName())]\(context.addSpacer(" ", to: context.message)) -- \(context.lineInfo())"
            case .error:
                formattedMessage = "\("❌") [\(context.threadName())]\(context.addSpacer(" ", to: context.message)) -- \(context.lineInfo())"
            case .fatal:
                formattedMessage = "\("🔥") [\(context.threadName())]\(context.addSpacer(" ", to: context.message)) -- \(context.lineInfo())"
            case .deinit:
                formattedMessage = "\("❎ DEINIT")\(context.addSpacer(" ", to: context.message)) -- \(context.lineInfo())"
        }

        os_log("%s", formattedMessage)
    }
}

GitHub

link
Stars: 2
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Update log format
3 weeks ago

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