JSch Logs in files

Use Logger.log in the MyLogger.log:

public void log(int level, String message){
    LOGGER.log(loggerlevel, message);
}

A full code can be like:

static private class MyJSchLogger implements com.jcraft.jsch.Logger {
    private java.util.logging.Logger logger;

    public MyJSchLogger(java.util.logging.Logger logger) {
        this.logger = logger;
    }

    public boolean isEnabled(int level){
        return true;
    }
    public void log(int level, String message){
        java.util.logging.Level l;
        switch (level)
        {
        case com.jcraft.jsch.Logger.DEBUG:
            l = java.util.logging.Level.FINE;
            break;
        case com.jcraft.jsch.Logger.INFO:
            l = java.util.logging.Level.INFO;
            break;
        case com.jcraft.jsch.Logger.WARN:
            l = java.util.logging.Level.WARNING;
            break;
        default:
        case com.jcraft.jsch.Logger.ERROR:
        case com.jcraft.jsch.Logger.FATAL:
            l = java.util.logging.Level.SEVERE;
            break;
        }
        this.logger.log(l, message);
    }
}

To associate the logger with JSch use:

JSch.setLogger(new MyJSchLogger(logger));

Assuming the Java logger exists.

If not, you can create one like:

java.util.logging.Logger logger = java.util.logging.Logger.getLogger("MyJSch");
java.util.logging.FileHandler fh = new java.util.logging.FileHandler("C:\\path\\jsch.log");
java.util.logging.SimpleFormatter formatter = new java.util.logging.SimpleFormatter();  
fh.setFormatter(formatter);  
logger.addHandler(fh);

Though if you just need to log to a file, you can do it directly:

JSch.setLogger(new com.jcraft.jsch.Logger() {
    Path path = Paths.get("C:\\path\\jsch.log");
    @Override
    public boolean isEnabled(int level){
        return true;
    }
    public void log(int level, String message){
        try {
            StandardOpenOption option =
               !Files.exists(path) ? StandardOpenOption.CREATE : StandardOpenOption.APPEND;
            Files.write(path, java.util.Arrays.asList(message), option);
        } catch (IOException e) {
            System.err.println(message);
        }
    }
});

Leave a Comment