Giter VIP home page Giter VIP logo

Comments (3)

miguel-vila avatar miguel-vila commented on June 8, 2024 3

+1

This would be helpful for us as well.

As a work around I created a custom slf4j Logging constructor that uses structured logging. It creates a logstash Marker from a zio LogContext. It's far from ideal because annotations need to be known beforehand, as @frekw noted:

object Slf4jWithMarkedAnnotationsLogger {

  private val tracing = Tracing(
    Tracer.globallyCached(new AkkaLineNumbersTracer),
    TracingConfig.enabled
  )

  private def classNameForLambda(lambda: => AnyRef) =
    tracing.tracer.traceLocation(() => lambda) match {
      case SourceLocation(_, clazz, _, _) => Some(clazz)
      case NoLocation(_)                  => None
    }

  private def logger(name: String) =
    ZIO.effectTotal(
      LoggerFactory.getLogger(
        name
      )
    )

  def make(logAnnotations: LogAnnotation[_]*): UIO[Logging] =
    Logging.make { (context, line) =>
      val loggerName = context.get(LogAnnotation.Name) match {
        case Nil   => classNameForLambda(line).getOrElse("ZIO.defaultLogger")
        case names => LogAnnotation.Name.render(names)
      }
      def logContextMarker = logContext2Marker(context, logAnnotations)
      logger(loggerName).map(slf4jLogger =>
        context.get(LogAnnotation.Level).level match {
          case LogLevel.Off.level => ()
          case LogLevel.Debug.level =>
            slf4jLogger.debug(logContextMarker, line)
          case LogLevel.Trace.level =>
            slf4jLogger.trace(logContextMarker, line)
          case LogLevel.Info.level =>
            slf4jLogger.info(logContextMarker, line)
          case LogLevel.Warn.level =>
            slf4jLogger.warn(logContextMarker, line)
          case LogLevel.Error.level =>
            slf4jLogger.error(logContextMarker, line)
          case LogLevel.Fatal.level =>
            slf4jLogger.error(logContextMarker, line)
      })
    }

  private def logContext2Marker(
      logContext: LogContext,
      logAnnotations: Seq[LogAnnotation[_]]): Marker = {
    import net.logstash.logback.marker.Markers._
    import scala.collection.JavaConverters._

    def fromLogAnnotation[A](logAnnotation: LogAnnotation[A]): Marker =
      append(
        logAnnotation.name,
        logAnnotation.render(logContext.get(logAnnotation))
      )

    aggregate(logAnnotations.map(l => fromLogAnnotation(l)).asJava)
  }

}

I'm not sure what design changes should be made to support structured logging more generally but it would be really useful!

from zio-logging.

pshemass avatar pshemass commented on June 8, 2024 3

I added render on context that returns Map[String, String] with all annotations. #64

from zio-logging.

jdegoes avatar jdegoes commented on June 8, 2024

Structured logging will be supported in ZIO Logging 2.0 and preliminary support is already complete.

from zio-logging.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.