在使用kettle进行数据分析和清洗时日志非常多而且杂乱,使用原有的日志有时找不到异常的位置,有时日志不够详细,说简单一点就是日志不是我们想要的。因而对kettle日志进行相应的管理就想得尤为重要了。大家都知道java最常用的日志管理包log4j可以很好地实现java日志的管理,然而kettle是使用java开发的,因而log4j是个不二的选择。好了说了这么多,下面看看kettle怎么使用log4j进行日志的管理吧。
1、kettle加载和使用log4j
在转换的JavaScript中添加log4j的支持
//Script here
//updateOuputLog();
//confFile 日志配置文件路径
//增加日志输出文件
//通过/../conf/log.xml文件配置日志输出
function updateOutputLog(confFile){
var first = getVariable("FIRST_TIME_UPDATE_LOG", "true");
if(first == "true"){
logWriter = org.pentaho.di.core.logging.LogWriter.getInstance();
var softPath = getThisSoftPath();
setVariable("WORKDIR", softPath, "s");//设置日志路径变量
var logConf = softPath + "/../conf/log.xml";
if(confFile != null && confFile != ""){
logConf = softPath + "/" + confFile;
}else{
if(!fileExists(logConf)){
logConf = softPath + "/log.xml";
}
if(!fileExists(logConf)){
logConf = softPath + "/conf/log.xml";
}
}
writeToLog("m", "======logConfPath====>"+logConf);
org.apache.log4j.xml.DOMConfigurator.configure(logConf);
var logger = org.apache.log4j.Logger.getLogger("kettle_log");
var appenders = logger.getAllAppenders();
while(appenders.hasMoreElements()){
var appender = appenders.nextElement();
writeToLog("m", "======add==log====>"+appender.getName());
logWriter.addAppender(appender);
}
setVariable("FIRST_TIME_UPDATE_LOG", "false", "r");
}
} //Alert(getThisSoftPath());
//返回程序所在目录 不带前缀file:
function getThisSoftPath(){
var osName = java.lang.System.getProperty("os.name").toLowerCase();
var path = getVariable("Internal.Transformation.Filename.Directory", "");
if(osName.indexOf("windows") >= 0){//WINDOWS系统
path = path.substring(8);
}else{
path = path.substring(7);
}
return path;
} function getConfigPath(softDir,confPath,splitStr){
var arr = confPath.split(splitStr);
var str = softDir+arr[0];
for(var i=1; i<arr.length; i++){
var s = new java.lang.String(arr[i]);
if(arr[i] != null && s.endsWith("properties")){
str = str + splitStr + softDir+ arr[i];
}
}
//Alert(str);
return str;
}
2、准备log4j配置文件log.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="kettle_info" class="org.apache.log4j.DailyRollingFileAppender">
<param name="threshold" value="info" />
<param name="File" value="${WORKDIR}/../log/run.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p (%F:%L) -> %m%n" />
</layout>
</appender>
<appender name="kettle_error" class="org.apache.log4j.DailyRollingFileAppender">
<param name="threshold" value="error" />
<param name="File" value="${WORKDIR}/../log/error.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p (%F:%L) -> %m%n" />
</layout>
</appender>
<logger name="kettle_log">
<appender-ref ref="kettle_info" />
<appender-ref ref="kettle_error" />
</logger>
</log4j:configuration>
此处需要注意log4j日志的存放路径,路径由变量${WORKDIR}控制,存放路径为项目根目录下的log目录。
3、测试
运行结果:
2011-06-08 09:50:28,897 INFO (LogWriter.java:450) -> 设置默认配置文件.0 - ======add==log====>kettle_error
2011-06-08 09:50:28,897 INFO (LogWriter.java:450) -> 设置默认配置文件.0 - 增加配置日志成功
2011-06-08 09:50:28,912 INFO (LogWriter.java:450) -> 设置默认配置文件.0 - E:/test/src/../conf/config_static.properties
2011-06-08 09:50:28,912 INFO (LogWriter.java:450) -> 设置默认配置文件.0 - 完成处理 (I=0, O=0, R=1, W=1, U=1, E=0
2011-06-08 09:50:28,943 INFO (LogWriter.java:450) -> 拆分路径 2.0 - 完成处理 (I=0, O=0, R=1, W=1, U=1, E=0
2011-06-08 09:50:28,943 INFO (LogWriter.java:450) -> 读取配置文件.0 - 配置文件:TO_DB_PWD1=test
2011-06-08 09:50:28,943 INFO (LogWriter.java:450) -> 配置文件输入.0 - 完成处理 (I=20, O=0, R=1, W=20, U=20, E=0
2011-06-08 09:50:28,943 INFO (LogWriter.java:450) -> 读取配置文件.0 - 配置文件:DEST_DB_UNAME2=test
注:11年测试的日志文件