When we use external tomcat to deploy our Spring MVC application, there are certain log files which get printed in the logs folder, i.e., host-manager.log, localhost.log, manager.log, etc. I needed to enable the generation of these logs in my Spring Boot Application too. How can we do that?
Asked
Active
Viewed 1,956 times
0
-
This should help: https://stackoverflow.com/questions/48312851/spring-boot-embedded-tomcat-logs – Debanjan Jun 05 '22 at 08:40
-
@Debanjan Thanks for your reply. I have added the following property in the application, but I think all the logs are getting printed on console only. I needed to print them in different files. logging.level.org.apache=TRACE – acsh Jun 05 '22 at 09:18
1 Answers
1
Please check spring docs for logging in file-output: https://docs.spring.io/spring-boot/docs/2.7.0/reference/htmlsingle/#features.logging.file-output
Or alternatively you can use logback-spring.xml
to achieve this.
Sample logback-spring.xml
which prints logs in the console and file and it daily rollovers the file or if it reaches 10MB:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs"/> <!-- the folder where you want your log files to be -->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/spring-boot-logger.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10MBs -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at INFO level -->
<root level="INFO">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</root>
More about how to config logback: https://logback.qos.ch/documentation.html

xerZV
- 19
- 3