1
    <?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 位置创建。

4

2 回答 2

2

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

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

# 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

如果您仍然面临麻烦,请告诉我

于 2018-08-31T12:48:15.910 回答
0

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

于 2019-01-06T11:41:53.073 回答