Log4j2.x使用教程 - TFdream/blog GitHub Wiki

Log4j2

maven


    <!--slf4j-->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>log4j-over-slf4j</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <!--log4j2-->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <!--用于与slf4j保持桥接-->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <version>${log4j2.version}</version>
    </dependency>

lo4j2.xml

<Configuration debug="off" monitorInterval="300">
    <properties>
        <property name="LOG_DIR">/Users/apple/Ricky/logs</property>
        <property name="FILE_NAME">demo</property>
    </properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <RollingRandomAccessFile name="RollingFile"
                                 fileName="${LOG_DIR}/${FILE_NAME}.log"
                                 filePattern="${LOG_DIR}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd HH-mm}-%i.log">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="20" />
        </RollingRandomAccessFile>
    </Appenders>

    <Loggers>
        <Logger name="mylog" level="debug" additivity="false">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="INFO">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

动态调整日志级别

import com.mindflow.demo.log4j2.util.ThreadUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Ricky Fung
 * @version 1.0
 * @since 2018-06-22 18:54
 */
@Service
public class LogService {

    @PostConstruct
    public void adjustLogLevel() {
        new Thread(() -> {

            for (int i=0; i<100; i++) {
                Level level = Level.DEBUG;
                if (i%3 == 0) {
                    level = Level.DEBUG;
                } else if (i%3 == 1) {
                    level = Level.INFO;
                } else {
                    level = Level.ERROR;
                }
                System.out.println("动态调整日志级别为:"+level);
                adjustLogLevel(level);
                ThreadUtils.sleep(5);
            }
        }).start();
    }

    private void adjustLogLevel(Level newLevel) {
        Collection<Logger> notCurrentLoggerCollection = LoggerContext.getContext(false).getLoggers();
        Collection<Logger> currentLoggerCollection = org.apache.logging.log4j.core.LoggerContext.getContext().getLoggers();
        Collection<Logger> allLoggerCollection = new ArrayList<>(notCurrentLoggerCollection);
        allLoggerCollection.addAll(currentLoggerCollection);
        //逐一调整
        for (Logger logger : allLoggerCollection){
            logger.setLevel(newLevel);
        }
    }
}
⚠️ **GitHub.com Fallback** ⚠️