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

com.malliina.logback.fs2.fs2Appenders.scala Maven / Gradle / Ivy

There is a newer version: 2.8.0
Show newest version
package com.malliina.logback.fs2

import cats.effect.{Concurrent, IO}
import ch.qos.logback.core.AppenderBase
import fs2.Stream
import fs2.concurrent.{SignallingRef, Topic}

class FS2IOAppender[E]()(implicit c: Concurrent[IO]) extends FS2Appender[E] {
  override def append(eventObject: E): Unit =
    topic.publish1(Option(eventObject)).unsafeRunAsyncAndForget()
}

abstract class FS2Appender[E](implicit c: Concurrent[IO]) extends AppenderBase[E] {
  val topic = Topic[IO, Option[E]](None).unsafeRunSync()
  val signal = SignallingRef[IO, Boolean](false).unsafeRunSync()
  val source: Stream[IO, E] = topic
    .subscribe(maxQueued = 10)
    .drop(1)
    .flatMap(opt => opt.map(e => Stream(e)).getOrElse(Stream.empty))
    .interruptWhen(signal)

  def close(): Unit = signal.set(true).unsafeRunSync()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy