log4j 自定义 level
2015-08-06 12:55
375 查看
下面自定义一个EMAIL级别
经过查看源码找到了一个简单的新增自定义level的方式
org.apache.log4j.helpers.OptionConverter#toLevel
从源码可以看出自定义level是通过#来分割level名称和class
-----------------
然后自定义了一个 EmailLogger
log4j.xml
经过查看源码找到了一个简单的新增自定义level的方式
org.apache.log4j.helpers.OptionConverter#toLevel
Level toLevel(String value, Level defaultValue) { if(value == null) return defaultValue; value = value.trim(); int hashIndex = value.indexOf('#'); if (hashIndex == -1) { if("NULL".equalsIgnoreCase(value)) { return null; } else { // no class name specified : use standard Level class return(Level) Level.toLevel(value, defaultValue); } } Level result = defaultValue; String clazz = value.substring(hashIndex+1); String levelName = value.substring(0, hashIndex); // This is degenerate case but you never know. if("NULL".equalsIgnoreCase(levelName)) { return null; } LogLog.debug("toLevel" + ":class=[" + clazz + "]" + ":pri=[" + levelName + "]"); try { Class customLevel = Loader.loadClass(clazz); // get a ref to the specified class' static method // toLevel(String, org.apache.log4j.Level) Class[] paramTypes = new Class[] { String.class, org.apache.log4j.Level.class }; java.lang.reflect.Method toLevelMethod = customLevel.getMethod("toLevel", paramTypes); // now call the toLevel method, passing level string + default Object[] params = new Object[] {levelName, defaultValue}; Object o = toLevelMethod.invoke(null, params); result = (Level) o; } catch(ClassNotFoundException e) { LogLog.warn("custom level class [" + clazz + "] not found."); } catch(NoSuchMethodException e) { LogLog.warn("custom level class [" + clazz + "]" + " does not have a class function toLevel(String, Level)", e); } catch(java.lang.reflect.InvocationTargetException e) { if (e.getTargetException() instanceof InterruptedException || e.getTargetException() instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.warn("custom level class [" + clazz + "]" + " could not be instantiated", e); } catch(ClassCastException e) { LogLog.warn("class [" + clazz + "] is not a subclass of org.apache.log4j.Level", e); } catch(IllegalAccessException e) { LogLog.warn("class ["+clazz+ "] cannot be instantiated due to access restrictions", e); } catch(RuntimeException e) { LogLog.warn("class ["+clazz+"], level ["+levelName+ "] conversion failed.", e); } return result; }
从源码可以看出自定义level是通过#来分割level名称和class
-----------------
然后自定义了一个 EmailLogger
public class EmailLogger extends Logger { private static final String FQCN = EmailLogger.class.getName(); private static Logger log = Logger.getLogger(EmailLogger.class); protected EmailLogger(String name) { super(name); } public static void email(Object message){ log.log(FQCN, EZheLogLevel.EMAIL, message, null); } public void email(Object message, Throwable t){ log.log(FQCN, EZheLogLevel.EMAIL, message, t); } }
log4j.xml
<appender name="EMAIL_APPENDER" class="com.gozap.logging.log4j.EmailAppender"> <param name="Threshold" value="EMAIL#com.xxx.logging.log4j.EmailLogLevel" /> <triggeringPolicy class="com.gozap.logging.log4j.EmailEventTriggering"> <param name="level" value="EMAIL#com.xxx.logging.log4j.EmailLogLevel" /> </triggeringPolicy> </appender>
public final static int EMAIL_INT = 45000; public static Level EMAIL = new Level(EMAIL_INT, "EMAIL", 2);
相关文章推荐
- OC 便利构造器 自定义初始化 分数加减乘除
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- ACM贪心覆盖问题
- double格式小数位
- ssl自签名证书生成脚本
- Unity项目工程集成Mob社会化分享SDK(android篇)
- 优化算法的比较,选择
- mysql左连和右连
- spring配置中mappingResources和mappingLocations
- iOS绘图API的使用
- 【暑期基础2】O HDU 2040 亲和数
- poj 3264 Balanced Lineup 【RMQ 裸题】
- Hibernate单向关联1-N
- 【@property的特性】
- 有关XCode6(iOS8)UITableViewCell与iOS7在UITableViewCell问题
- 查看linux并发连接数的方法
- Web.xml配置详解之context-param
- 汉诺塔
- mac下wireshark找不到网卡解决办法