All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wvlet.log.Handler.scala Maven / Gradle / Ivy

There is a newer version: 24.9.0
Show newest version
package wvlet.log

import java.io.{PrintStream, PrintWriter}
import java.util.{logging => jl}

/**
  * Output log to stderr
  *
  * @param formatter
  */
class ConsoleLogHandler(formatter: LogFormatter, out: PrintStream = LogEnv.defaultConsoleOutput) extends jl.Handler {
  override def publish(record: jl.LogRecord): Unit = {
    out.println(formatter.format(record))
  }
  override def flush(): Unit = System.err.flush()
  override def close(): Unit = {}
}

/**
  * Handlers for discarding logs
  */
object NullHandler extends jl.Handler {
  override def flush(): Unit = {
    // do nothing
  }
  override def publish(record: jl.LogRecord): Unit = {
    // do nothing
  }
  override def close(): Unit = {
    // do nothing
  }
}

/**
  * Handlers for storing log messages as a sequence. This is useful for debugging.
  *
  * @param formatter
  */
class BufferedLogHandler(formatter: LogFormatter) extends jl.Handler {
  private val buf = Seq.newBuilder[String]

  override def flush(): Unit = {}
  override def publish(record: jl.LogRecord): Unit =
    synchronized {
      val log = formatter.format(record)
      buf += log
    }
  override def close(): Unit = {
    // do nothing
  }

  def logs: Seq[String] = buf.result()

  def clear: Unit = {
    buf.clear()
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy