Output console log-to-text in Java programs

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_76a8411a01010u2h.html

First: When we introduce all the jar packages under the Data-integration\lib folder and run the Java program, we have to introduce log4j, so we can confirm that the information for the console output is log4j:

The procedure is as follows:

public static void Main (string[] args) throws exception{
Kettleenvironment.init ();
try {

Jobmetajobmeta = new Jobmeta ("E:\\BI\\SAMPLE.KJB", null,null);
Job Job = Newjob (null, Jobmeta);
Jobmeta.setarguments (newstring[]{"AAAAA", "BBBBBB"});//Pass parameter
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's job task is finished, mark it as Finished.
           job.setfinished (true);

Cleanup the parameters used by the job. Post that INVOKEGC.
Jobmeta.eraseparameters ();
Job.eraseparameters ();
} catch (Exception e) {
E.printstacktrace ();
}

}

Steps to resolve:

1, first think of the source of the view output these logs: Job class

2, through the anti-compilation software or view the source file View Code found: This.log = new Logchannel (this);

3, enter Logchannel find: private staticlogwriter log = Logwriter.getinstance ();

Can be seen as a single case

4. See how the Logwiter is constructed:

Private LogWriter ()
{
This.pentahologger = Logger.getlogger ("Org.pentaho.di");
This.pentahoLogger.setAdditivity (FALSE);

This.pentahoLogger.setLevel (Level.all);

Layout = new Log4jkettlelayout ();

Boolean consoleappenderfound = false;
Enumerationappenders = 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-mmhh:mm:ss,sss}-%m%n");
Consoleappender Consoleappender = Newconsoleappender (patternlayout);
Consoleappender.setname ("ConsoleAppender:org.pentaho.di");
This.pentahoLogger.addAppender (Consoleappender);
}

Logmanager.getlogger ("Org.apache.commons.vfs"). SetLevel (Level.warn);
}
5, see the original they are judged there is no console output, if not added to a console output,

The identifier for log is: Logger.getlogger ("org.pentaho.di");

6, ==============================================================

=================================================================

Thus, we can add a line of code to our program: Manually to join a Fileappender

In the try, the first line joins:

Logger.getlogger ("Org.pentaho.di"). Addappender (Newfileappender (New Simplelayout (), "E:\\logger.log"));

So that we can input the log from the console side directly into a log file.



Output console log-to-text in Java programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.