Skip to content

Commit

Permalink
slf4j-bridge - trace and annotations for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Oct 21, 2024
1 parent 3061afb commit 9047fc7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 22 deletions.
12 changes: 6 additions & 6 deletions slf4j-bridge/src/main/java/org/slf4j/helpers/ZioLoggerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ZioLoggerBase(String name) {

abstract protected void log(Level level, Marker marker, String messagePattern, Object[] arguments, Throwable throwable);

abstract protected boolean isEnabled(String name, Level level);
abstract protected boolean isEnabled(Level level);

private void logWithThrowable(Level level, Marker marker, String msg, Throwable t) {
log(level, marker, msg, null, t);
Expand Down Expand Up @@ -56,27 +56,27 @@ private void logWithArgs(Level level, Marker marker, String msg, Object[] args)

@Override
public boolean isTraceEnabled() {
return isEnabled(name, Level.TRACE);
return isEnabled(Level.TRACE);
}

@Override
public boolean isDebugEnabled() {
return isEnabled(name, Level.DEBUG);
return isEnabled(Level.DEBUG);
}

@Override
public boolean isErrorEnabled() {
return isEnabled(name, Level.ERROR);
return isEnabled(Level.ERROR);
}

@Override
public boolean isWarnEnabled() {
return isEnabled(name, Level.WARN);
return isEnabled(Level.WARN);
}

@Override
public boolean isInfoEnabled() {
return isEnabled(name, Level.INFO);
return isEnabled(Level.INFO);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ package org.slf4j.impl

import org.slf4j.Marker
import org.slf4j.event.Level
import zio.logging.slf4j.bridge.LoggerData

trait LoggerRuntime {

def log(
name: String,
logger: LoggerData,
level: Level,
marker: Marker,
messagePattern: String,
arguments: Array[AnyRef],
throwable: Throwable
): Unit

def isEnabled(name: String, level: Level): Boolean
def isEnabled(logger: LoggerData, level: Level): Boolean
}
7 changes: 5 additions & 2 deletions slf4j-bridge/src/main/scala/org/slf4j/impl/ZioLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ package org.slf4j.impl
import org.slf4j.Marker
import org.slf4j.event.Level
import org.slf4j.helpers.ZioLoggerBase
import zio.logging.slf4j.bridge.LoggerData

final class ZioLogger(name: String, factory: ZioLoggerFactory) extends ZioLoggerBase(name) {
private val data: LoggerData = LoggerData(name)

override protected def log(
level: Level,
marker: Marker,
messagePattern: String,
arguments: Array[AnyRef],
throwable: Throwable
): Unit =
factory.log(name, level, marker, messagePattern, arguments, throwable)
factory.log(data, level, marker, messagePattern, arguments, throwable)

override protected def isEnabled(name: String, level: Level): Boolean = factory.isEnabled(name, level)
override protected def isEnabled(level: Level): Boolean = factory.isEnabled(data, level)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.slf4j.impl

import org.slf4j.event.Level
import org.slf4j.{ ILoggerFactory, Logger, Marker }
import zio.logging.slf4j.bridge.LoggerData

import java.util.concurrent.ConcurrentHashMap
import scala.jdk.CollectionConverters._
Expand All @@ -29,17 +30,17 @@ class ZioLoggerFactory extends ILoggerFactory {
this.runtime = runtime

private[impl] def log(
name: String,
logger: LoggerData,
level: Level,
marker: Marker,
messagePattern: String,
arguments: Array[AnyRef],
throwable: Throwable
): Unit =
if (runtime != null) runtime.log(name, level, marker, messagePattern, arguments, throwable)
if (runtime != null) runtime.log(logger, level, marker, messagePattern, arguments, throwable)

private[impl] def isEnabled(name: String, level: Level): Boolean =
if (runtime != null) runtime.isEnabled(name, level) else false
private[impl] def isEnabled(logger: LoggerData, level: Level): Boolean =
if (runtime != null) runtime.isEnabled(logger, level) else false

override def getLogger(name: String): Logger =
loggers.getOrElseUpdate(name, new ZioLogger(name, this))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2019-2024 John A. De Goes and the ZIO Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package zio.logging.slf4j.bridge

import zio.Trace

final case class LoggerData(name: String) {

lazy val trace: Trace = Trace(name, "", 0)

lazy val annotations: Map[String, String] = Map(zio.logging.loggerNameAnnotationKey -> name)

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import org.slf4j.event.Level
import org.slf4j.helpers.MessageFormatter
import org.slf4j.impl.LoggerRuntime
import zio.logging.LogFilter
import zio.{ Cause, Fiber, FiberId, FiberRef, FiberRefs, LogLevel, Runtime, Trace, Unsafe }
import zio.{Cause, Fiber, FiberId, FiberRef, FiberRefs, LogLevel, Runtime, Unsafe}

final class ZioLoggerRuntime(runtime: Runtime[Any], filter: LogFilter[Any]) extends LoggerRuntime {

override def log(
name: String,
logger: LoggerData,
level: Level,
marker: Marker,
messagePattern: String,
Expand All @@ -34,7 +34,7 @@ final class ZioLoggerRuntime(runtime: Runtime[Any], filter: LogFilter[Any]) exte
): Unit =
Unsafe.unsafe { implicit u =>
val logLevel = ZioLoggerRuntime.logLevelMapping(level)
val trace = Trace.empty
val trace = logger.trace
val fiberId = FiberId.Gen.Live.make(trace)
val currentFiber = Fiber._currentFiber.get()

Expand All @@ -44,8 +44,8 @@ final class ZioLoggerRuntime(runtime: Runtime[Any], filter: LogFilter[Any]) exte
runtime.fiberRefs.joinAs(fiberId)(currentFiber.unsafe.getFiberRefs())
}

val logSpan = zio.LogSpan(name, java.lang.System.currentTimeMillis())
val loggerName = (zio.logging.loggerNameAnnotationKey -> name)
val logSpan = zio.LogSpan(logger.name, java.lang.System.currentTimeMillis())
val loggerName = (zio.logging.loggerNameAnnotationKey -> logger.name)

val fiberRefs = currentFiberRefs
.updatedAs(fiberId)(FiberRef.currentLogSpan, logSpan :: currentFiberRefs.getOrDefault(FiberRef.currentLogSpan))
Expand All @@ -71,18 +71,18 @@ final class ZioLoggerRuntime(runtime: Runtime[Any], filter: LogFilter[Any]) exte
fiberRuntime.log(() => msg, cause, Some(logLevel), trace)
}

override def isEnabled(name: String, level: Level): Boolean = {
override def isEnabled(logger: LoggerData, level: Level): Boolean = {
val logLevel = ZioLoggerRuntime.logLevelMapping(level)

filter(
Trace(name, "", 0),
logger.trace,
FiberId.None,
logLevel,
() => "",
Cause.empty,
FiberRefs.empty,
List.empty,
Map(zio.logging.loggerNameAnnotationKey -> name)
logger.annotations
)
}

Expand Down

0 comments on commit 9047fc7

Please sign in to comment.