SpringBoot集成Log4j2

在 Spring Boot 中集成 Log4j 2.x 的步骤如下:

    • 1.在 pom.xml 中添加 Log4j 2.x 的依赖:
    • 2.在 resources 中配置 Log4j2.xml:
    • 3.在代码中使用 Log4j 2.x:
    • 4.启动应用程序,查看控制台和日志文件中的日志输出。

1.在 pom.xml 中添加 Log4j 2.x 的依赖:

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-slf4j-implartifactId>
    <version>2.17.1version>
dependency>

2.在 resources 中配置 Log4j2.xml:





<Configuration status="WARN" monitorInterval="60">
    
    <Properties>
        
        <Property name="log_base_dir">/app_data/logs/my_appProperty>
        
        <Property name="log_pattern">[%d{yyyy-MM-dd HH:mm:ss.SSS}][%-5p][%T][%c.%M:%L] %msg%xEx%nProperty>
        
        <Property name="max_single_file_size">1MBProperty>
    Properties>
 
    
    <Appenders>
        
        <Console name="Console" target="SYSTEM_OUT">
            
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="${log_pattern}"/>
        Console>
 
        
        
        <RollingFile name="DebugLogRollingFile" fileName="${log_base_dir}/my_app_debug.log"
                     filePattern="${log_base_dir}/$${date:yyyy_MM_dd}/my_app_debug_%d{yyyy_MM_dd_HH}_%i.log.gz">
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${log_pattern}" charset="UTF-8"/>
            
            <Policies>
                
                <TimeBasedTriggeringPolicy interval="1" modulate="false"/>
                
                <SizeBasedTriggeringPolicy size="${max_single_file_size}"/>
            Policies>

你可能感兴趣的:(SpringBoot,log4j2,spring,boot,java,log4j)