Springへのログイン

Dec 10 2020

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 

等...

このログ出力を取得する必要があります:

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

どうすればこれを行うことができますか?

ありがとうございました!

回答

Saptarshi Dec 10 2020 at 16:14

プロパティで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>