Skip to content

Commit

Permalink
Make klogger use java.util.logging.* on the JVM (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Mar 24, 2023
1 parent 8bfef88 commit d06059b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions klogger/src/commonMain/kotlin/com/soywiz/klogger/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import com.soywiz.klogger.internal.*
* ```
*/
class Logger private constructor(val name: String, val normalizedName: String, val dummy: Boolean) {
var nativeLogger: Any? = null

var optLevel: Level? = null
var optOutput: Output? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package com.soywiz.klogger

import java.util.logging.Level

actual object DefaultLogOutput : Logger.Output {
override fun output(logger: Logger, level: Logger.Level, msg: Any?) = Logger.ConsoleLogOutput.output(logger, level, msg)
override fun output(logger: Logger, level: Logger.Level, msg: Any?) {
if (logger.nativeLogger == null) {
logger.nativeLogger = java.util.logging.Logger.getLogger(logger.name)
}
//println("logger=$logger, level=$level, msg=$msg")
(logger.nativeLogger as java.util.logging.Logger).log(level.toJava(), msg.toString())
}
}

fun Logger.Level.toJava(): Level = when (this) {
Logger.Level.NONE -> Level.OFF
Logger.Level.FATAL -> Level.SEVERE
Logger.Level.ERROR -> Level.SEVERE
Logger.Level.WARN -> Level.WARNING
Logger.Level.INFO -> Level.FINE
Logger.Level.DEBUG -> Level.FINEST
Logger.Level.TRACE -> Level.ALL
}

0 comments on commit d06059b

Please sign in to comment.