Ravi,

I stole this from StackOverflow, haven't tested it, but it should be fairly straightforward:

|    private  String  readStream(InputStream  iStream)  throws  IOException  {
        //build a Stream Reader, it can read char by char
        InputStreamReader  iStreamReader=  new  InputStreamReader(iStream);
        //build a buffered Reader, so that i can read whole line at once
        BufferedReader  bReader=  new  BufferedReader(iStreamReader);
        String  line=  null;
        StringBuilder  builder=  new  StringBuilder();
        while((line=  bReader.readLine())  !=  null)  {   //Read till end
            builder.append(line);
        }
        bReader.close();          //close all opened stuff
        iStreamReader.close();
        iStream.close();
        return  builder.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> (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 <mailto: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

Reply via email to