Logback未在SpringBoot中创建日志文件

时间:2018-08-31 11:11:09

标签: spring-boot logging logback spring-logback

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>true</withJansi>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger - %msg%n</Pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/logs/tomcat/LogBackDemo/logback/app_LogBackDemo.log</file>

        <withJansi>true</withJansi>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %logger - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>/logs/tomcat/LogBackDemo/logback/app_LogBackDemo.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>10</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>

        </rollingPolicy>

    </appender>

    <logger name="com.logBack.demo" level="debug"
            additivity="false">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="error" additivity="false">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

这是我的logback-spring.xml文件,但是当我运行应用程序时,日志在控制台中正确显示,但未在指定位置创建日志文件。我正在使用Windows系统,因此理想情况下,日志文件应在c:/logs/tomcat/LogBackDemo/logback/app_LogBackDemo.log位置中创建。

2 个答案:

答案 0 :(得分:2)

我使用了您的确切文件(已删除<withJansi>true</withJansi>),并且当日志配置文件保存在资源文件夹(src \ main \ resources \ logback-spring.xml)中时,它正在创建日志文件。我已经修改了 按照我的软件包名称com.logBack.demo(记录器名称),然后打印日志。

您可以使用以下属性并轻松设置日志记录,包括Spring Boot中带有application.property文件的日志文件。

# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.

如果您想使用特殊的xml文件(我不喜欢),则可以进行检查, https://www.baeldung.com/spring-boot-logging

如果您仍然遇到麻烦,请告诉我

答案 1 :(得分:0)

我有同样的问题。我只是从文件系统中删除logback.xml并再次创建它就可以了