Swiftpack.co -  Package - janodev/log
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
janodev/log
Log handler for Swift-Log
.package(url: "https://github.com/janodev/log.git", from: "1.0.3")

Swift

Appenders for swift-log.

What is this?

This package contains three appenders for Apple’s swift-log:

  • NoopLogger: ignores log.
  • OSLogLogger: sends log towards os_log.
  • PrintLogger: sends log to console.

I suggest you use PrintLogger. os_log is designed as a system logger and it’s not practical to use for debugging.

Additional information:

Installation

Import the package for swift-log and this package and add the dependencies to your target.

.package(url: "https://github.com/apple/log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
...
.target(name: "ExampleApp", dependencies: ["Log", "Logging"])

Usage

Create a file with a global variable:

var logger: Logger = {
    #if DEBUG
        LoggingSystem.bootstrap(PrintLogger.init)
    #else
        LoggingSystem.bootstrap(NoopLogger.init)
    #endif
    var logger = Logger(label: "your.bundle.identifier")
    logger.logLevel = .trace
    return logger
}()

Then log like this:

log.debug("You clicked on index path \(indexPath)")

The argument must be a string, not a variable. You can change the log level setting logger.logLevel.

Running LoggingSystem.bootstrap more than once in an application results in undefined behavior/crashes. If you have several frameworks you may want to create a file per package, but call LoggingSystem.bootstrap from the main app only.

OSLogLogger

Output looks like this:

2020-01-15 00:02:50.242311+0100 TeamworkProjects[14722:6965943] [data] request(resource:):47 - debug: *** Method GET
2020-01-15 00:02:50.242402+0100 TeamworkProjects[14722:6965943] [data] request(resource:):48 - debug: *** Body: No body

The [data] part is the last component of the bundle identifier. Unfortunately you can’t avoid the garbage before that. Logs are visible in Xcode, Console.app, or in the terminal. Unfortunately the Console doesn’t show levels lower than error, even if you choose Action > Include Debug Messages.

PrintLogger

Output prints [package] file.method:line · message:

[data]     WebService.request(resource:): 47 · *** Method GET
[data]     WebService.request(resource:): 48 · *** Body: No body

This appender is inefficient but useful.

  • It doesn’t print elements that are useless in the Xcode console, like the full timestamp and project name.
  • It aligns the method name so it is easy to scan.
  • It includes a character · so you can discard OS_Activity garbage and multiline logs.

Try activating it in the AppDelegate:

// let handlerFactory = OSLogLogger.factory()
// LoggingSystem.bootstrap(handlerFactory)
LoggingSystem.bootstrap(PrintLogger.init)

GitHub

link
Stars: 1
Last commit: 5 days ago

Dependencies

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