Skip to content

Commit d6fddf4

Browse files
author
Denys Fakhritdinov
committed
do not leave null as mdc map even if mdc is empty
1 parent bd2139c commit d6fddf4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

logback/src/main/scala/com/evolutiongaming/catshelper/LogOfFromLogback.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.evolutiongaming.catshelper.Log.Mdc
88

99
import scala.collection.JavaConverters._
1010

11+
// format: off
1112
/**
1213
* ===Motivation===
1314
* Direct logback usage required to overcome limitations of SLF4J MDC API.
@@ -19,6 +20,7 @@ import scala.collection.JavaConverters._
1920
* Please be aware that using other version of logback (than used in `cats-helper-logback`) might bring '''RUNTIME ERRORS''' or '''MISSING LOGS''' in case of binary incompatibility between them.
2021
* Suggested approach is in using exactly same logback version as used in `cats-helper-logback` (among all others available through transitive dependencies)
2122
*/
23+
// format: on
2224
object LogOfFromLogback {
2325

2426
def apply[F[_]: Sync]: F[LogOf[F]] =
@@ -41,16 +43,20 @@ object LogOfFromLogback {
4143

4244
val FQCN = getClass.getName
4345

44-
def append(msg: => String,
45-
mdc: Mdc,
46-
level: Level,
47-
throwable: Throwable = null): F[Unit] = Sync[F].delay {
46+
def append(
47+
msg: => String,
48+
mdc: Mdc,
49+
level: Level,
50+
throwable: Throwable = null
51+
): F[Unit] = Sync[F].delay {
4852
if (logger.isEnabledFor(level)) {
4953
val event =
5054
new LoggingEvent(FQCN, logger, level, msg, throwable, null)
51-
mdc.context.map(_.toSortedMap) foreach { mdc =>
52-
event.setMDCPropertyMap(mdc.asJava)
55+
val mdc1 = mdc.context match {
56+
case Some(mdc) => mdc.toSortedMap.asJava
57+
case None => new java.util.HashMap[String, String]
5358
}
59+
event.setMDCPropertyMap(mdc1)
5460
logger.callAppenders(event)
5561
}
5662
}

0 commit comments

Comments
 (0)