|
| 1 | +import Foundation |
| 2 | +import os.log |
| 3 | + |
| 4 | +@objc |
| 5 | +public final class StytchConsoleLogger: NSObject { |
| 6 | + // Create a logger with subsystem and category for filtering in Console |
| 7 | + // In Console.app you can filter using: |
| 8 | + // subsystem:com.stytch.sdk |
| 9 | + // category:console |
| 10 | + // You can also combine filters, for example: |
| 11 | + // subsystem:com.stytch.sdk level:error |
| 12 | + private static let logger = Logger(subsystem: "com.stytch.sdk", category: "console") |
| 13 | +} |
| 14 | + |
| 15 | +// These map directly to OSLogType levels and are easy to call. |
| 16 | +// Note: error and warning messages always appear in Console by default. |
| 17 | +// Info messages do NOT appear unless you enable "Action->Include Info Messages" in Console |
| 18 | +// or change log settings via `log config` in Terminal. This is an Apple logging behavior |
| 19 | +// designed to reduce noise, not a limitation of this class. |
| 20 | +public extension StytchConsoleLogger { |
| 21 | + /// Log an informational message |
| 22 | + @objc static func log(message: String) { |
| 23 | + logger.info("\(message, privacy: .public)") |
| 24 | + } |
| 25 | + |
| 26 | + /// Log a warning message |
| 27 | + @objc static func warn(message: String) { |
| 28 | + logger.warning("\(message, privacy: .public)") |
| 29 | + } |
| 30 | + |
| 31 | + /// Log an error message |
| 32 | + @objc static func error(message: String) { |
| 33 | + logger.error("\(message, privacy: .public)") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Use this if you want one entry point with a configurable log level. |
| 38 | +// Callers pass in an OSLogType (.debug, .info, .error, etc). |
| 39 | +// Example: |
| 40 | +// StytchConsoleLogger.log(message: "debugging", type: .debug) |
| 41 | +// StytchConsoleLogger.log(message: "problem!", type: .error) |
| 42 | +public extension StytchConsoleLogger { |
| 43 | + /// Log a message with a custom log level |
| 44 | + @objc static func log(message: String, type: OSLogType = .default) { |
| 45 | + logger.log(level: type, "\(message, privacy: .public)") |
| 46 | + } |
| 47 | +} |
0 commit comments