Allow clients to provide a custom log destination#156
Allow clients to provide a custom log destination#156zoejessica wants to merge 5 commits intomainfrom
Conversation
svara
left a comment
There was a problem hiding this comment.
Nice addition @zoejessica 👌
b715fc7 to
349a191
Compare
Source/HotwireConfig.swift
Outdated
| /// | ||
| /// If no custom logger is provided, Hotwire will use the system `OSLog` framework. | ||
| /// Log messages will stream to Xcode's console and the Console app. | ||
| public var logger: HotwireLogger? { |
There was a problem hiding this comment.
I would prefer that this is not nullable and we set this to the default implementation here. Then, any place that needs to obtain the current logger instance can do it through Hotwire.config.logger and not need to hold additional state/logic for getting the custom instance or default instance.
| public protocol HotwireLogger { | ||
| func debug(_ message: String, file: StaticString, function: StaticString, line: UInt) | ||
| func info(_ message: String, file: StaticString, function: StaticString, line: UInt) | ||
| func notice(_ message: String, file: StaticString, function: StaticString, line: UInt) | ||
| func error(_ message: String, file: StaticString, function: StaticString, line: UInt) | ||
| func warning(_ message: String, file: StaticString, function: StaticString, line: UInt) | ||
| func fault(_ message: String, file: StaticString, function: StaticString, line: UInt) | ||
| } |
There was a problem hiding this comment.
We have introduced a var logLevel: HotwireLogLevel as part of the HotwireLogger interface on the Android side. This lets apps set the log level directly on the logger instance via Hotwire.config.logger.logLevel = HotwireLogLevel.ERROR. Should we consider the same thing for iOS?
There was a problem hiding this comment.
Reading the commit, this is where the user can pass in a log level, and any messages that don't have that or a higher criticality are excluded from the logs, correct?
I'm a bit wary of adding it on iOS, for the same reason that we removed the debugLogEnabled flag on this platform. We're sticking fairly close to the default system OSLog implementation, so it seems right to also lean on its handling of different levels. OSLog treats different log levels automatically, for example, a production app on device does not persist any DEBUG messages. (Which I think is quite different from the Android side?) So as long as we're being sensible about the kind of data we're sending to persistable-by-default logs, I think this makes sense.
The log level you choose determines how the system handles the message. The system stores all messages in memory initially, and it writes messages with more severe log levels to disk.
Users who were set on changing this behaviour could do it trivially in a custom implementation of HotwireLogger. For instance, if you wanted to persist all HN log messages regardless of how we've happened to categorize them, you could wrap an instance of OSLog.Logger and forward all the logs to the ERROR level. Or provide your own log level as a filter.
Co-authored-by: Joe Masilotti <joe@masilotti.com>
For consistency between Android and iOS, we are removing this flag. By default on iOS, Hotwire will use the OSLog framework which provides persistence behaviour based on log level. Debug level log messages are never persisted. If you need to customize this behaviour, provide a custom logger.
349a191 to
3411eaa
Compare
Introduces a new
logDestinationproperty onHotwireConfig. When clients set this property, and setdebugLoggingEnabledtotrue, Hotwire will send logs to this destination instead of the defaultOSLog.Loggerthat's currently used.Notes
debugLoggingEnabledisfalse, logs go to the samedisabledOSLog logger as beforepublicHotwireOSLoggerWrapperfor easy injection of an existing OSLog logger