Skip to content

Repository files navigation

Flog

Functional logging with metadata

GitLab CIMaven CentralLicense

libraryDependencies ++="io.taig"%%"flog-core"%"x.x.x"::"io.taig"%%"flog-slf4j"%"x.x.x"::Nil

Usage

importcats.effect.*importcats.effect.std.Dispatcherimportio.taig.flog.data.Levelimportio.taig.flog.http4s.{CorrelationMiddleware, LoggingMiddleware}
importio.taig.flog.slf4j.FlogSlf4jBinderimportorg.http4s.ember.server.EmberServerBuilderimportorg.http4s.server.Serverimportorg.http4s.{HttpApp, HttpRoutes, Response}
importorg.http4s.dsl.io.*importcom.comcast.ip4s.*objectSampleAppextendsResourceApp.Forever:defapp(logger: Logger[IO]):HttpApp[IO] =HttpRoutes.of[IO] {
caseGET->Root/"crash"=>IO.raiseError(newRuntimeException("💣"))
caseGET->Root=>
logger.info("I'm handling a request here, and a trace information is automagically attached to my payload!") *>Ok()
}.orNotFound
defserver(logger: ContextualLogger[IO]):Resource[IO, Server] =EmberServerBuilder
.default[IO]
.withHost(host"0.0.0.0")
.withPort(port"8080")
.withHttpApp(CorrelationMiddleware(logger)(LoggingMiddleware(logger)(app(logger))))
.build
vallogger:Resource[IO, Logger[IO]] =Dispatcher
.parallel[IO]
.flatMap: dispatcher =>Resource
.eval(Logger.stdOut[IO])
.flatMap(Logger.queued[IO])
.map(_.minimum(Level.Info))
.evalTap(FlogSlf4jBinder.initialize(_, dispatcher))
overridedefrun(arguments: List[String]):Resource[IO, Unit] =for
logger <- logger
contextual <-Resource.eval(ContextualLogger.ofIO(logger))
_ <- server(contextual)
yield ()
[2023-06-17 10:08:50.582][info][server] Request
{
"request" : {
"method" : "GET",
"uri" : "/",
"headers" : {
"Host" : "localhost:8080",
"User-Agent" : "curl/7.88.1",
"Accept" : "*/*"
}
},
"correlation" : "351f211a-c857-4b98-a1dd-b16240fa7fa1"
}
[2023-06-17 10:08:50.597][info][/] I'm handling a request here, and a trace information is automagically attached to my payload!
{
"correlation" : "351f211a-c857-4b98-a1dd-b16240fa7fa1"
}
[2023-06-17 10:08:50.598][info][server] Response
{
"response" : {
"status" : "200 OK",
"headers" : {
"Content-Length" : "0"
}
},
"correlation" : "351f211a-c857-4b98-a1dd-b16240fa7fa1"
}
[2023-06-17 10:08:54.808][info][server] Request
{
"request" : {
"method" : "GET",
"uri" : "/crash",
"headers" : {
"Host" : "localhost:8080",
"User-Agent" : "curl/7.88.1",
"Accept" : "*/*"
}
},
"correlation" : "47b1dca1-50be-4c53-a621-ad16d72b1b35"
}
[2023-06-17 10:08:54.813][error][server] Request failed
{
"correlation" : "47b1dca1-50be-4c53-a621-ad16d72b1b35"
}
java.lang.RuntimeException: 💣
at io.taig.flog.SampleApp$$anon$1.applyOrElse(SampleApp.scala:16)
[...]

Slf4j

Flog comes with its own slf4j backend in the flog-slf4j module. In order to enable it call FlogSlf4jBinder.initialize as early in your application initialization as possible.

About

Functional logging with metadata

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages