简体   繁体   中英

Spring Boot app having trouble with logback.groovy config file

I am trying to get Spring Boot working with Logback and am running into an error/issue that I can't figure out.

To reproduce this issue in its entirety, I created a Spring Boot Example repo on GitHub, but essentially, this is my application.yml :

logging:
  config: 'logback.groovy'
server:
  port: 9200
  error:
    whitelabel:
      enabled: false

And my logback.groovy :

statusListener(OnConsoleStatusListener)

def LOG_PATH = '/opt/springbootexample/logs/springbootexample'

appender('CONSOLE', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = '%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n'
    }
}

appender('FILE', FileAppender) {
    file = "${LOG_PATH}.log"
    encoder(PatternLayoutEncoder) {
        pattern = '%msg%n'
        outputPatternAsHeader = true
    }
}

appender('ROLLING', RollingFileAppender) {
    encoder(PatternLayoutEncoder) {
        Pattern = '%d %level %thread %mdc %logger - %m%n'
    }
    rollingPolicy(TimeBasedRollingPolicy) {
        fileNamePattern = "${LOG_PATH}-%d{yyyy-MM}.zip"
        maxHistory = 30
        totalSizeCap = '1KB'
    }
}

root(INFO, ["CONSOLE", "ROLLING"])

I've made sure that /opt/springbootexample/logs exists I ran chmod -R 777 /opt/springbootexample so my Spring Boot app should have no problem creating log files there and writing to them.

When I run the app locally, I get no errors/exceptions/warnings; everything looks perfectly fine in the console output. I then fire up a browser and point it to http://localhost:9200 , which should return a simple dummy message, however nothing happens. Even worse, nothing in the console happens either.

The only clue is that after I've shut down the app, if I go to /opt/springbootexample/logs/springbootexample.log , its contents are:

#logback.classic pattern: %msg%n

Which tells me that perhaps there's something misconfigured in my logback.groovy 's FileAppender maybe? Any ideas/thoughts?

Your application works (web page loads correctly and shows "Greetings from Spring Boot!"), your logging configuration is just a little wonky.

First problem is that you forgot to add the non-rolling file appender to your root logger. Just add "FILE" to the list at the bottom of logback.groovy like so:

root(INFO, ["FILE", "CONSOLE", "ROLLING"])
//          ^^^^^^^

And the log works. Without this, the appender is initialized, the file created with the "weird header" (because you chose to use outputPatternAsHeader = true ), and nothing is ever appended, because you chose not to.

So the only weird issue is why the web page does not load for you... But perhaps you'll get more info if the log works :)

Try something much simpler for LOG_PATH , such as def LOG_PATH = '/tmp/foo' , because if that is wrong the server will shut down, and obviously the page will fail to load.

If the path points to something that exists and is writable, it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM