Logger

This package is a thin wrapper for os.Logger.

MIT License Platform: iOS 14.0+|iPadOS 14.0+|macOS 11.0+|Mac Catalyst 14.0+|tvOS 14.0+|watchOS 7.0+| Swift: 5.5 Swift Package Manager compatible Xcode Build

Usage

  1. Define os.Logger as an extension of Logger with the property name of the string that the system uses to categorize emitted signposts.
  2. Call Logger.configure(bundleIdentifier:) to set a string that identifies the subsystem that emits signposts.
  3. Write a message to the log using os.Logger defined in “1.”

import Logger

extension Logger {
    static var categoryName1: os.Logger { logger() } // The property name automatically becomes the category name
    static var categoryName2: os.Logger { logger("Category Name 2") } // Customize the category name
}

struct MyApp: App {
    init() {
        Logger.configure()
    }
    
    var body: some Scene {
        WindowGroup {
            Text("Hello, logger world!")
                .onAppear {
                    Logger.categoryName1.log(level: .info, "categoryName1's OSLogMessage")
                    // 1998-06-21 09:41:00.000000+0900 LoggerExample[88450:10207919] [categoryName1] categoryName1's OSLogMessage
                    
                    Logger.categoryName2.log(level: .info, "Category Name 2's OSLogMessage")
                    // 1998-06-21 09:41:00.000100+0900 LoggerExample[88450:10207919] [Category Name 2] Category Name 2's OSLogMessage
                    
                    Logger.categoryName1
                        .handleLogger { _ in print("some action") }
                        .log(level: .debug, "categoryName1's OSLogMessage")
                }
        }
    }
}

GitHub

View Github