Skip to content

Commit

Permalink
slf4j bridge - logger name annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Dec 29, 2022
1 parent f974fe2 commit 3ef441b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
9 changes: 4 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ inThisBuild(
)
)

val ZioVersion = "2.0.5"
val scalaJavaTimeVersion = "2.3.0"
val slf4jVersion = "1.7.36"
val slf4j2Version = "2.0.5"
val logbackVersion = "1.2.11"
val ZioVersion = "2.0.5"
val slf4jVersion = "1.7.36"
val slf4j2Version = "2.0.6"
val logbackVersion = "1.2.11"

addCommandAlias("fix", "; all compile:scalafix test:scalafix; all scalafmtSbt scalafmtAll")
addCommandAlias("check", "; scalafmtSbtCheck; scalafmtCheckAll; compile:scalafix --check; test:scalafix --check")
Expand Down
2 changes: 1 addition & 1 deletion project/MimaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sbt.Keys.{ name, organization }
import sbt._

object MimaSettings {
lazy val bincompatVersionToCompare = "2.1.4"
lazy val bincompatVersionToCompare = "2.1.7"

def mimaSettings(failOnProblem: Boolean) =
Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import zio.{ ZIO, ZLayer }

object Slf4jBridge {

val loggerNameAnnotationKey: String = "slf4j_logger_name"

def initialize: ZLayer[Any, Nothing, Unit] =
ZLayer {
ZIO.runtime[Any].flatMap { runtime =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ final class ZioLoggerRuntime(runtime: Runtime[Any]) extends LoggerRuntime {
runtime.unsafe.run {
val logLevel = ZioLoggerRuntime.logLevelMapping(level)
ZIO.logSpan(name) {
ZIO.logLevel(logLevel) {
lazy val msg = if (arguments != null) {
MessageFormatter.arrayFormat(messagePattern, arguments.toArray).getMessage
} else {
messagePattern
}
val cause = if (throwable != null) {
Cause.die(throwable)
} else {
Cause.empty
}
ZIO.logAnnotate(Slf4jBridge.loggerNameAnnotationKey, name) {
ZIO.logLevel(logLevel) {
lazy val msg = if (arguments != null) {
MessageFormatter.arrayFormat(messagePattern, arguments.toArray).getMessage
} else {
messagePattern
}
val cause = if (throwable != null) {
Cause.die(throwable)
} else {
Cause.empty
}

ZIO.logCause(msg, cause)
ZIO.logCause(msg, cause)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,42 @@ object Slf4jBridgeSpec extends ZIOSpecDefault {
LogEntry(
List("test.logger"),
LogLevel.Debug,
Map.empty,
Map(Slf4jBridge.loggerNameAnnotationKey -> "test.logger"),
"test debug message",
Cause.empty
),
LogEntry(
List("test.logger"),
LogLevel.Warning,
Map.empty,
Map(Slf4jBridge.loggerNameAnnotationKey -> "test.logger"),
"hello world",
Cause.empty
),
LogEntry(
List("test.logger"),
LogLevel.Warning,
Map.empty,
Map(Slf4jBridge.loggerNameAnnotationKey -> "test.logger"),
"3..2..1 ... go!",
Cause.empty
),
LogEntry(
List("test.logger"),
LogLevel.Warning,
Map.empty,
Map(Slf4jBridge.loggerNameAnnotationKey -> "test.logger"),
"warn cause",
Cause.die(testFailure)
),
LogEntry(
List("test.logger"),
LogLevel.Error,
Map.empty,
Map(Slf4jBridge.loggerNameAnnotationKey -> "test.logger"),
"error",
Cause.die(testFailure)
),
LogEntry(
List("test.logger"),
LogLevel.Error,
Map.empty,
Map(Slf4jBridge.loggerNameAnnotationKey -> "test.logger"),
"error",
Cause.empty
)
Expand Down

0 comments on commit 3ef441b

Please sign in to comment.