log4cats icon indicating copy to clipboard operation
log4cats copied to clipboard

Are values in the MDC intended to be include in the context logged by Slf4jLogger?

Open morgen-peschke opened this issue 8 months ago • 4 comments

While adding tests to Slf4jLogger in #850 I noticed that, every now and again, the tests would fail because extra values would be captured as context.

Looking at the failure, it's the value set in the "Slf4jLoggerInternal resets after logging" test. I didn't see in the docs if this was intended behavior or not, and I could see arguments for either behavior, so I figured it's worth asking about and documenting.

munit.ComparisonFailException: /Users/morgen/learning/log4cats/slf4j/src/test/scala/org/typelevel/log4cats/slf4j/internal/Slf4jLoggerInternalSuite.scala:256
255:      slf4jLogger.error("error(msg)").assert >>
256:      IO(testLogger.logs().asScala.toList).assertEquals(
257:        List(
values are not the same
=> Obtained
List(
  DeferredStructuredLogMessage(LogLevel.Trace,Map(foo -> yellow),None,trace(msg)),
  DeferredStructuredLogMessage(LogLevel.Debug,Map(foo -> yellow),None,debug(msg)),
  DeferredStructuredLogMessage(LogLevel.Info,Map(foo -> yellow),None,info(msg)),
  DeferredStructuredLogMessage(LogLevel.Warn,Map(foo -> yellow),None,warn(msg)),
  DeferredStructuredLogMessage(LogLevel.Error,Map(foo -> yellow),None,error(msg))
)
=> Diff (- obtained, + expected)
 List(
-  DeferredStructuredLogMessage(LogLevel.Trace,Map(foo -> yellow),None,trace(msg)),
-  DeferredStructuredLogMessage(LogLevel.Debug,Map(foo -> yellow),None,debug(msg)),
-  DeferredStructuredLogMessage(LogLevel.Info,Map(foo -> yellow),None,info(msg)),
-  DeferredStructuredLogMessage(LogLevel.Warn,Map(foo -> yellow),None,warn(msg)),
-  DeferredStructuredLogMessage(LogLevel.Error,Map(foo -> yellow),None,error(msg))
+  DeferredStructuredLogMessage(LogLevel.Trace,Map(),None,trace(msg)),
+  DeferredStructuredLogMessage(LogLevel.Debug,Map(),None,debug(msg)),
+  DeferredStructuredLogMessage(LogLevel.Info,Map(),None,info(msg)),
+  DeferredStructuredLogMessage(LogLevel.Warn,Map(),None,warn(msg)),
+  DeferredStructuredLogMessage(LogLevel.Error,Map(),None,error(msg))
 )

morgen-peschke avatar Jun 14 '24 02:06 morgen-peschke