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

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

    here is an updated (2016) groovy logback config that dumps out your logs in json format to a file, and debug lines in the console. Took me all day to figure out so i thought i'd update the thread.

        import ch.qos.logback.classic.encoder.PatternLayoutEncoder
    import ch.qos.logback.core.ConsoleAppender
    import ch.qos.logback.core.rolling.FixedWindowRollingPolicy
    import ch.qos.logback.core.rolling.RollingFileAppender
    import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy
    import net.logstash.logback.encoder.LogstashEncoder
    
    import static ch.qos.logback.classic.Level.INFO
    import static ch.qos.logback.classic.Level.WARN
    
    def PROJECT_ID = "com.foo"
    
        appender("file", RollingFileAppender) {
            file = "/tmp/logs/${PROJECT_ID}.json"
            encoder(LogstashEncoder)
            rollingPolicy(FixedWindowRollingPolicy) {
                maxIndex = 1
                fileNamePattern = "logs/${PROJECT_ID}.json.%i"
            }
            triggeringPolicy(SizeBasedTriggeringPolicy) {
                maxFileSize = "1MB"
            }
        }
    
    
        appender("STDOUT", ConsoleAppender) {
            encoder(PatternLayoutEncoder) {
                pattern = "%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"
            }
        }
    
        logger("com.foo", INFO, ["STDOUT", "file"], false)
    
        root(WARN, ["STDOUT", "file"])
    

提交回复
热议问题