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

scribe.writer.CacheWriter.scala Maven / Gradle / Ivy

There is a newer version: 3.15.3
Show newest version
package scribe.writer
import scribe.LogRecord
import scribe.output.LogOutput
import scribe.output.format.OutputFormat

class CacheWriter(max: Int = CacheWriter.DefaultMax) extends Writer {
  private var recordCache = List.empty[LogRecord]
  private var outputCache = List.empty[LogOutput]

  override def write(record: LogRecord, output: LogOutput, outputFormat: OutputFormat): Unit = synchronized {
    recordCache = (record :: recordCache).take(max)
    outputCache = (output :: outputCache).take(max)
  }

  def records: List[LogRecord] = recordCache
  def output: List[LogOutput] = outputCache

  def consume[Return](f: List[LogRecord] => Return): Return = try {
    f(records)
  } finally {
    clear()
  }

  def consumeMessages[Return](f: List[String] => Return): Return = consume { list =>
    f(list.map(_.messages.map(_.logOutput.plainText).mkString(" ")))
  }

  def clear(): Unit = synchronized {
    recordCache = Nil
    outputCache = Nil
  }
}

object CacheWriter {
  val DefaultMax: Int = 100
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy