LoggerFactory

main

Simple convenience methods to make it easier to define and use OSLog Loggers.
DandyLyons/LoggerFactory

LoggerFactory

A LoggerFactory defines all the categies within one particular subsystem and makes it easy to create consistent Loggers within your codebase.

Logger is a much better print()

Most of us simply use print(), but there are a variety of problems with printing including:

  1. It's easy to accidentally leak private user data.
  2. print() gives you no context for what called print() or where it was called.

To address these issues and others Apple provides the Unified Logging System which can be used through the Logger type.

Advantages of Logger:

  1. separates "print" calls into subsystems
  2. separates subsystems into categories
  3. automatically redacts sensitive user data

Disadvantages

  1. It's just easier to print()... until now.

Now it's easy to define reusable, consistent, organized Loggers which can be used throughout your codebase.

Defining a LoggerFactory

import Foundation
struct MyAppLogger: LoggerFactory {
    static let subsystem = Bundle.main.bundleIdentifier ?? "MyApp"
    typealias Categories = MyCategories


    enum MyCategories: String, StringRawRepresentable {
        case settings, networking, appLifeTime
    }
}

Using a Logger

MyAppLogger.logger(.appLifeTime).info("App is now active.")

Or you can reuse loggers.

let logger = MyAppLogger.logger(.appLifeTime)
logger.info("App is now active.")
// somewhere else...
logger.info("App will soon be inactive.")

Logging Errors

The library also includes convenience methods for logging Swift Errors.

do {
    try throwingFunction()
} catch {
    logger.error(error)
    logger.error(error) { e in
        "throwingFunction threw Error: \(e.localizedDescription)"
    }
}

License

The library is released under the MIT License.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue May 13 2025 00:03:16 GMT-0900 (Hawaii-Aleutian Daylight Time)