自定义log4j日志级别
Posted 池的巧克力
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义log4j日志级别相关的知识,希望对你有一定的参考价值。
转载自: http://blog.csdn.net/seven_cm/article/details/26849821
自定义log4j日志级别
参考了网上资料: http://www.360doc.com/content/13/0527/11/10825198_288498671.shtml
因为项目中需要输出一些特别的日志来做数据统计。如果开启log4j提供的INFO日志级别,每天生成的日志文件就会变得越来越大。这样就得写个定 时任务来删除这个文件。为了只输出所需的日志级别,唯有自己定义一个log4j的级别,这样一来就好控制了,而且不需要对之前的代码进行修改。好了,废话 不多说,上代码:
1、CustomerLog
- package common.log;
- import org.apache.log4j.Level;
- import org.apache.log4j.Logger;
- import org.apache.log4j.net.SyslogAppender;
- public class CustomerLog {
- /**
- * 继承Level
- * @author Sevencm
- *
- */
- private static class CustomerLogLevel extends Level{
- public CustomerLogLevel(int level, String levelStr, int syslogEquivalent) {
- super(level, levelStr, syslogEquivalent);
- }
- }
- /**
- * 自定义级别名称,以及级别范围
- */
- private static final Level CustomerLevel = new CustomerLogLevel(20050,"CUSTOMER",SyslogAppender.LOG_LOCAL0);
- /**
- * 使用日志打印logger中的log方法
- *
- * @param logger
- * @param objLogInfo
- */
- public static void customerLog(Logger logger,Object objLogInfo){
- logger.log(CustomerLevel, objLogInfo);
- }
- }
2、Log Filter
- package common.log;
- import org.apache.log4j.spi.Filter;
- import org.apache.log4j.spi.LoggingEvent;
- public class CustomerLogFilter extends Filter {
- boolean acceptOnMatch = false;
- private String levelMin;
- private String levelMax;
- public String getLevelMin() {
- return levelMin;
- }
- public void setLevelMin(String levelMin) {
- this.levelMin = levelMin;
- }
- public String getLevelMax() {
- return levelMax;
- }
- public void setLevelMax(String levelMax) {
- this.levelMax = levelMax;
- }
- public boolean isAcceptOnMatch() {
- return acceptOnMatch;
- }
- public void setAcceptOnMatch(boolean acceptOnMatch) {
- this.acceptOnMatch = acceptOnMatch;
- }
- @Override
- public int decide(LoggingEvent lgEvent) {
- int inputLevel = lgEvent.getLevel().toInt();
- if(inputLevel>=getLevel(levelMin) && inputLevel <= getLevel(levelMax)){
- return 0;
- }
- return -1;
- }
- private int getLevel(String level){
- level = level.toUpperCase();
- if(level.equals("CUSTOMER")){
- return LevelType.CUSTOMER.getType();
- }
- if(level.equals("OFF")){
- return LevelType.OFF.getType();
- }
- if(level.equals("FATAL")){
- return LevelType.FATAL.getType();
- }
- if(level.equals("ERROR")){
- return LevelType.ERROR.getType();
- }
- if(level.equals("INFO")){
- return LevelType.INFO.getType();
- }
- if(level.equals("WARN")){
- return LevelType.WARN.getType();
- }
- if(level.equals("DEBUG")){
- return LevelType.DEBUG.getType();
- }
- if(level.equals("ALL")){
- return LevelType.ALL.getType();
- }
- return LevelType.OFF.getType();
- }
- private static enum LevelType{
- OFF(2147483647),
- FATAL(50000),
- ERROR(40000),
- WARN(30000),
- INFO(20000),
- DEBUG(10000),
- ALL(-2147483648),
- CUSTOMER(20050);
- int type;
- public int getType() {
- return type;
- }
- private LevelType(int type) {
- this.type = type;
- }
- }
- }
3、配置文件的设置:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- <log4j:configuration xmlns:log4j=‘http://jakarta.apache.org/log4j/‘ >
- <appender name="Console" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d [%p] %c{1} - %m%n" />
- </layout>
- <!--过滤器设置输出的级别 -->
- <filter class="common.log.CustomerLogFilter">
- <param name="levelMin" value="info" />
- <param name="levelMax" value="Customer" />
- <param name="AcceptOnMatch" value="true" />
- </filter>
- </appender>
- <!-- 根logger的设置-->
- <root>
- <priority value ="INFO"/>
- <appender-ref ref="Console"/>
- </root>
- </log4j:configuration>
4、测试类
- package common.test;
- import org.apache.log4j.Logger;
- import common.log.CustomerLog;
- public class Test {
- private static final Logger logger = Logger.getLogger("customer");
- public static void main(String[] args) {
- CustomerLog.customerLog(logger, "自定义日志级别");
- logger.info("哈哈哈哈");
- }
- }
5、运行结果:
2014-05-24 17:22:45,647 [CUSTOMER] customer - 自定义日志级别
2014-05-24 17:22:45,648 [INFO] customer - 哈哈哈哈
6、通过修改
- <param name="levelMin" value="info" />
上面的 Value 值来控制日志的输出级别。
另外说明常用log4j日志级别具体值:
public class Level extends Priority
implements Serializable {
public static final int TRACE_INT = 5000;
public static final Level OFF = new Level(2147483647, "OFF", 0);
public static final Level FATAL = new Level(50000, "FATAL", 0);
public static final Level ERROR = new Level(40000, "ERROR", 3);
public static final Level WARN = new Level(30000, "WARN", 4);
public static final Level INFO = new Level(20000, "INFO", 6);
public static final Level DEBUG = new Level(10000, "DEBUG", 7);
public static final Level TRACE = new Level(5000, "TRACE", 7);
}
像FATAL、ERROR这些级别,实际上对应一数字50000、40000
以上是关于自定义log4j日志级别的主要内容,如果未能解决你的问题,请参考以下文章
Log4j 自定义配置为每个级别分隔日志 - Playframework 1.2.5
Java日志框架 -- LOG4J(Log4j入门案例日志级别Log4j组件(LoggersAppendersLayouts)配置文件内置日志记录自定义Logger)
Java日志框架 -- LOG4J(Log4j入门案例日志级别Log4j组件(LoggersAppendersLayouts)配置文件内置日志记录自定义Logger)