Skip to content

Commit

Permalink
introduce ComposedLoggedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
vagroz committed Nov 14, 2022
1 parent 0605c23 commit 2cbf36b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package tofu.logging

import cats.kernel.Semigroup
import tofu.logging.impl.ComposedLoggedValue

import scala.{specialized => sp}

trait LoggedValue {
Expand Down Expand Up @@ -30,6 +33,10 @@ object LoggedValue {
implicit def loggableToLoggedValue[A](x: A)(implicit loggable: Loggable[A]): LoggedValue = loggable.loggedValue(x)

def error(cause: Throwable): LoggedThrowable = new LoggedThrowable(cause)

implicit val loggedValueSemigroup: Semigroup[LoggedValue] = Semigroup.instance { (a, b) =>
new ComposedLoggedValue(a :: b :: Nil)
}
}

final class LoggedThrowable(cause: Throwable) extends Throwable(cause.getMessage, cause) with LoggedValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tofu.logging.impl

import tofu.logging.{LogRenderer, LoggedValue}

/** This is supposed to be used to log several `LoggedValue` as if they were passed as arguments to the logging method.
* E.g. to provide single `LoggedValue` into `ContextMarker`. Be careful: the resulting structured log may contain the
* same fields.
*/
class ComposedLoggedValue(values: Iterable[LoggedValue]) extends LoggedValue {
override def toString: String = values.map(_.toString).mkString(", ")

override def logFields[I, V, @specialized R, @specialized M](input: I)(implicit r: LogRenderer[I, V, R, M]): R =
values.foldLeft(r.noop(input)) { (acc, value) =>
r.combine(acc, value.logFields(input))
}
}

0 comments on commit 2cbf36b

Please sign in to comment.