Refer to the online data: http://www.360doc.com/content/13/0527/11/10825198_288498671.shtml
Because the project needs to output some special log to do data statistics. If you turn on the info log level provided by log4j, the log files generated every day will become larger and bigger. This should be done by writing a timed task to delete this file. In order to output only the required log level, it is only by defining a log4j level that you can control, and you do not need to modify the previous code. Well, nonsense not much to say, on the code:
1, Customerlog
Package common.log;
Import Org.apache.log4j.Level;
Import Org.apache.log4j.Logger;
Import Org.apache.log4j.net.SyslogAppender;
public class Customerlog {
/**
* Inheritance level
* @author SEVENCM
* */
private Static class Customerloglevel extends level{public
customerloglevel (int level, String levelstr, int syslogequivalent) {
Super (level, LEVELSTR, syslogequivalent);
}
/**
* Custom level name, and level range
/private static final levels Customerlevel = new Customerloglevel (20050, "CUSTOMER ", syslogappender.log_local0);
/**
* Log printing in logger using logs * *
@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), INF
O (20000), DEBUG (10000), all ( -2147483648), CUSTOMER (20050);
int type;
public int GetType () {return type;
Private LevelType (int type) {this.type = type;
}
}
}
3, the configuration file settings:
<?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 sets the level of output-->
<filter class=" Common.log.CustomerLogFilter " >
<param name= "levelmin" value= "info"/> <param name=
"Levelmax" value= "Customer"/>
< param name= "Acceptonmatch" value= "true"/>
</filter>
</appender>
<!-- Root logger settings-->
<root>
<priority value = "INFO"/>
<appender-ref ref= "Console"/>
</root>
</log4j:configuration>
4. Test class
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, custom log level);
Logger.info ("haha haha");
}
5. Operation Result:
2014-05-24 17:22:45,647 [Customer] customer-custom log level
2014-05-24 17:22:45,648 [INFO] customer-haha haha
6. By modifying
<param name= "Levelmin" value= "info"/>
The value above to control the output level of the log.