Testing
When using the colog
loggers in our code, we have the posibility of testing that the expected log statements are actually produced by using a pure logger type in our testing code.
The pure logger can be instantiated using Loggers.pure
and we need to provide it with a type that is able to buffer the log statements in memory for us. In the core
module we have the MemLog
(or it's transformer sibling MemLogT
) type which we could use out of the box for that purpose:
type BufferedLog[A] = MemLog[String, A]
val bufferL = Loggers.pure[BufferedLog, String]
// bufferL: Logger[BufferedLog, String] = Logger(
// colog.Loggers$$$Lambda$16019/411295653@7191245b
// )
So now we can use that newly defined logger to emit our log statements:
val logged = bufferL.log("Hello")
// logged: BufferedLog[Unit] = WriterT((Vector("Hello"), ()))
The log statements have been accumulated in memory by a Writer monad, so we need to run it to obtain them:
val (logs, _) = logged.run
// logs: Vector[String] = Vector("Hello")
Loggers.pure
can work with any functor that supports the tell
operation (known as FunctorTell
in cats-mtl
), a writer monad is one of them (and that's what the MemLog
type is) although it is not required to use it as long as a resolvable instance of a FunctorTell[F[_], Vector[A]]
is available.