JAVA | logback.xml configures log output to specify a separate folder

There will be a lot of log printing in the project. If the debug mode is enabled, even if only a certain type of log is output, a lot of logs will be printed when the amount of data is very large, especially when database statements are to be printed. So how to print the debug log of the database read and write operations under a certain class into a separate log and output it to the specified folder?

configuration

lockback.xml configuration file


<!-- 配置mongodb日志输出至单独的日志文件中 -->
<appender name="mongodb_log" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!--日志文件输出的文件名-->
        <FileNamePattern>
            ${logs.dir}/app_mongodb_log.%d{yyyy-MM-dd}.%i.log
        </FileNamePattern>
        <cleanHistoryOnStart>false</cleanHistoryOnStart>
        <!-- 最多保存30天日志-->
        <MaxHistory>30</MaxHistory>
        <timeBasedFileNamingAndTriggeringPolicy  class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">      
        	<!--日志文件最大的大小-->
            <maxFileSize>30MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
    <encoder>
        <charset>UTF-8</charset>
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n
        </Pattern>
    </encoder>
</appender>

<!-- 日志输出级别 -->
<root>
    <level value="WARN" />
    <appender-ref ref="stdout" />
    <appender-ref ref="file" />
    <!--将新增的配置添加进root中统一设置输出级别 -->
    <appender-ref ref="mongodb_log" />
</root>

<!--打印 MongoTemplate 类下的debug日志,并输出到mongodb_log的配置下-->
<logger name="org.springframework.data.mongodb.core.MongoTemplate" level="DEBUG" additivity="false">
    <appender-ref ref= "mongodb_log"/>
</logger>

illustrate:

  • additivityIf set to false, the child Logger will only output in its own appender, but not in the appender of the parent Logger. The default is true.

Guess you like

Origin blog.csdn.net/u012294515/article/details/108418761