log4j2单独的配置与使用&log4j2+slf4j的结合的配置与使用

log4j2单独的配置与使用&log4j2+slf4j的结合的配置与使用

一 . log4j2单独的配置与使用

  • 所用jar文件
log4j-api-2.8.2.jar
log4j-core-2.8.2.jar
  • 配置文件 log4j2.xml

<configuration status="error">
    
    <appenders>
       
        <Console name="Console" target="SYSTEM_OUT">
            
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        Console>
       
        <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        File>
       
        <RollingFile name="RollingFile" fileName="/home/webservice/ftp_home/Ganso_mst_Shop_IF.log"
                     filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <SizeBasedTriggeringPolicy size="50MB"/>
        RollingFile>
    appenders>
  
    <loggers>
       
        <root level="trace">
            <appender-ref ref="RollingFile"/>
            <appender-ref ref="Console"/>
        root>
    loggers>
configuration> 
  • 使用:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Test{
//定义LOG
    static Logger logger = LogManager.getLogger();
    //或者(static Logger logger = LogManager.getLogger(Test.class.getName());)
    //(static Logger logger = LogManager.getLogger(Test.class);)

 public static void main(String[] args) throws Exception {
        logger.info("{}{}",1234455,"djsfhsdjhf");
        logger.debug("this is a test"); 
    }
}




二 .log4j2+slf4j的结合的配置与使用

  • 所用jar
log4j-api-2.8.2.jar
log4j-core-2.8.2.jar
log4j-slf4j-impl-2.8.2.jar
slf4j-api-1.7.25.jar
  • 配置文件log4j2.xml

<configuration status="error">
    
    <appenders>
       
        <Console name="Console" target="SYSTEM_OUT">
            
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        Console>
       
        <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        File>
       
        <RollingFile name="RollingFile" fileName="/home/webservice/ftp_home/Ganso_mst_Shop_IF.log"
                     filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <SizeBasedTriggeringPolicy size="50MB"/>
        RollingFile>
    appenders>
  
    <loggers>
       
        <root level="trace">
            <appender-ref ref="RollingFile"/>
            <appender-ref ref="Console"/>
        root>
    loggers>
configuration> 
  • 使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test {
    private static Logger logger =        
    LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
    public static void main(String[] args) throws Exception {
        logger.info("{}以及{}","张三","李四");    
    }

}

你可能感兴趣的:(---------【Java】,◆编程语言)