Contents

DandyLyons/LoggerFactory

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

`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.

Package Metadata

Repository: DandyLyons/LoggerFactory

Stars: 0

Forks: 0

Open issues: 0

Default branch: main

Primary language: swift

License: MIT

README: README.md