Hi Stan,
Thank you for the suggestion but its not working. I modified the code which is
Exec.java (An example code provided by Jsch) and finally it looks like below-
// ((ChannelExec) channel).setErrStream(System.err);
String errorStream = readStream(((ChannelExec) channel).getErrStream());
logger.error(errorStream);
Now the JSch is not working. I mean it is not returning anything. IMO, reading
from a stream and setting a stream, both are different things. Isn't it?
In the original code Exec.java (line no. 67), error stream is being set whereas
in your suggestion, error stream is being read. At the same time, this error
stream is being closed inside readStream() method, which is causing me to
suspect over it. Should we close the error stream of JSch, I think it must be
taken care by JSch.
To check the code, you can execute "java -version" command in any remote linux
machine, which is written to System.error in original Exec.java
-
Thanks
Ravi
On Tuesday, 31 December 2013 1:46 AM, Stanley Ipkiss <kteli...@gmail.com> wrote:
Ravi,
I stole this from StackOverflow, haven't tested it, but it should
be fairly straightforward:
privateStringreadStream(InputStreamiStream)throwsIOException{//build a Stream
Reader, it can read char by charInputStreamReaderiStreamReader
=newInputStreamReader(iStream);//build a buffered Reader, so that i can read
whole line at onceBufferedReaderbReader
=newBufferedReader(iStreamReader);Stringline =null;StringBuilderbuilder
=newStringBuilder();while((line =bReader.readLine())!=null){//Read till
endbuilder.append(line);}bReader.close();//close all opened
stuffiStreamReader.close();iStream.close();returnbuilder.toString();}
// Call the above routine with the error stream
String errorStream = readStream(channel.getErrStream());
// Log it with your preferred logger
logger.error(errorStream);
Many other threads on stack overflow related to this...
http://stackoverflow.com/questions/6902386/how-to-read-jsch-command-output
Stan
On 12/30/2013 8:59 AM, Ravi Joshi wrote:
Thanks Viet,
>
>
>I got your idea and looked over the example code. However I am looking for any
>short solution for this. Basically the example code
>(http://www.jcraft.com/jsch/examples/Exec.java.html) line no 67 has following
>line-
>
>
>((ChannelExec) channel).setErrStream(System.err);
>
>
>
>Now I have a modified class of Exec.java which uses log4j logger like this-
>
>
>import org.apache.log4j.Logger;
>
>
>public class Exec{
> private static final Logger logger = Logger.getLogger(Exec.class);
> public void execute(){
> //All of the JSch errors are now written to System.error stream
> ((ChannelExec) channel).setErrStream(System.err);
>
>
> //Instead of above line, how can I write these errors to logger
> logger.error(/*set error stream to this*/);
> }
>}
>
>
>How can I achieve this implementation?
>
>-
>Regards
>Ravi
>
>
>
>On Monday, 30 December 2013 7:56 AM, Viet H. Phan <hoangvietp...@yahoo.com>
>wrote:
>
>Hi Ravi,
>
>
>1) Create your own Logger class that implements com.jcraft.jsch.Logger and
>uses log4j
>2) Apply your Logger using
>com.jcraft.jsch.JSch.setLogger(com.jcraft.jsch.Logger) method
>Check Logger class of JSch example for reference.
>
>
>Hope this helps.
>
>
>Regards,
>Viet
>
>
>
>
>
>
>
>________________________________
> From: Ravi Joshi <ravi.josh...@yahoo.com>
>To: "jsch-users@lists.sourceforge.net" <jsch-users@lists.sourceforge.net>
>Sent: Sunday, 29 December 2013 4:47 PM
>Subject: [JSch-users] How to set ChannelExec error stream from System.err to
>log4j
>
>
>
>Hi,
>
>
>I want to execute a command to remote linux machine. I am referring Exec.java
>(http://www.jcraft.com/jsch/examples/Exec.java.html) example code.
>
>
>I just wanted to know How to set ChannelExec error stream from System.err to
>log4j, so that all of these errors, I can get captured by log4j under error
>catagory.
>
>
>
>
>-
>Thanks
>Ravi
>
>
>------------------------------------------------------------------------------
>Rapidly troubleshoot problems before they
affect your business. Most IT
>organizations don't have a clear picture
of how application performance
>affects their revenue. With AppDynamics,
you get 100% visibility into your
>Java,.NET, & PHP application. Start
your 15-day FREE TRIAL of AppDynamics Pro!
>http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
>_______________________________________________
>JSch-users mailing list
>JSch-users@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/jsch-users
>
>
>
>
>
>
>
>------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
>
>
>_______________________________________________
JSch-users mailing list JSch-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jsch-users
------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
JSch-users mailing list
JSch-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jsch-users