【问题标题】:Spring boot logbcak to file is not working on tomcatSpring boot logback to file在tomcat上不起作用
【发布时间】:2019-10-14 08:31:56
【问题描述】:

我在 tomcat 上运行 spring boot 作为战争,并登录到控制台和文件。

只要我作为 Java 应用程序运行就可以了,我可以在控制台和文件中看到日志。 但在服务器上运行时,我没有看到打印到文件的日志。

我也试过设置记录器管理器,没有用。想知道是否有人遇到过类似的问题。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <property name="LOG_FILE" 
     value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}app.log}"/>    
    <property name="LOG_FILE_MAX_SIZE" value="10MB" />
    <property name="LOG_TOTAL_SIZE_CAP" value="100MB" />
    <property name="LOG_FILE_MAX_HISTORY" value="20" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

【问题讨论】:

  • 类路径上有logback-spring.xml 吗?您是否在application.properties 中指定了logging.file=&lt;path to log file&gt;,或者您是否在logback-spring.xml 中设置了文件路径?
  • 是的,我在类路径上有 logback-spring.xml。我尝试了 application.properties 中的 looging.file 并在 logback-spring.xml 中设置文件路径。但不是运气!
  • 我假设你的 appender 有类似 &lt;appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"&gt;&lt;file&gt;/log/server.log&lt;/file&gt; 的东西,并且你的应用程序确实有权写入这个路径?
  • 是的,它确实有权写入。
  • 您能否分享更多信息,例如您的 pom、yml 和 logback.xml 或 logback-spring.xml

标签: spring-boot tomcat logback


【解决方案1】:

验证您是否具有以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

或者即使您添加了 spring-boot-starter-web 依赖项,日志记录也应该可以工作。 并在 yml 或属性文件中包含以下内容:

logging.path=logs
logging.file=${logging.path}/log.log
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n

您还可以拥有logback.xml 并使用默认的弹簧base.xml,以便所有默认的弹簧配置也适用于您的日志记录:

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
</configuration>

【讨论】:

  • 感谢您的回复。我有网络依赖项,我尝试使用日志记录属性和 logback.xml,正如你提到的;两者都没有工作。我分享了我拥有的 logback-spring.xml
【解决方案2】:

这是我拥有的 logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <property name="LOG_FILE" 
     value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}app.log}"/>    
    <property name="LOG_FILE_MAX_SIZE" value="10MB" />
    <property name="LOG_TOTAL_SIZE_CAP" value="100MB" />
    <property name="LOG_FILE_MAX_HISTORY" value="20" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

【讨论】:

    【解决方案3】:

    我可以通过将日志写入不同的文件夹来修复它看起来应用程序没有写访问路径,但是我需要对 springboot 主类进行一些更改以加载基于应用程序道具的配置文件,请在下面找到类。不确定其他人是否也必须这样做。

    无论如何我很高兴它终于可以工作了:)

    公共类应用扩展 SpringBootServletInitializer{

    public String PROFILE = null;
    private static String CONFIG_LOCATION = null;
    
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //Grab the active profile from the servlet conext
        PROFILE = servletContext.getInitParameter("spring.profiles.active");
        CONFIG_LOCATION = servletContext.getInitParameter("spring.config.path");        
        super.onStartup(servletContext);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //...and pass it to the boot application
        application.application().setAdditionalProfiles(PROFILE);
        return application.sources(Application.class).properties(getProperties());
    }
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    //For Loading config from server 
    static Properties getProperties() {
        Properties props = new Properties();
        props.put("spring.config.location", CONFIG_LOCATION);
        return props;
    }
    

    }

    web.xml

    <context-param>
     <param-name>spring.profiles.active</param-name>
     <param-value>dev</param-value>
    </context-param>
    
    <context-param>
     <param-name>spring.config.path</param-name>    
     <param-value>classpath:app.config/</param-value>
    </context-param>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 2020-10-23
      • 2016-12-11
      • 2017-08-27
      • 2016-06-12
      • 2017-09-07
      相关资源
      最近更新 更多