JavaDemo——slf4j搭载log4j2

maven导入:


	    org.apache.logging.log4j
	    log4j-slf4j-impl
	    2.13.3
	
	
	    org.apache.logging.log4j
	    log4j-api
	    2.13.3
	
	
	    org.apache.logging.log4j
	    log4j-core
	    2.13.3
	

默认名称叫log4j2.xml的配置文件:





	
		
		utf8
	
	
		
   		
     		
   		
   		
	   	
	     	
	   	
	   	
		
			
	     	
	     	
	     	
	     		
            	
            	
            
	     	
	     	 
	     	
	     	
	     		
	     		
	     		
	     		
	     	
	     	
	     	
		
		
		
            
            
            	
            
            
            	
            	
            	
            
            
            	
            	
            		
            		
            		
            		
            		
            			
            			
            			
            		
            	
            
        
	
	
		
		
			
		
		
			
		 	
		 	
		
	

测试Demo:

MainTestlog2.java文件:

/**
 * 2020年8月20日下午3:36:04
 */
package testlog4j2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import testlogger.TestLoggerConfig;

/**
 * @author XWF
 *
 */
public class MainTestlog2 {

	private static final Logger logger = LoggerFactory.getLogger("MyLogName");
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Thread(()->{
			MainTestlog2.showlog();
		}).start();
		TestLoggerConfig.showfun("ABC");
	}

	public static void showlog() {
		for(int i=0; i<100; i++) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			logger.info("print info:" + i);
			logger.warn("print warn:" + i);
			logger.error("print error" + i);
		}
	}
}

另一个包下的TestLoggerConfig.java文件:

/**
 * 2020年8月20日下午4:02:28
 */
package testlogger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author XWF
 *
 */
public class TestLoggerConfig {
	
	private static final Logger logger = LoggerFactory.getLogger(TestLoggerConfig.class);
	
	public static int showfun(String str) {
		for(int i=0; i<100; i++) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			logger.info("+++++" + str + "info:" + i);
			logger.warn("+++++" + str + "warn:" + i);
			logger.error("+++++" + str + "error" + i);
		}
		return 1;
	}
	
}

为了将log4j2.xml文件放到jar包外面方便修改调整,增加一个log4j2.component.properties文件到resource里并打包到jar里用来指定xml文件位置;

配置文件log4j2.component.properties:

log4j2.loggerContextFactory=org.apache.logging.log4j.core.impl.Log4jContextFactory
log4j.configurationFile=./log4j2.xml

eclipse里的目录结构:

JavaDemo——slf4j搭载log4j2_第1张图片

Demo运行结果:

控制台:

JavaDemo——slf4j搭载log4j2_第2张图片

file.log

JavaDemo——slf4j搭载log4j2_第3张图片

rolling.log

JavaDemo——slf4j搭载log4j2_第4张图片

rollingrandom.log

 

 

参考:

https://www.cnblogs.com/new-life/p/9246143.html

https://blog.csdn.net/luoxiang183/article/details/79281886?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.channel_param

https://blog.csdn.net/saya0/article/details/107334015/

https://blog.csdn.net/qq_24879495/article/details/78061388

https://blog.csdn.net/zhang168/article/details/46814489

https://www.cnblogs.com/frankwin608/p/log4j2.html

https://www.cnblogs.com/jessezeng/p/5144317.html

官网https://logging.apache.org/log4j/2.x/manual/appenders.html#DefaultRolloverStrategy

 

你可能感兴趣的:(JavaDemos,log4j2)