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

后端 未结 7 2288
攒了一身酷
攒了一身酷 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:57

    Just came over this myself and found an article with a few recommandations on logging.

    If you use maven put this dependency into pom.xml

    
        net.logstash.logback
        logstash-logback-encoder
        3.4
    
    

    And put something like this into logback.xml

    
        
        
            logs/${PROJECT_ID}.json
            
            
                1
                logs/${PROJECT_ID}.json.%i
            
            
                1MB
            
        
        
            
        
        
            
        
    
    

    This creates example.json file under logs/. The file is rolled once when it reaches 1MB in size.

    LOGGER.debug(append("object", someObject), "log message");
    

提交回复
热议问题