如何在Sping Boot 中启用Tomcat日志

cnwbcb6i  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(232)

当我们使用外部tomcat来部署我们的Spring MVC应用程序时,有一些日志文件会在logs文件夹中打印出来,例如host-manager.log、localhost.log、manager.log等。我也需要在我的Sping Boot 应用程序中启用这些日志的生成。我们如何才能做到这一点?

rsaldnfx

rsaldnfx1#

请检查spring docs以记录文件输出:https://docs.spring.io/spring-boot/docs/2.7.0/reference/htmlsingle/#features.logging.file-output
或者,您也可以使用logback-spring.xml来实现这一点。
示例logback-spring.xml在控制台和文件中打印日志,并且它每天滚动文件,或者如果它达到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>

有关如何配置回登录的详细信息:https://logback.qos.ch/documentation.html

相关问题