Log4j 2.0 使用说明(3) 之组件及过滤器

测试用代码:

package com.test;

import java.util.Random;

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

public class TestService {
	private Logger logger = LogManager.getLogger(TestService.class.getName());
	
	private String[] messages = new String[] {
			"Hello, World",
			"Goodbye Cruel World",
			"You had me at hello"
	};
	private Random rand = new Random(1);
	
	public String retrieveMessage() {
		logger.entry();
		String testMsg = getMessage(getKey());
		return logger.exit(testMsg);
	}
	
	public void exampleException() {
		logger.entry();
		try {
			String msg = messages[messages.length];
			logger.error("An exception should have been throw");
		} catch (Exception e) {
			logger.catching(e);
		}
		logger.exit();
	}
	
	public String getMessage(int key) {
		logger.entry(key);
		String value = messages[key];
		return logger.exit(value);
	}
	
	public int getKey() {
		logger.entry();
		int key = rand.nextInt(messages.length);
		return logger.exit(key);
	}
}

package com.test;

public class App {

	public static void main(String[] args) {
		TestService service = new TestService();
		service.retrieveMessage();
		service.retrieveMessage();
		service.exampleException();
	}
}

程序输出:

10:10:46.078 TRACE com.test.TestService 19 retrieveMessage -  entry
10:10:46.171 TRACE com.test.TestService 42 getKey -  entry
10:10:46.171 TRACE com.test.TestService 44 getKey -  exit with (0)
10:10:46.171 TRACE com.test.TestService 36 getMessage -  entry parms(0)
10:10:46.171 TRACE com.test.TestService 38 getMessage -  exit with (Hello, World)
10:10:46.171 TRACE com.test.TestService 21 retrieveMessage -  exit with (Hello, World)
10:10:46.171 TRACE com.test.TestService 19 retrieveMessage -  entry
10:10:46.171 TRACE com.test.TestService 42 getKey -  entry
10:10:46.171 TRACE com.test.TestService 44 getKey -  exit with (1)
10:10:46.171 TRACE com.test.TestService 36 getMessage -  entry parms(1)
10:10:46.171 TRACE com.test.TestService 38 getMessage -  exit with (Goodbye Cruel World)
10:10:46.171 TRACE com.test.TestService 21 retrieveMessage -  exit with (Goodbye Cruel World)
10:10:46.171 TRACE com.test.TestService 25 exampleException -  entry
10:10:46.171 DEBUG com.test.TestService 30 exampleException - catching java.lang.ArrayIndexOutOfBoundsException: 3
	at com.test.TestService.exampleException(TestService.java:27) [bin/:?]
	at com.test.App.main(App.java:9) [bin/:?]


10:10:46.187 TRACE com.test.TestService 32 exampleException -  exit
其Xml配置文件为:

<?xml version="1.0" encoding="UTF-8"?>
<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="target/test.log" append="false">
	  <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
	</File>
	<RollingFile name="RollingFile" fileName="logs/app.log"
                 filePattern="logs/$${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="500 MB" />
    </RollingFile>
  </appenders>
  <loggers>
	<root level="trace">
	  <appender-ref ref="RollingFile"/>
	  <appender-ref ref="Console"/>
    </root>
  </loggers>
</configuration>

扩展组件

1,ConsoleAppender

输出结果到System.out或是System.err。

2,FileAppender

输出结果到指定文件,同时可以指定输出数据的格式。

3,RollingFileAppender

自动追加日志信息到文件中,直至文件达到预定的大小,然后自动重新生成另外一个文件来记录之后的日志。

过滤标签

1,ThresholdFilter

用来过滤指定优先级的事件。

2,TimeFilter

设置start和end,来指定接收日志信息的时间区间。

你可能感兴趣的:(log4j,exception,String,Random,Class,encoding)