zoukankan      html  css  js  c++  java
  • 自定义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

    1. package common.log;  
    2.   
    3. import org.apache.log4j.Level;  
    4. import org.apache.log4j.Logger;  
    5. import org.apache.log4j.net.SyslogAppender;  
    6.   
    7. public class CustomerLog {    
    8.       
    9.     /** 
    10.      * 继承Level 
    11.      * @author Sevencm 
    12.      * 
    13.      */  
    14.     private static class CustomerLogLevel extends Level{  
    15.         public CustomerLogLevel(int level, String levelStr, int syslogEquivalent) {  
    16.             super(level, levelStr, syslogEquivalent);  
    17.         }         
    18.     }  
    19.       
    20.     /** 
    21.      * 自定义级别名称,以及级别范围 
    22.      */  
    23.     private static final Level CustomerLevel = new CustomerLogLevel(20050,"CUSTOMER",SyslogAppender.LOG_LOCAL0);  
    24.       
    25.     /** 
    26.      * 使用日志打印logger中的log方法 
    27.      *  
    28.      * @param logger 
    29.      * @param objLogInfo 
    30.      */  
    31.     public static void customerLog(Logger logger,Object objLogInfo){  
    32.         logger.log(CustomerLevel, objLogInfo);  
    33.     }  
    34.       
    35.       
    36.       
    37. }  

    2、Log Filter
    1. package common.log;  
    2.   
    3. import org.apache.log4j.spi.Filter;  
    4. import org.apache.log4j.spi.LoggingEvent;  
    5.   
    6. public class CustomerLogFilter extends Filter {  
    7.     boolean acceptOnMatch = false;  
    8.     private String levelMin;  
    9.     private String levelMax;  
    10.       
    11.       
    12.       
    13.       
    14.       
    15.     public String getLevelMin() {  
    16.         return levelMin;  
    17.     }  
    18.   
    19.   
    20.   
    21.     public void setLevelMin(String levelMin) {  
    22.         this.levelMin = levelMin;  
    23.     }  
    24.   
    25.   
    26.   
    27.     public String getLevelMax() {  
    28.         return levelMax;  
    29.     }  
    30.   
    31.   
    32.   
    33.     public void setLevelMax(String levelMax) {  
    34.         this.levelMax = levelMax;  
    35.     }  
    36.   
    37.   
    38.   
    39.     public boolean isAcceptOnMatch() {  
    40.         return acceptOnMatch;  
    41.     }  
    42.   
    43.   
    44.   
    45.     public void setAcceptOnMatch(boolean acceptOnMatch) {  
    46.         this.acceptOnMatch = acceptOnMatch;  
    47.     }  
    48.   
    49.   
    50.     @Override  
    51.     public int decide(LoggingEvent lgEvent) {  
    52.         int inputLevel = lgEvent.getLevel().toInt();  
    53.           
    54.         if(inputLevel>=getLevel(levelMin) && inputLevel <= getLevel(levelMax)){  
    55.             return 0;  
    56.         }         
    57.           
    58.         return -1;  
    59.     }  
    60.       
    61.     private int getLevel(String level){  
    62.         level = level.toUpperCase();  
    63.         if(level.equals("CUSTOMER")){  
    64.             return LevelType.CUSTOMER.getType();  
    65.         }  
    66.         if(level.equals("OFF")){  
    67.             return LevelType.OFF.getType();  
    68.         }  
    69.         if(level.equals("FATAL")){  
    70.             return LevelType.FATAL.getType();  
    71.         }  
    72.         if(level.equals("ERROR")){  
    73.             return LevelType.ERROR.getType();  
    74.         }  
    75.         if(level.equals("INFO")){  
    76.             return LevelType.INFO.getType();  
    77.         }  
    78.         if(level.equals("WARN")){  
    79.             return LevelType.WARN.getType();  
    80.         }  
    81.         if(level.equals("DEBUG")){  
    82.             return LevelType.DEBUG.getType();  
    83.         }  
    84.         if(level.equals("ALL")){  
    85.             return LevelType.ALL.getType();  
    86.         }  
    87.         return LevelType.OFF.getType();  
    88.     }  
    89.       
    90.     private static enum LevelType{  
    91.           
    92.         OFF(2147483647),  
    93.           
    94.         FATAL(50000),  
    95.           
    96.         ERROR(40000),  
    97.           
    98.         WARN(30000),  
    99.           
    100.         INFO(20000),  
    101.           
    102.         DEBUG(10000),  
    103.           
    104.         ALL(-2147483648),  
    105.           
    106.         CUSTOMER(20050);  
    107.           
    108.         int type;  
    109.           
    110.         public int getType() {  
    111.             return type;  
    112.         }  
    113.   
    114.         private LevelType(int type) {  
    115.             this.type = type;  
    116.         }  
    117.     }  
    118.   
    119. }  

    3、配置文件的设置:


    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
    3.     
    4. <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >  
    5.     
    6.   
    7.     <appender name="Console" class="org.apache.log4j.ConsoleAppender">  
    8.         <layout class="org.apache.log4j.PatternLayout">  
    9.             <param name="ConversionPattern" value="%d [%p] %c{1} - %m%n" />  
    10.         </layout>  
    11.         <!--过滤器设置输出的级别 -->  
    12.         <filter class="common.log.CustomerLogFilter">  
    13.             <param name="levelMin" value="info" />  
    14.             <param name="levelMax" value="Customer" />  
    15.             <param name="AcceptOnMatch" value="true" />  
    16.         </filter>  
    17.     </appender>  
    18.   
    19.     <!-- 根logger的设置-->  
    20.     <root>  
    21.         <priority value ="INFO"/>  
    22.         <appender-ref ref="Console"/>  
    23.     </root>  
    24. </log4j:configuration>  

    4、测试类


    1. package common.test;  
    2.   
    3. import org.apache.log4j.Logger;  
    4.   
    5. import common.log.CustomerLog;  
    6.   
    7. public class Test {  
    8.     private static final Logger logger = Logger.getLogger("customer");  
    9.       
    10.     public static void main(String[] args) {  
    11.         CustomerLog.customerLog(logger, "自定义日志级别");  
    12.         logger.info("哈哈哈哈");  
    13.     }  
    14. }  

    5、运行结果:


    2014-05-24 17:22:45,647 [CUSTOMER] customer - 自定义日志级别
    2014-05-24 17:22:45,648 [INFO] customer - 哈哈哈哈


    6、通过修改

    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

  • 相关阅读:
    Log Explorer的使用
    Windows消息大全
    Devepress LayoutControl的使用
    IIS7虚拟目录出现HTTP错误500.19(由于权限不足而无法读取配置文件)
    检索参数信息并填充指定的 SqlCommand 对象的 Parameters 集合
    Failed to access IIS metabase.
    SQL Server FOR XML PATH 语句的应用
    C#调用API:mouse_event 模拟鼠标事件
    C#执行SQL脚本
    ADO.NET 中的表达式
  • 原文地址:https://www.cnblogs.com/widget90/p/7501291.html
Copyright © 2011-2022 走看看