Logging in Spring

Viewed 141

I have an spring boot application, and after it starts we usually see the following output to the console

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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...

I need to get this log-output :

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

and put it to the file using slf4j

I have an application.properties with the settings of slf4j

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

But I don`t get a full output to the file. Only responses to my GetMappings

For example

[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

How do I get this done?

Thank you!

1 Answers

I presume u can use appender.rolling.policies.startup.type = OnStartupTriggeringPolicy in properties or update the log4j2.xml to include the below inside 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>
Related