问题
I am trying to apply logging in external file in my spring boot project. Is it possible. I have followed many websites. Please help.
回答1:
You have to define a logback.xml file at the root of the classpath (typically in src/main/resources).
Example :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework.web" level="DEBUG" />
<logger name="your.custom.package" level="TRACE" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
This configuration creates a logs.log file (at the root of your Maven project).
Check the Spring and Logback documentations to get more details.
回答2:
Just add this line to your application.yaml (or equivalent in application.properties)
logging:
file: directorname/nameoflogfile.log
Spring Boot will handle the rest. I don't think you explicitly need to add in a logback.xml as there's one shipped in the classpath anyway.
来源:https://stackoverflow.com/questions/45051135/how-to-apply-logging-in-spring-boot-in-external-log-file