Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for os_log on Apple platforms #227

Merged
merged 3 commits into from May 20, 2022
Merged

Conversation

conradev
Copy link
Contributor

@conradev conradev commented Apr 9, 2022

Apple platforms (macOS, iOS, watchOS, tvOS) have a unified logging system called os_log. I implemented support for os_log and made it the default on Apple platforms. This is similar to how slf4j-android implements slf4j-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 with os_log_create(2), but if you don't have either you can just use OS_LOG_DEFAULT

My implementation makes OS_LOG_DEFAULT the default for Apple platforms, but you can specify a subsystem with:

KotlinLoggingConfiguration.appender = OSLogSubsystemAppender("com.example.app")

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, the loggerName will instead be used as the category. Similar to the subsystem and the category os_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 a OSLogSubsystemAppender, 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 my commonMain and It Just Works™

This implementation is shared across all native implementations
On Darwin platforms using os_log, a separate logger can be created for
each differently named logger. The Appender, which directs the text of
the log message, needs to know which logger to direct it to.
@oshai
Copy link
Owner

oshai commented Apr 12, 2022

Thanks for the PR! I will look into it as soon as possible.

@oshai
Copy link
Owner

oshai commented Apr 20, 2022

LGTM!
Do you want to also create a documentation for that (in the wiki?) once merged?

@conradev
Copy link
Contributor Author

Yes, I can do that! I will also make sure that the README is updated in the commit before merge

@oshai oshai merged commit f089853 into oshai:master May 20, 2022
@oshai
Copy link
Owner

oshai commented May 20, 2022

Thanks for the PR!

oshai added a commit that referenced this pull request Aug 9, 2023
based on previous impl and https://blog.shipbook.io/ios-logs

looks like a better approach will be to just delegate logs to the os logger.

* change subsystem and category resolution based on #227
* add constant logger
* add constant constantOsDefaultLogger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants