Java: How to log raw JSON as JSON and avoid escaping during logging with logback / slf4j

后端 未结 7 2313
攒了一身酷
攒了一身酷 2021-02-05 13:09

I\'m using SLF4J with Logback in a JAX-RS application... I want to log to JSON in such a way that my message is not encoded again but printed raw into the logfile:

At t

7条回答
  •  我寻月下人不归
    2021-02-05 13:54

    If you have a Json formatted messages, the upper solutions work, but are not so nice, since you don´t want to call a logstash specific code, each time you use your logger in the code.

    Just adding a

    net.logstash.logback.encoder.LogstashEncoder
    

    is not enough, since the message itsself stays escaped. To solve this, try the following in your logback.xml:

    
         
            
            
            
            
                
                    {
                    "jsonMessage": "#asJson{%message}"
                    }
                
            
        
    
    

    The #asJson pattern will unescape your message.

提交回复
热议问题