Log4j2 Application

First, change the pom.xml of Maven Project:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.foo</groupId>
  <artifactId>Ber</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Ber</name>
  <url>http://maven.apache.org</url>

 	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		
		<junit.version>4.12</junit.version>
		
		<slf4j.version>1.7.7</slf4j.version>
		<log4j-slf4j-impl.version>2.5</log4j-slf4j-impl.version>
		<log4j-api.version>2.5</log4j-api.version>
		<log4j-core.version>2.5</log4j-core.version>
		<log4j-web.version>2.5</log4j-web.version>
		<disruptor.version>3.3.4</disruptor.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		
		<!-- ===================== log相关 start ======================== -->
		<!-- log4j2核心jar包 -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>${log4j-api.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>${log4j-core.version}</version>
		</dependency> 
		
		<!-- slf4j相关jar包 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>jcl-over-slf4j</artifactId>  
            <version>${slf4j.version}</version>  
            <scope>runtime</scope>  
        </dependency>
        
        <!-- 用于与slf4j保持桥连 -->
        <dependency>  
            <groupId>org.apache.logging.log4j</groupId>  
            <artifactId>log4j-slf4j-impl</artifactId>  
            <version>${log4j-slf4j-impl.version}</version>  
        </dependency>
        
        <!-- web工程需要包含log4j-web,非web工程不需要
        <dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-web</artifactId>
			<version>${log4j-web.version}</version>
		</dependency>  -->
		
		<!--需要使用log4j2的AsyncLogger需要包含disruptor-->  
        <dependency>  
            <groupId>com.lmax</groupId>  
            <artifactId>disruptor</artifactId>  
            <version>${disruptor.version}</version>  
        </dependency>
		<!-- ====================== log相关 end ========================== -->
	</dependencies>
</project>


Second, add log4j2.xml under the src/main/resources:
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
	<Appenders>
		<!-- 文件输出,这里使用绝对地址 -->
		<File name="A1" fileName="F:/LOG_HOME/Ber-test.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{100} -%msg%n"/>
        </File>
        <!-- 控制台输出,在生产环境中不使用 -->
		<Console name="CONSOLE" target="SYSTEM_OUT">
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{100} -%msg%n"/>
		</Console>
		<!-- 异步输出,缓存大小根据项目大小而定 -->
		<Async name="ASYNC" bufferSize="512">
            <AppenderRef ref="A1"/>
            <AppenderRef ref="CONSOLE"/>
        </Async>
	</Appenders>
	<Loggers>
		<!-- 配置日志级别,项目中应配置到ERROR级别 -->
		<Root level="TRACE">
			<AppenderRef ref="ASYNC" />
		</Root>
	</Loggers>
</Configuration>

Third, new App.java under the src/main/java:

App.java

package foo;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Hello Log4j2!
 * @author Tracy_Sero
 */
public class App {
	
	private static final Logger logger = LogManager.getLogger(App.class);
	
    public static void main( String[] args ) {
    	
    	logger.trace("trace:Log4j2 Begin!");
    	
    	logger.debug("debug:Hello Log4j2!");
    	logger.info("info:Hello Log4j2!");
    	logger.warn("warn:Hello Log4j2!");
    	logger.error("error:Hello Log4j2!");
    	
    	logger.trace("trace:Log4j2 End!");
    	
    }
}

Finally, run App.java as Java Application and we will see the result:
    Run App.java

Console output:
2016-04-15 17:02:02 [main] TRACE foo.App -trace:Log4j2 Begin!
2016-04-15 17:02:02 [main] DEBUG foo.App -debug:Hello Log4j2!
2016-04-15 17:02:02 [main] INFO  foo.App -info:Hello Log4j2!
2016-04-15 17:02:02 [main] WARN  foo.App -warn:Hello Log4j2!
2016-04-15 17:02:02 [main] ERROR foo.App -error:Hello Log4j2!
2016-04-15 17:02:02 [main] TRACE foo.App -trace:Log4j2 End!

File output:
Address at F:/LOG_HOME/Ber-test.log
2016-04-15 17:02:02 [main] TRACE foo.App -trace:Log4j2 Begin!
2016-04-15 17:02:02 [main] DEBUG foo.App -debug:Hello Log4j2!
2016-04-15 17:02:02 [main] INFO  foo.App -info:Hello Log4j2!
2016-04-15 17:02:02 [main] WARN  foo.App -warn:Hello Log4j2!
2016-04-15 17:02:02 [main] ERROR foo.App -error:Hello Log4j2!
2016-04-15 17:02:02 [main] TRACE foo.App -trace:Log4j2 End!

Okay, have a try!I'd love to hear your comments and inputs.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值