how to apply logging in spring boot in external log file? [closed]

为君一笑 提交于 2019-12-25 09:44:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!