Hi Viet,

Here we go-

import java.io.IOException;
import java.io.OutputStream;
import org.apache.log4j.Logger;

public class Log4JOutputStream extends OutputStream {
    private static final Logger logger = 
Logger.getLogger(Log4JOutputStream.class);
    final StringBuilder string = new StringBuilder();

    @Override
    public void write(int b) throws IOException {
    char current = (char) b;
    if (current == '\n') {
            logger.error(string.toString());
            // Reset it
            string.setLength(0);
        } else {
            string.append(current);
        }
    }
}


and the main class is 
((ChannelExec) channel).setErrStream(new Log4JOutputStream());


-
Thanks
Ravi




On Tuesday, 31 December 2013 8:59 AM, Viet H. Phan <hoangvietp...@yahoo.com> 
wrote:
 
Hi Ravi,

Following please find a solution:
1) Create a class (Log4JOutputStream) that extends OutputStream. Implement 
write(...)methods of Log4JOutputStream in which you actually use log4j
2) Set a Log4JOutputStreaminstance to the ChannelExec channel

Hope this helps.

Regards,
Viet


________________________________
 From: Ravi Joshi <ravi.josh...@yahoo.com>
To: Viet H. Phan <hoangvietp...@yahoo.com>; "jsch-users@lists.sourceforge.net" 
<jsch-users@lists.sourceforge.net> 
Sent: Monday, 30 December 2013 11:59 PM
Subject: Re: [JSch-users] How to set ChannelExec error stream from System.err 
to        log4j
 


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

Reply via email to