loki4j / loki-logback-appender

Fast and lightweight implementation of Logback appender for Grafana Loki
https://loki4j.github.io/loki-logback-appender/
BSD 2-Clause "Simplified" License
300 stars 26 forks source link

Add a support for JSON message layout #205

Closed nehaev closed 8 months ago

gerhardj-b commented 8 months ago

Currently, I keep getting malformed JSON when using the configuration as in https://github.com/loki4j/loki-logback-appender/issues/201#issuecomment-1862159478_

Do I suspect correctly, that this problem would be solved by this issue? Or is there any configuration right now, that I can use to fix this problem?

Below is an example of such a malformed message, that cannot be parsed with Grafana:

{
                        "level":"ERROR",
                        "class":"o.a.c.c.C.[.[.[.[dispatcherServlet]",
                        "thread":"http-nio-8080-exec-1",
                        "message": "Servlet.service() for servlet [dispatcherServlet] in context with path [/my/request] threw exception [Request processing failed: org.springframework.web.client.HttpClientErrorException$NotFound: 404 404: "{<EOL>  "timestamp" : "2024-01-18T10:48:56.212+00:00",<EOL>  "status" : 404,<EOL>  "error" : "Not Found",<EOL>  "message" : "No message available",<EOL>  "path" : "/microservice2/request"<EOL>}"] with root cause",
                        "exception":"org.springframework.web.client.HttpClientErrorException$NotFound: 404 404: "{<EOL>  "timestamp" : "2024-01-18T10:48:56.212+00:00",<EOL>  "status" : 404,<EOL>  "error" : "Not Found",<EOL>  "message" : "No message available",<EOL>  "path" : "/microservice2/request"<EOL>}"
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:112)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137)
...
nehaev commented 8 months ago

Hi @gerhardj-b, yes, this ticket is about adding a support for a json layout. Currently only a plain text layout is supported, so no surprise it doesn't work for json.

gerhardj-b commented 8 months ago

Alright, thanks for the confirmation.

To anyone struggling with the same issue, I found a workaround for now that works for me:

<pattern>
    {
    "level":"%level",
    "class":"%logger{36}",
    "thread":"%thread",
    "message": "%replace(%message){'"','\\"'}",
    "exception":"%replace(%replace(%ex){'"','\\"'}){'\n','\\n'}"
    }
</pattern>