log4j支持自定义的输出。全部的输出都实现了自Appender接口。通常来讲,自定义输出值须要继承AppenderSkeleton类,并实现几个方法就能够了。
写这篇博客,我主要也是想说,框架之全部被成为是一个框架,是在帮咱们完成大部分的通用代码,这就有一个前提就是说它必需要有具备良好的扩张性。方便每个使用者来扩展,固然咱们也能够根据本身的喜爱去改人家框架的源码,可是最实在的也是最有效的去扩展人家开源框架,在扩展的时候咱们也能够参照人家原来的默认实现,这样子对于咱们的学习也是一大进步。
OK,废话不说了,如今咱们开始吧。先来看一个自定义输出的例子,CountingConsoleAppender跟控制台输出相似,不一样的是会统计日志输出的次数。当输出次数超出预约的值时,会作相应的业务处理,这里简单的为打印出一行提示信息,并中止输出。代码以下:
package org.linkinpark.commons.logtest;
import java.util.Objects;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;
public class CountingConsoleAppender extends AppenderSkeleton
{
protected int count = 0;
protected int limit = 10;
/**
* 关闭资源
*/
@Override
public void close()
{
if (this.closed)
{
return;
}
this.closed = true;
}
/**
* 这里须要使用格式化器
*/
@Override
public boolean requiresLayout()
{
return true;
}
@Override
protected void append(LoggingEvent event)
{
// 1,验证,若是没有格式化器,报错,若是次数超过限制,报错
if (this.layout == null)
{
errorHandler.error("没有设置[" + name + "]日志格式化器。", null, ErrorCode.MISSING_LAYOUT);
return;
}
if (count >= limit)
{
errorHandler.error("输出次数[" + limit + "]达到了[" + getName() + "]的上限。", null, ErrorCode.WRITE_FAILURE);
return;
}
// 控制台打印日志
System.out.println(this.layout.format(event));
// 若是配置的格式化器没有处理异常,这里打印异常栈信息
if (layout.ignoresThrowable())
{
String[] throwableStrRep = event.getThrowableStrRep();
if (Objects.nonNull(throwableStrRep))
{
for (String throwStr : throwableStrRep)
{
System.out.println(throwStr);
}
}
}
// 打印日志结束,修改打印次数
count++;
}
public int getCount()
{
return count;
}
public CountingConsoleAppender setCount(int count)
{
this.count = count;
return this;
}
public int getLimit()
{
return limit;
}
public void setLimit(int limit)
{
this.limit = limit;
}
}
配置文件以下:
#定义输出等级和输出appender
log4j.rootLogger=DEBUG,countingconsole
log4j.appender.countingconsole=org.linkinpark.commons.logtest.CountingConsoleAppender
#设置输出样式
log4j.appender.countingconsole.layout=org.apache.log4j.PatternLayout
#日志输出信息格式为
log4j.appender.countingconsole.layout.ConversionPattern=[%-d{yyyy-MM-dd HH:mm:ss}]-[%t-%5p]-[%C-%M(%L)]: %m%n
#控制最大输出次数
log4j.appender.countingconsole.limit=3
#打开4j自己的日志输出
log4j.debug=true
OK,如今咱们来运行下测试看下控制台输出状况,测试代码以下:
package org.linkinpark.commons.logtest;
import org.apache.log4j.Logger;
import org.junit.Test;
/**
* @建立做者: LinkinPark
* @建立时间: 2016年2月23日
* @功能描述: 测试本身扩展的CountConsoleAppender
*/
public class Log4jTest
{
public static Logger log = Logger.getLogger(Log4jTest.class);
@Test
public void logTest()
{
log.debug("debug级别的日志输出");
log.debug("debug级别的日志输出1");
log.debug("debug级别的日志输出2");
log.debug("debug级别的日志输出3");
}
}
测试绿条,控制台输出以下:
log4j: Parsing for [root] with value=[DEBUG,countingconsole].
log4j: Level token is [DEBUG].
log4j: Category root set to DEBUG
log4j: Parsing appender named "countingconsole".
log4j: Parsing layout options for "countingconsole".
log4j: Setting property [conversionPattern] to [[%-d{yyyy-MM-dd HH:mm:ss}]-[%t-%5p]-[%C-%M(%L)]: %m%n ].
log4j: End of parsing for "countingconsole".
log4j: Setting property [limit] to [3].
log4j: Parsed "countingconsole" options.
log4j: Finished configuring.
[2016-02-25 23:42:16]-[main-DEBUG]-[org.linkinpark.commons.logtest.Log4jTest-logTest(19)]: debug级别的日志输出
[2016-02-25 23:42:16]-[main-DEBUG]-[org.linkinpark.commons.logtest.Log4jTest-logTest(20)]: debug级别的日志输出1
[2016-02-25 23:42:16]-[main-DEBUG]-[org.linkinpark.commons.logtest.Log4jTest-logTest(21)]: debug级别的日志输出2
log4j:ERROR 输出次数[3]达到了[countingconsole]的上限。
1,在扩展这个appender的时候,我有参照consoleAppender的实现。核心就是说实现append方法,固然咱们直接继承自
AppenderSkeleton类来进行的扩展,因此能够直接拿到里面的一些属性,好比layput,好比erroHandler等等
2,刚开始的写这个类的时候,我直接定义了一个limit属性,用来控制日志输出次数,直接是在代码中赋的初始值,为了方便,因此我就想写进配置文件中,可是怎么都注入不进去,控制台一直报下面这个error:
log4j:WARN Failed to set property [limit] to value "3".
没办法,我只要打开log4j自己的日志,配置文件中设值log4j.debug=true就OK。后来终于发现个人set方法有问题,这个方法这里必须是void返回类型的,而我通常的set方法都是返回自身this,因此这里没有注入。关于log4j处理set注入我下面一节会整理到。
3,固然咱们在扩展的时候直接继承
ConsoleAppender自这个类也是能够的,这样子的话只须要重写append方法就够了,其余的都不须要了。我本身试了一下测试经过,代码相似,这里不作赘述了。
咱们常常编码,可是其实写反射的代码并非不少,通常的在IOC框架中都是读取配置文件或者说扫描注解来获取相关key-value,返回跑下set方法的反射,就能够设值到一个对象里面去了,这样子的话就能够把一些属性的设值放入到配置文件中,实现解耦。
在之前咱们是这样子编码的:
// 取出须要设置Field值的目标对象
Object target = getObject(objAndProp[0]);
// 该Field对应的setter方法名:set + "属性的首字母大写" + 剩下部分
String mtdName = "set" + objAndProp[1].substring(0 , 1).toUpperCase() + objAndProp[1].substring(1);
// 经过target的getClass()获取它实现类所对应的Class对象
Class<?> targetClass = target.getClass();
// 获取该属性对应的setter方法,下面这一行道出了springIOC的精髓,为何实现XML咱们每次都要提供get和set方法,除了注解的哦
Method mtd = targetClass.getMethod(mtdName , String.class);
// 经过Method的invoke方法执行setter方法,将config.getProperty(name)的属性值做为调用setter的方法的实参
mtd.invoke(target , config.getProperty(name));
看过了log4j的源码之后,咱们多了一种选择,就是使用JDK自带的PropertyDescriptor类,这个类就是按照javabean规范写的一个存储器。
该类里面有2个方法能够直接获取咱们的get和set方法:
setReadMethod,getWriteMethod。之后这也是一种尝试,必要的时候能够参照log4j来用这个方式跑反射。OK,我这里贴出log4j中该类的源码:
package org.apache.log4j.config;
import org.apache.log4j.Appender;
import org.apache.log4j.Level;
import org.apache.log4j.Priority;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.spi.OptionHandler;
import org.apache.log4j.spi.ErrorHandler;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InterruptedIOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Properties;
/**
* General purpose Object property setter. Clients repeatedly invokes
* {@link #setProperty setProperty(name,value)} in order to invoke setters
* on the Object specified in the constructor. This class relies on the
* JavaBeans {@link Introspector} to analyze the given Object Class using
* reflection.
*
* <p>
* Usage:
*
* <pre>
* PropertySetter ps = new PropertySetter(anObject);
* ps.set("name", "Joe");
* ps.set("age", "32");
* ps.set("isMale", "true");
* </pre>
*
* will cause the invocations anObject.setName("Joe"), anObject.setAge(32),
* and setMale(true) if such methods exist with those signatures.
* Otherwise an {@link IntrospectionException} are thrown.
*
* @author Anders Kristensen
* @since 1.1
*/
public class PropertySetter
{
protected Object obj;
protected PropertyDescriptor[] props;
/**
* Create a new PropertySetter for the specified Object. This is done
* in prepartion for invoking {@link #setProperty} one or more times.
*
* @param obj
* the object for which to set properties
*/
public PropertySetter(Object obj)
{
this.obj = obj;
}
/**
* Uses JavaBeans {@link Introspector} to computer setters of object to be
* configured.
*/
protected void introspect()
{
try
{
BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
props = bi.getPropertyDescriptors();
}
catch (IntrospectionException ex)
{
LogLog.error("Failed to introspect " + obj + ": " + ex.getMessage());
props = new PropertyDescriptor[0];
}
}
/**
* Set the properties of an object passed as a parameter in one
* go. The <code>properties</code> are parsed relative to a
* <code>prefix</code>.
*
* @param obj
* The object to configure.
* @param properties
* A java.util.Properties containing keys and values.
* @param prefix
* Only keys having the specified prefix will be set.
*/
public static void setProperties(Object obj, Properties properties, String prefix)
{
new PropertySetter(obj).setProperties(properties, prefix);
}
/**
* Set the properites for the object that match the
* <code>prefix</code> passed as parameter.
*
*
*/
public void setProperties(Properties properties, String prefix)
{
int len = prefix.length();
for (Enumeration e = properties.propertyNames(); e.hasMoreElements();)
{
String key = (String) e.nextElement();
// handle only properties that start with the desired frefix.
if (key.startsWith(prefix))
{
// ignore key if it contains dots after the prefix
if (key.indexOf('.', len + 1) > 0)
{
// System.err.println("----------Ignoring---["+key
// +"], prefix=["+prefix+"].");
continue;
}
String value = OptionConverter.findAndSubst(key, properties);
key = key.substring(len);
if (("layout".equals(key) || "errorhandler".equals(key)) && obj instanceof Appender)
{
continue;
}
//
// if the property type is an OptionHandler
// (for example, triggeringPolicy of org.apache.log4j.rolling.RollingFileAppender)
PropertyDescriptor prop = getPropertyDescriptor(Introspector.decapitalize(key));
if (prop != null && OptionHandler.class.isAssignableFrom(prop.getPropertyType()) && prop.getWriteMethod() != null)
{
OptionHandler opt = (OptionHandler) OptionConverter.instantiateByKey(properties, prefix + key, prop.getPropertyType(), null);
PropertySetter setter = new PropertySetter(opt);
setter.setProperties(properties, prefix + key + ".");
try
{
Method writeMethod = prop.getWriteMethod();
System.out.println("woqu=" + writeMethod);
prop.getWriteMethod().invoke(this.obj, new Object[] { opt });
}
catch (IllegalAccessException ex)
{
LogLog.warn("Failed to set property [" + key + "] to value \"" + value + "\". ", ex);
}
catch (InvocationTargetException ex)
{
if (ex.getTargetException() instanceof InterruptedException || ex.getTargetException() instanceof InterruptedIOException)
{
Thread.currentThread().interrupt();
}
LogLog.warn("Failed to set property [" + key + "] to value \"" + value + "\". ", ex);
}
catch (RuntimeException ex)
{
LogLog.warn("Failed to set property [" + key + "] to value \"" + value + "\". ", ex);
}
continue;
}
setProperty(key, value);
}
}
activate();
}
/**
* Set a property on this PropertySetter's Object. If successful, this
* method will invoke a setter method on the underlying Object. The
* setter is the one for the specified property name and the value is
* determined partly from the setter argument type and partly from the
* value specified in the call to this method.
*
* <p>
* If the setter expects a String no conversion is necessary.
* If it expects an int, then an attempt is made to convert 'value'
* to an int using new Integer(value). If the setter expects a boolean,
* the conversion is by new Boolean(value).
*
* @param name
* name of the property
* @param value
* String value of the property
*/
public void setProperty(String name, String value)
{
if (value == null)
{
return;
}
name = Introspector.decapitalize(name);
PropertyDescriptor prop = getPropertyDescriptor(name);
// LogLog.debug("---------Key: "+name+", type="+prop.getPropertyType());
if (prop == null)
{
LogLog.warn("No such property [" + name + "] in " + obj.getClass().getName() + ".");
}
else
{
try
{
setProperty(prop, name, value);
}
catch (PropertySetterException ex)
{
LogLog.warn("Failed to set property [" + name + "] to value \"" + value + "\". ", ex.rootCause);
}
}
}
/**
* Set the named property given a {@link PropertyDescriptor}.
*
* @param prop
* A PropertyDescriptor describing the characteristics
* of the property to set.
* @param name
* The named of the property to set.
* @param value
* The value of the property.
*/
public void setProperty(PropertyDescriptor prop, String name, String value) throws PropertySetterException
{
Method setter = prop.getWriteMethod();
if (setter == null)
{
throw new PropertySetterException("No setter for property [" + name + "].");
}
Class[] paramTypes = setter.getParameterTypes();
if (paramTypes.length != 1)
{
throw new PropertySetterException("#params for setter != 1");
}
Object arg;
try
{
arg = convertArg(value, paramTypes[0]);
}
catch (Throwable t)
{
throw new PropertySetterException("Conversion to type [" + paramTypes[0] + "] failed. Reason: " + t);
}
if (arg == null)
{
throw new PropertySetterException("Conversion to type [" + paramTypes[0] + "] failed.");
}
LogLog.debug("Setting property [" + name + "] to [" + arg + "].");
try
{
setter.invoke(obj, new Object[] { arg });
}
catch (IllegalAccessException ex)
{
throw new PropertySetterException(ex);
}
catch (InvocationTargetException ex)
{
if (ex.getTargetException() instanceof InterruptedException || ex.getTargetException() instanceof InterruptedIOException)
{
Thread.currentThread().interrupt();
}
throw new PropertySetterException(ex);
}
catch (RuntimeException ex)
{
throw new PropertySetterException(ex);
}
}
/**
* Convert <code>val</code> a String parameter to an object of a
* given type.
*/
protected Object convertArg(String val, Class type)
{
if (val == null)
{
return null;
}
String v = val.trim();
if (String.class.isAssignableFrom(type))
{
return val;
}
else if (Integer.TYPE.isAssignableFrom(type))
{
return new Integer(v);
}
else if (Long.TYPE.isAssignableFrom(type))
{
return new Long(v);
}
else if (Boolean.TYPE.isAssignableFrom(type))
{
if ("true".equalsIgnoreCase(v))
{
return Boolean.TRUE;
}
else if ("false".equalsIgnoreCase(v))
{
return Boolean.FALSE;
}
}
else if (Priority.class.isAssignableFrom(type))
{
return OptionConverter.toLevel(v, Level.DEBUG);
}
else if (ErrorHandler.class.isAssignableFrom(type))
{
return OptionConverter.instantiateByClassName(v, ErrorHandler.class, null);
}
return null;
}
protected PropertyDescriptor getPropertyDescriptor(String name)
{
if (props == null)
{
introspect();
}
for (int i = 0; i < props.length; i++)
{
if (name.equals(props[i].getName()))
{
return props[i];
}
}
return null;
}
public void activate()
{
if (obj instanceof OptionHandler)
{
((OptionHandler) obj).activateOptions();
}
}
}
Log4j源码仍是写的不错的,特别是一些小巧的设计,好比hashtable的性能提高,好比layout引入了解释器模式等等,都是值得咱们借鉴的,在扩展性方面也写的挺好。
若是有必要的咱们能够本身重写一个appender来实现咱们本身的特定功能,OK,先这样子吧。