1. 程式人生 > >自定義log4j日誌級別

自定義log4j日誌級別

extends false nal obj customer 文件 type word ati

轉載自: 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

[java] view plain copy
  1. package common.log;
  2. import org.apache.log4j.Level;
  3. import org.apache.log4j.Logger;
  4. import org.apache.log4j.net.SyslogAppender;
  5. public class CustomerLog {
  6. /**
  7. * 繼承Level
  8. * @author Sevencm
  9. *
  10. */
  11. private static class CustomerLogLevel extends Level{
  12. public CustomerLogLevel(int level, String levelStr, int syslogEquivalent) {
  13. super(level, levelStr, syslogEquivalent);
  14. }
  15. }
  16. /**
  17. * 自定義級別名稱,以及級別範圍
  18. */
  19. private static final Level CustomerLevel = new CustomerLogLevel(20050,"CUSTOMER",SyslogAppender.LOG_LOCAL0);
  20. /**
  21. * 使用日誌打印logger中的log方法
  22. *
  23. * @param logger
  24. * @param objLogInfo
  25. */
  26. public static void customerLog(Logger logger,Object objLogInfo){
  27. logger.log(CustomerLevel, objLogInfo);
  28. }
  29. }

2、Log Filter [java] view plain copy
  1. package common.log;
  2. import org.apache.log4j.spi.Filter;
  3. import org.apache.log4j.spi.LoggingEvent;
  4. public class CustomerLogFilter extends Filter {
  5. boolean acceptOnMatch = false;
  6. private String levelMin;
  7. private String levelMax;
  8. public String getLevelMin() {
  9. return levelMin;
  10. }
  11. public void setLevelMin(String levelMin) {
  12. this.levelMin = levelMin;
  13. }
  14. public String getLevelMax() {
  15. return levelMax;
  16. }
  17. public void setLevelMax(String levelMax) {
  18. this.levelMax = levelMax;
  19. }
  20. public boolean isAcceptOnMatch() {
  21. return acceptOnMatch;
  22. }
  23. public void setAcceptOnMatch(boolean acceptOnMatch) {
  24. this.acceptOnMatch = acceptOnMatch;
  25. }
  26. @Override
  27. public int decide(LoggingEvent lgEvent) {
  28. int inputLevel = lgEvent.getLevel().toInt();
  29. if(inputLevel>=getLevel(levelMin) && inputLevel <= getLevel(levelMax)){
  30. return 0;
  31. }
  32. return -1;
  33. }
  34. private int getLevel(String level){
  35. level = level.toUpperCase();
  36. if(level.equals("CUSTOMER")){
  37. return LevelType.CUSTOMER.getType();
  38. }
  39. if(level.equals("OFF")){
  40. return LevelType.OFF.getType();
  41. }
  42. if(level.equals("FATAL")){
  43. return LevelType.FATAL.getType();
  44. }
  45. if(level.equals("ERROR")){
  46. return LevelType.ERROR.getType();
  47. }
  48. if(level.equals("INFO")){
  49. return LevelType.INFO.getType();
  50. }
  51. if(level.equals("WARN")){
  52. return LevelType.WARN.getType();
  53. }
  54. if(level.equals("DEBUG")){
  55. return LevelType.DEBUG.getType();
  56. }
  57. if(level.equals("ALL")){
  58. return LevelType.ALL.getType();
  59. }
  60. return LevelType.OFF.getType();
  61. }
  62. private static enum LevelType{
  63. OFF(2147483647),
  64. FATAL(50000),
  65. ERROR(40000),
  66. WARN(30000),
  67. INFO(20000),
  68. DEBUG(10000),
  69. ALL(-2147483648),
  70. CUSTOMER(20050);
  71. int type;
  72. public int getType() {
  73. return type;
  74. }
  75. private LevelType(int type) {
  76. this.type = type;
  77. }
  78. }
  79. }

3、配置文件的設置:


[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3. <log4j:configuration xmlns:log4j=‘http://jakarta.apache.org/log4j/‘ >
  4. <appender name="Console" class="org.apache.log4j.ConsoleAppender">
  5. <layout class="org.apache.log4j.PatternLayout">
  6. <param name="ConversionPattern" value="%d [%p] %c{1} - %m%n" />
  7. </layout>
  8. <!--過濾器設置輸出的級別 -->
  9. <filter class="common.log.CustomerLogFilter">
  10. <param name="levelMin" value="info" />
  11. <param name="levelMax" value="Customer" />
  12. <param name="AcceptOnMatch" value="true" />
  13. </filter>
  14. </appender>
  15. <!-- 根logger的設置-->
  16. <root>
  17. <priority value ="INFO"/>
  18. <appender-ref ref="Console"/>
  19. </root>
  20. </log4j:configuration>

4、測試類


[java] view plain copy
  1. package common.test;
  2. import org.apache.log4j.Logger;
  3. import common.log.CustomerLog;
  4. public class Test {
  5. private static final Logger logger = Logger.getLogger("customer");
  6. public static void main(String[] args) {
  7. CustomerLog.customerLog(logger, "自定義日誌級別");
  8. logger.info("哈哈哈哈");
  9. }
  10. }

5、運行結果:


2014-05-24 17:22:45,647 [CUSTOMER] customer - 自定義日誌級別
2014-05-24 17:22:45,648 [INFO] customer - 哈哈哈哈


6、通過修改

[html] view plain copy
  1. <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日誌級別