You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently Android is supported via slf4j extension which have issues and seems deprecated for that use. In #121 it was suggested to use Kotlin Multi Platform and have a module for android separated from the regular jvm module.
Below is a code sample that can assist.
Basically such module should be similar to the js module without slf4j dependency. Here are the docs for android multiplatform support.
importandroid.util.Logimportmu.KLoggableimportmu.KLoggerimportmu.Markerimportmu.KLoggerFactoryimportjava.lang.reflect.Modifier/** * Android Logging implementation of [KLogger]*/classAndroidLogger(overridevalname:String) : KLogger {
// No implementation, "catching" support not applied to android logger, unlikely to be used at this pointoverridefun <T:Throwable> catching(throwable:T) =Unitoverridefundebug(msg: () ->Any?) {
if (Log.isLoggable(name, Log.DEBUG)) {
Log.d(name, msg.toStringSafely())
}
}
overridefundebug(t:Throwable?, msg: () ->Any?) {
if (Log.isLoggable(name, Log.DEBUG)) {
Log.d(name, msg.toStringSafely(), t)
}
}
// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefundebug(marker:Marker?, msg: () ->Any?) =Unit// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefundebug(marker:Marker?, t:Throwable?, msg: () ->Any?) =Unit// No implementation, "entry" support not applied to android logger, unlikely to be used at this pointoverridefunentry(varargargArray:Any?) =Unitoverridefunerror(msg: () ->Any?) {
if (Log.isLoggable(name, Log.ERROR)) {
Log.e(name, msg.toStringSafely())
}
}
overridefunerror(t:Throwable?, msg: () ->Any?) {
if (Log.isLoggable(name, Log.ERROR)) {
Log.e(name, msg.toStringSafely(), t)
}
}
// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefunerror(marker:Marker?, msg: () ->Any?) =Unit// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefunerror(marker:Marker?, t:Throwable?, msg: () ->Any?) =Unit// No implementation, "exit" support not applied to android logger, unlikely to be used at this pointoverridefunexit() =Unit// No implementation, "exit" support not applied to android logger, unlikely to be used at this pointoverridefun <T> exit(result:T): T=throwNotImplementedError("Exit not supported by android logger")
overridefuninfo(msg: () ->Any?) {
if (Log.isLoggable(name, Log.INFO)) {
Log.i(name, msg.toStringSafely())
}
}
overridefuninfo(t:Throwable?, msg: () ->Any?) {
if (Log.isLoggable(name, Log.INFO)) {
Log.i(name, msg.toStringSafely(), t)
}
}
// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefuninfo(marker:Marker?, msg: () ->Any?) =Unit// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefuninfo(marker:Marker?, t:Throwable?, msg: () ->Any?) =Unit// No implementation, "throwing" support not applied to android logger, unlikely to be used at this pointoverridefun <T:Throwable> throwing(throwable:T): T=throwNotImplementedError("throwing not supported by android logger")
overridefuntrace(msg: () ->Any?) {
if (Log.isLoggable(name, Log.VERBOSE)) {
Log.v(name, msg.toStringSafely())
}
}
overridefuntrace(t:Throwable?, msg: () ->Any?) {
if (Log.isLoggable(name, Log.VERBOSE)) {
Log.v(name, msg.toStringSafely(), t)
}
}
// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefuntrace(marker:Marker?, msg: () ->Any?) =Unit// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefuntrace(marker:Marker?, t:Throwable?, msg: () ->Any?) =Unitoverridefunwarn(msg: () ->Any?) {
if (Log.isLoggable(name, Log.WARN)) {
Log.w(name, msg.toStringSafely())
}
}
overridefunwarn(t:Throwable?, msg: () ->Any?) {
if (Log.isLoggable(name, Log.WARN)) {
Log.w(name, msg.toStringSafely(), t)
}
}
// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefunwarn(marker:Marker?, msg: () ->Any?) =Unit// No implementation, "marker" support not applied to android logger, unlikely to be used at this pointoverridefunwarn(marker:Marker?, t:Throwable?, msg: () ->Any?) =Unit
}
privatefun (() ->Any?)?.toStringSafely() =this?.invoke()?.toString().orEmpty()
/** * [KLoggerFactory] that emits [android.util.Log] backed log entries*/classAndroidLoggerFactory : KLoggerFactory {
overridefunlogger(func: () ->Unit): KLogger= logger(name(func))
overridefunlogger(name:String): KLogger=AndroidLogger(name)
overridefunlogger(loggable:KLoggable): KLogger= logger(unwrapCompanionClass(loggable.javaClass).name)
}
/** * unwrap companion class to enclosing class given a Java Class*/privatefun <T:Any> unwrapCompanionClass(clazz:Class<T>): Class<*> {
clazz.enclosingClass?.also { enclosingClass ->try {
val field = enclosingClass.getField(clazz.simpleName)
if (Modifier.isStatic(field.modifiers) && field.type == clazz) {
// && field.get(null) === obj// the above might be safer but problematic with initialization orderreturn enclosingClass
}
} catch (e:Exception) {
// ok, it is not a companion object
}
}
return clazz
}
/** * get class name for function by the package of the function*/privatefunname(func: () ->Unit): String {
val name = func.javaClass.name
returnwhen {
name.contains("Kt$") -> name.substringBefore("Kt$")
name.contains("$") -> name.substringBefore("$")
else-> name
}
}
The text was updated successfully, but these errors were encountered:
Currently Android is supported via slf4j extension which have issues and seems deprecated for that use. In #121 it was suggested to use Kotlin Multi Platform and have a module for android separated from the regular jvm module.
Below is a code sample that can assist.
Basically such module should be similar to the js module without slf4j dependency.
Here are the docs for android multiplatform support.
The text was updated successfully, but these errors were encountered: