From d71f5b0f46807461065400b2133a73728c193834 Mon Sep 17 00:00:00 2001 From: Cedrick Cooke Date: Tue, 31 Aug 2021 14:38:52 -0700 Subject: [PATCH 1/2] Add AppleSystemLogger for bridging Tuulbox -> Apple System Log --- .../src/appleMain/kotlin/AppleSystemLogger.kt | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 logging/src/appleMain/kotlin/AppleSystemLogger.kt diff --git a/logging/src/appleMain/kotlin/AppleSystemLogger.kt b/logging/src/appleMain/kotlin/AppleSystemLogger.kt new file mode 100644 index 00000000..79fcf1cd --- /dev/null +++ b/logging/src/appleMain/kotlin/AppleSystemLogger.kt @@ -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()) + } + } +} From e9e430e8a870ff57de621c16b9fce8374fd4836b Mon Sep 17 00:00:00 2001 From: Cedrick Cooke Date: Tue, 31 Aug 2021 14:52:25 -0700 Subject: [PATCH 2/2] Update readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index eeec14aa..031de412 100644 --- a/README.md +++ b/README.md @@ -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: