Redirect the output from the console to a file

Community Manager

Redirect the output from the console to a file

A common question I get when working on client sites is, "How can we save the output from the console without having to copy and paste it?". The console (from Talend DI v6) is shown below.

 

quicktips_console.jpg

Unfortunately there isn't a "built-in" way of doing this, but it is very simple to implement this functionality. All you need to do is start your job with a tJava component and add the code below to it.....

java.io.File outputFile = new java.io.File("e:/output.txt");
System.setOut(new java.io.PrintStream(new java.io.FileOutputStream(outputFile, true), true));

 

This code redirects the console output to a file called "output.txt" on the e: drive. Change the file location as required.

Using this trick will ensure that all logging output is sent to the file, but you will no longer see it in the console.

All errors will be shown in the console though.

 

NOTE: I received a question from @gslokoski about whether the output could be split so that it remains in the console as well as being added to a file. I suggested trying a solution I had found on Google. @gslokoski came back with this solution which I have subsequently tried. It's pretty nice, so I thought I would add it here. All credit to @gslokoski.

 

java.io.File outputFile = new java.io.File("c:/temp/output.txt");
java.io.FileOutputStream fos = new java.io.FileOutputStream(outputFile);
//we will want to print in standard "System.out" and in "file"
org.apache.commons.io.output.TeeOutputStream myOut=new org.apache.commons.io.output.TeeOutputStream(System.out, fos);
java.io.PrintStream ps = new java.io.PrintStream(myOut, true); //true - auto-flush after println
System.setOut(ps);
System.setErr(ps);

You will need to add the commons-io-2.4.jar which comes with Talend. You can do this with a tLibraryLoad component.

Six Stars

Re: Redirect the output from the console to a file

This is Nice and Brilliant! I use tJava to print it in the standard output and use tWarn with (info) and tLogCatcher and tFileOutputDelimited to write the same information in a file. This is simple and sleek. I will try this for sure!!!  

Six Stars

Re: Redirect the output from the console to a file

colud you ellabrate this i have very little knowledge on java i can't understand this code
Community Manager

Re: Redirect the output from the console to a file

What don't you understand? The code can be used "as is" so long as the path to your log file is acceptable. Otherwise, just change that path.

Six Stars

Re: Redirect the output from the console to a file

Can you please attache the job and code. it will help for others.

 

Regards,

Community Manager

Re: Redirect the output from the console to a file

@apentyala there is no job, you simply copy the code I have given into a tJava. It is that simple, honestly :-)

Four Stars

Re: Redirect the output from the console to a file

Works perfectly! Just what I needed, thanks.

Five Stars

Re: Redirect the output from the console to a file


@rhall_2_0 wrote:

@apentyala there is no job, you simply copy the code I have given into a tJava. It is that simple, honestly :-)


Someone  get size file 0KB in the Folder ?

I use this code but its look like is not working the file creating.... but is  always empty.

its happen to any one >?

 

Community Manager

Re: Redirect the output from the console to a file

Where is the tJava component in your job? It should either be at the beginning (a start component) or connected to the tPreJob component. 

Four Stars

Re: Redirect the output from the console to a file

it is not working. Output file is creating with status disconnected all the data in console is not saving

Highlighted
Community Manager

Re: Redirect the output from the console to a file

Can you give a bit more detail about what you are doing and what is happening? If you have the code configured as in the original post, you should be seeing the output in the file and not in the output window.

Four Stars

Re: Redirect the output from the console to a file

I Have to track the data truncation errors in Talend ,Data truncation errors is not capturing in any log components available in talend.

But those errors will come in talend console,So I want to capture data in console to identify those data truncation errors.
Community Manager

Re: Redirect the output from the console to a file

Ah OK. You need to add some code for the errors. Try this (but change the file paths to your output and error files)...

 

java.io.File outputFile = new java.io.File("/Users/richardhall/Documents/output.txt");
java.io.File outputFileErr = new java.io.File("/Users/richardhall/Documents/outputErr.txt");
System.setOut(new java.io.PrintStream(new java.io.FileOutputStream(outputFile, true), true));
System.setErr(new java.io.PrintStream(new java.io.FileOutputStream(outputFileErr, true), true));
Four Stars

Re: Redirect the output from the console to a file

Thanks,It was Working.
Is there any possibility to edit Excel and Xml Files in Talend .

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog