log4j2 configuration

//lib
log4j-core-xx.jar    log4j-api-xx.jar


package com.ztao2333.spring;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class HelloTest {
	
	@Test  
	public void testHelloWorld () {  
		Logger logger = LogManager.getLogger(HelloTest.class.getName());
		logger.trace("trace level");  
		logger.debug("debug level");  
		logger.info("info level");  
		logger.warn("warn level");  
		logger.error("error level");  
		logger.fatal("fatal level");
		//1. Read the configuration file and instantiate an IoC container   
		//Use the classpath path The default classpath is the src folder
		ApplicationContext context = new ClassPathXmlApplicationContext("helloWorld.xml");
		
		//Use the path of the file system, the default refers to the root path of the project
		//ApplicationContext context = new FileSystemXmlApplicationContext("src/helloWorld.xml");
		
		//2. Get the Bean from the container, note that this is completely "interface-oriented programming, not implementation-oriented"  
		HelloAPI helloApi = context.getBean("hello", HelloAPI.class);  
		//3. Execute business logic  
		helloApi.sayHello();
	}
}

//Create a new log4j2.xml under src (the path under src is the classpath path)
<?xml version="1.0" encoding="utf-8"?>
<configuration status="OFF">    
	<appenders>
	<!-- Use ThresholdFilter to set the log level separately for the name Console log RollingFile-->     
 
		<Console name="Console" target="SYSTEM_OUT">
			<!--The console only outputs information of level and above (onMatch), other directly rejected (onMismatch)-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>       
        	<PatternLayout pattern="%5p:%d{yyyy-MM-dd HH:mm:ss.SSS}[%M] %m %n"/>        
		</Console>  
     
     	<!--The file will print out all the information, the log will be automatically cleared every time the program is run, determined by the append attribute, this is also very useful, suitable for temporary testing -->
     	<File name="log" fileName="D:\test.log" append="false">
     		<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
			<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
		</File>
		
		<!--This will print out all the information, each time the size exceeds size, the log of this size will be automatically stored under the folder created by year-month and compressed as an archive-->
        <RollingFile name="RollingFile" fileName="d:\app.log"
                     filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
     		<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <SizeBasedTriggeringPolicy size="1MB"/>
        </RollingFile>
    </appenders>
    
     
   <loggers>  
   
   		<!--We only let this logger output info information, the others are error levels
        If additivity is enabled, since this logger also satisfies root, it will be printed twice.
        additivity is false, the logger prints it again.
        
        	log level settings for specific classes
        -->
       <!--  <logger name="com.ztao2333.spring.HelloTest" level="info" additivity="false">
            <appender-ref ref="Console"/>
        </logger>
 		-->        
	<!-- log TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
	<root level="debug">       
		<appender-ref ref="Console"/>        
		<appender-ref ref="log"/>   
		<appender-ref ref="RollingFile"/>   
	</root>   
   </loggers>
</configuration>


Learning: http://www.cnblogs.com/leo-lsw/p/log4j2tutorial.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326971849&siteId=291194637