简体   繁体   English

Spring Boot应用程序无法使用logback.groovy配置文件

[英]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. 我正在尝试让Spring Boot与Logback一起使用,并且遇到了我无法解决的错误/问题。

To reproduce this issue in its entirety, I created a Spring Boot Example repo on GitHub, but essentially, this is my application.yml : 为了完整地重现此问题,我在GitHub上创建了一个Spring Boot Example存储库 ,但从本质上讲,这是我的application.yml

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

And my logback.groovy : 和我的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. 我确保/opt/springbootexample/logs存在,我运行了chmod -R 777 /opt/springbootexample所以我的Spring Boot应用程序应该在那里创建日志文件并将其写入就不会有问题。

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. 然后,我启动浏览器并将其指向http://localhost:9200 ,该浏览器应返回一条简单的虚拟消息,但是什么也没有发生。 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: 唯一的提示是,在关闭应用程序后,如果转到/opt/springbootexample/logs/springbootexample.log ,其内容为:

#logback.classic pattern: %msg%n

Which tells me that perhaps there's something misconfigured in my logback.groovy 's FileAppender maybe? 这告诉我也许logback.groovyFileAppender可能配置错误? Any ideas/thoughts? 有什么想法/想法吗?

Your application works (web page loads correctly and shows "Greetings from Spring Boot!"), your logging configuration is just a little wonky. 您的应用程序正常工作(网页已正确加载并显示“ Spring Boot的问候!”),您的日志记录配置有些奇怪。

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: 只需将"FILE"添加到logback.groovy底部的列表中logback.groovy如下所示:

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. 否则,将初始化附加程序,并使用“怪异的标头”创建文件(因为您选择使用outputPatternAsHeader = true ),并且由于未选择不附加任何内容。

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. LOG_PATH尝试更简单的LOG_PATH ,例如def LOG_PATH = '/tmp/foo' ,因为如果这是错误的,服务器将关闭,显然该页面将无法加载。

If the path points to something that exists and is writable, it will work. 如果路径指向存在且可写的内容,则它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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