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 Apple System Log Logging #81

Merged
merged 2 commits into from
Sep 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -50,6 +50,14 @@ Log.dispatcher.install(ConsoleLogger)

Custom loggers can be created by implementing the [`Logger`] interface.

#### Apple (NSLog)

Log to the Apple System Log by installing the `AppleSystemLogger`.

```kotlin
Log.dispatcher.install(AppleSystemLogger)
```

### Log

Log message can be logged via:
38 changes: 38 additions & 0 deletions logging/src/appleMain/kotlin/AppleSystemLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.juul.tuulbox.logging

import platform.Foundation.NSLog

public object AppleSystemLogger : Logger {

override fun verbose(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log("V", tag, message, throwable)
}

override fun debug(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log("D", tag, message, throwable)
}

override fun info(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log("I", tag, message, throwable)
}

override fun warn(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log("W", tag, message, throwable)
}

override fun error(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log("E", tag, message, throwable)
}

override fun assert(tag: String, message: String, metadata: ReadMetadata, throwable: Throwable?) {
log("A", tag, message, throwable)
}

private fun log(level: String, tag: String, message: String, throwable: Throwable?) {
if (throwable == null) {
NSLog("%s/%s: %s", level, tag, message)
} else {
NSLog("%s/%s: %s\n%s", level, tag, message, throwable.stackTraceToString())
}
}
}