Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ComposedLoggedValue: single LoggedValue as several LoggedValues on the top-level #1037

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}