Add support for os_log on Apple platforms #227
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Apple platforms (macOS, iOS, watchOS, tvOS) have a unified logging system called
os_log
. I implemented support foros_log
and made it the default on Apple platforms. This is similar to howslf4j-android
implementsslf4j-api
to write to the Android system log.I kept the console output appender still available on Apple platforms because it might still be desirable to use it in, for example, a command line tool.
os_log
has two key concepts – a subsystem and a category. A subsystem is intended to be constant for an entire project (i.e. the reverse DNS notation for the project) and the category is supposed to change based on the component. Neither are usually part of the log message string itself, but rather used for filtering the log messages. If you have a subsystem and a category, you can create a log object withos_log_create(2)
, but if you don't have either you can just useOS_LOG_DEFAULT
My implementation makes
OS_LOG_DEFAULT
the default for Apple platforms, but you can specify a subsystem with:If you are using the default log, the
loggerName
is logged as a part of the message, but if you are using a specific subsystem, theloggerName
will instead be used as the category. Similar to the subsystem and the categoryos_log
also makes the level a parameter of the message itself (and something you can filter on), instead of as a part of the message text.I hide the log level and
loggerName
if you are using aOSLogSubsystemAppender
, but this part of the diff is a bit rough, and I'd love your thoughts on which direction it should go. For the time being, it works for my needs :)os_log
also has the ability to disable log levels for performance, and my implementation supports that as well.Thank you for writing this library, it is working great for me! Feel free to suggest changes, or to just make them yourself – the branch is yours 😊
One small note is that Kotlin multiplatform handles the artifact multiplexing automatically – I am able to just specify the single
io.github.microutils:kotlin-logging
dependency in mycommonMain
and It Just Works™