首页 > 解决方案 > 春季登录

问题描述

我有一个 spring boot 应用程序,它启动后我们通常会看到以下输出到控制台

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.0)
1283 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path 

ETC...

我需要得到这个日志输出:

1283 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path 

并使用 slf4j 将其放入文件中

我有一个带有 slf4j 设置的 application.properties

logging.level.root=INFO
logging.level.org.spring.upskill = INFO
logging.level.org.springframework.web = INFO
logging.file.name = app.log

但我没有得到文件的完整输出。只回复我的 GetMappings

例如

[2020-12-10 14:31:53.381] - 19960 INFO [main] --- org.apache.catalina.core.StandardService: Starting service [Tomcat]
[2020-12-10 14:31:53.385] - 19960 INFO [main] --- org.apache.catalina.core.StandardEngine: Starting Servlet engine: [Apache Tomcat/9.0.39]
[2020-12-10 14:31:53.432] - 19960 INFO [main] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]: Initializing Spring embedded WebApplicationContext

我该怎么做?

谢谢!

标签: javaspringspring-bootloggingslf4j

解决方案


我想你可以在属性中使用 appender.rolling.policies.startup.type = OnStartupTriggeringPolicy 或更新 log4j2.xml 以在 Appenders-->RollingRandomAccessFile 中包含以下内容

    <Policies>
        <!-- Starts a new log on tomcat start -->
        <OnStartupTriggeringPolicy />
        <!-- Starts a new file when size reaches threshold -->
        <SizeBasedTriggeringPolicy size="100 MB" />
        <!-- causes a rollover once the date/time pattern no longer applies to 
            the active file
        <TimeBasedTriggeringPolicy /> -->
    </Policies>

推荐阅读