http://blog.sina.com.cn/s/blog_76a8411a01010u2h.html
首先:當我們引入data-integration\lib文件夾下的所有jar包后 運行java程序要求我們必須引入log4j,由此我們可以確認控制台輸出的信息為log4j做的:
程序如下:
public static void main(String[] args) throws Exception{
KettleEnvironment.init();
try {
JobMeta jobMeta = new JobMeta("E:\\BI\\sample.kjb", null,null);
Job job = new Job(null, jobMeta);
jobMeta.setArguments(new String[]{"aaaaa","bbbbbb"});//傳參數
job.getJobMeta().setInternalKettleVariables(job);
job.setLogLevel(LogLevel.BASIC);
// Start the Job, as it is a Thread itself by Kettle.
job.start();
job.waitUntilFinished();
if (job.getResult() != null && job.getResult().getNrErrors() != 0) {
//Do something here.
}
// Now the job task is finished, mark it as finished.
job.setFinished(true);
// Cleanup the parameters used by the job. Post that invoke GC.
jobMeta.eraseParameters();
job.eraseParameters();
} catch (Exception e) {
e.printStackTrace();
}
}
解決步驟:
1、首先想到查看輸出這些日志的來源:Job類
2、通過反編譯軟件 或者 查看源文件 查看代碼 找到:this.log = new LogChannel(this);
3、進入LogChannel找到: private static LogWriter log = LogWriter.getInstance();
可以看出是單例
4、查看LogWiter的構造方法:
private LogWriter()
{
this.pentahoLogger = Logger.getLogger("org.pentaho.di");
this.pentahoLogger.setAdditivity(false);
this.pentahoLogger.setLevel(Level.ALL);
layout = new Log4jKettleLayout();
boolean consoleAppenderFound = false;
Enumeration appenders = this.pentahoLogger.getAllAppenders();
while (appenders.hasMoreElements()) {
Appender appender = (Appender)appenders.nextElement();
if ((appender instanceof ConsoleAppender)) {
consoleAppenderFound = true;
break;
}
}
if (!consoleAppenderFound) {
Layout patternLayout = new PatternLayout("%-5p %d{dd-MM HH:mm:ss,SSS} - %m%n");
ConsoleAppender consoleAppender = new ConsoleAppender(patternLayout);
consoleAppender.setName("ConsoleAppender:org.pentaho.di");
this.pentahoLogger.addAppender(consoleAppender);
}
LogManager.getLogger("org.apache.commons.vfs").setLevel(Level.WARN);
}
5、看出 原來 他們是判斷有沒有控制台輸出的,如果沒有就加入一個控制台輸出的,
log的識別符為:Logger.getLogger("org.pentaho.di");
6、==============================================================
=================================================================
由此,我們可以再我們程序中加入一行代碼: 手動的去加入一個FileAppender
在try中第一行加入:
Logger.getLogger("org.pentaho.di").addAppender(new FileAppender(new SimpleLayout(), "E:\\logger.log"));
這樣我們就可以把console端打印出來的log 直接輸入到一個log文件中去了