功能需求:将 log4j 打印的日志部分(这里是以 "ApiName" 开头的日志)上传到 flume。
实现:
- 设置 log4j.properties 文件配置信息:
log4j.appender.flume=com.joeychang.controller.LogFlumeAppender
log4j.appender.flume.layout=org.apache.log4j.PatternLayout
log4j.appender.flume.Hostname= 118.25.50.11
log4j.appender.flume.Port=44446
log4j.appender.flume.UnsafeMode = true
- 实现自定义 LogFlumeAppender 类 :
public class LogFlumeAppender extends Log4jAppender {
@Override
public synchronized void append(LoggingEvent event) throws FlumeException {
if(event.getMessage().toString().startsWith("ApiName")){
super.append(event);
}
}
}
简单几行代码,解决一个不错的功能,Perfect ...
网友评论