Comments (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.
I added render
on context that returns Map[String, String]
with all annotations. #64
from zio-logging.
Structured logging will be supported in ZIO Logging 2.0 and preliminary support is already complete.
from zio-logging.
Related Issues (20)
- `zio.logging.LogAnnotation` does not work when `version > 2.0.1` HOT 11
- `label` has invalid format when using `SL4J` HOT 1
- Support SLF4J 2.0.x for slf4j-bridge HOT 1
- logErrorCause does not show nested/suppressed/chained exceptions HOT 9
- Ability to filter and properly format logs received from Slf4J API HOT 1
- Feature request: support glob-like wildcard path matching for LogFilters HOT 1
- SLFJ4 binding can drop log records about application startup failure HOT 1
- ZIO logging 2.1.8 causes SLF4j log replay HOT 4
- slf4j v2 logging backend
- improve support for structured logging (Json and possibly other formats) HOT 5
- Official documentation not updated with latest changes HOT 3
- naming clash
- zio-logging 2.1.11 does not work with zio 2.0.11 HOT 3
- Missing structured annotations with consoleJsonLogger HOT 2
- multiple spans and attributes are concatenated without whitespace HOT 1
- Empty spans rendered as invalid json HOT 2
- class TypesafeConfigProvider is not found HOT 4
- Contributor's Guide page not found HOT 2
- slf4j bridge modules "enable" all log levels, causing performance issues HOT 1
- Custom Logger not adding annotations HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zio-logging.