Skip to content

feat!: add ContextPair to StructuredLogger #903

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -17,7 +17,6 @@
package org.typelevel.log4cats

import cats.*
import cats.Show.Shown

trait SelfAwareStructuredLogger[F[_]] extends SelfAwareLogger[F] with StructuredLogger[F] {
override def mapK[G[_]](fk: F ~> G): SelfAwareStructuredLogger[G] =
Expand All @@ -26,13 +25,6 @@ trait SelfAwareStructuredLogger[F[_]] extends SelfAwareLogger[F] with Structured
override def addContext(ctx: Map[String, String]): SelfAwareStructuredLogger[F] =
SelfAwareStructuredLogger.withContext(this)(ctx)

override def addContext(
pairs: (String, Shown)*
): SelfAwareStructuredLogger[F] =
SelfAwareStructuredLogger.withContext(this)(
pairs.map { case (k, v) => (k, v.toString) }.toMap
)

override def withModifiedString(f: String => String): SelfAwareStructuredLogger[F] =
SelfAwareStructuredLogger.withModifiedString[F](this, f)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.typelevel.log4cats

import cats.*
import cats.syntax.foldable.*
import cats.Show.Shown
import cats.Show.ContravariantShow

trait StructuredLogger[F[_]] extends Logger[F] {
def trace(ctx: Map[String, String])(msg: => String): F[Unit]
Expand All @@ -36,18 +38,27 @@ trait StructuredLogger[F[_]] extends Logger[F] {
def addContext(ctx: Map[String, String]): StructuredLogger[F] =
StructuredLogger.withContext(this)(ctx)

def addContext(pair: StructuredLogger.ContextPair): StructuredLogger[F] =
addContext(pair.toMap)

def addContext(
pairs: (String, Shown)*
pairs: StructuredLogger.ContextPair*
): StructuredLogger[F] =
StructuredLogger.withContext(this)(
pairs.map { case (k, v) => (k, v.toString) }.toMap
)
addContext(pairs.toList.foldMap(_.toMap))

override def withModifiedString(f: String => String): StructuredLogger[F] =
StructuredLogger.withModifiedString[F](this, f)
}

object StructuredLogger {

final case class ContextPair(value: (String, Shown)) extends AnyVal {
def toMap: Map[String, String] = Map(value._1 -> value._2.toString)
}

implicit def log4catsTupleToContextPair[A: ContravariantShow](pair: (String, A)): ContextPair =
ContextPair((pair._1, Shown.mat(pair._2)))

def apply[F[_]](implicit ev: StructuredLogger[F]): StructuredLogger[F] = ev

def withContext[F[_]](sl: StructuredLogger[F])(ctx: Map[String, String]): StructuredLogger[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import cats.data.Chain
import cats.effect.kernel.Resource.ExitCase
import cats.effect.kernel.{Concurrent, Ref, Resource}
import cats.syntax.all.*
import cats.{~>, Show}
import cats.~>
import org.typelevel.log4cats.SelfAwareStructuredLogger

/**
Expand All @@ -39,11 +39,6 @@ trait DeferredSelfAwareStructuredLogger[F[_]]
override def addContext(ctx: Map[String, String]): DeferredSelfAwareStructuredLogger[F] =
DeferredSelfAwareStructuredLogger.withContext(this)(ctx)

override def addContext(pairs: (String, Show.Shown)*): DeferredSelfAwareStructuredLogger[F] =
DeferredSelfAwareStructuredLogger.withContext(this)(
pairs.map { case (k, v) => (k, v.toString) }.toMap
)

override def withModifiedString(f: String => String): DeferredSelfAwareStructuredLogger[F] =
DeferredSelfAwareStructuredLogger.withModifiedString(this, f)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.typelevel.log4cats.extras

import cats.Show.Shown
import cats.data.Chain
import cats.effect.kernel.Resource.ExitCase
import cats.effect.kernel.{Concurrent, Ref, Resource}
Expand Down Expand Up @@ -56,11 +55,6 @@ trait DeferredStructuredLogger[F[_]] extends StructuredLogger[F] with DeferredLo
override def addContext(ctx: Map[String, String]): DeferredStructuredLogger[F] =
DeferredStructuredLogger.withContext(this, ctx)

override def addContext(
pairs: (String, Shown)*
): DeferredStructuredLogger[F] =
DeferredStructuredLogger.withContext(this, pairs.map { case (k, v) => (k, v.toString) }.toMap)

override def withModifiedString(f: String => String): DeferredStructuredLogger[F] =
DeferredStructuredLogger.withModifiedString[F](this, f)
}
Expand Down
Loading