Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSch logger - where can I configure the level

How can I configure the level of JSch logger?

Is it like Log4J configurable via XML?

like image 954
Alexey Avatar asked Nov 09 '14 09:11

Alexey


2 Answers

JSch doesn't seem to use any known logging framework (I use JSch v0.1.49, but the last version is v0.1.51), or any XML configuration file. So here is what I did:

private class JSCHLogger implements com.jcraft.jsch.Logger {
    private Map<Integer, MyLevel> levels = new HashMap<Integer, MyLevel>();

    private final MyLogger LOGGER;


    public JSCHLogger() {
        // Mapping between JSch levels and our own levels
        levels.put(DEBUG, MyLevel.FINE);
        levels.put(INFO, MyLevel.INFO);
        levels.put(WARN, MyLevel.WARNING);
        levels.put(ERROR, MyLevel.SEVERE);
        levels.put(FATAL, MyLevel.SEVERE);

        LOGGER = MyLogger.getLogger(...); // Anything you want here, depending on your logging framework
    }

    @Override
    public boolean isEnabled(int pLevel) {
        return true; // here, all levels enabled 
    }

    @Override
    public void log(int pLevel, String pMessage) {
        MyLevel level = levels.get(pLevel);
        if (level == null) {
            level = MyLevel.SEVERE;
        }
        LOGGER.log(level, pMessage); // logging-framework dependent...
    }
}

Then before using JSch:

JSch.setLogger(new JSCHLogger());

Note that instead of MyLevel and MyLogger, you can use any logging framework classes you want (Log4j, Logback, ...)

You can get a complete example here: http://www.jcraft.com/jsch/examples/Logger.java.html

like image 99
xav Avatar answered Oct 15 '22 08:10

xav


Just wanted to add a small comment to the accepted answer, but reputation doesnt allow. Sorry if this way via another answer is evil, but really want to mention the following.

The log activation works this way, and it can get you a lot of info about the connection process (key exchange and such). But there is practically no such thing as debug output for the core functionality after authentication, at least for SFTP. And a look at the source shows / confirms there is no logging in ChannelSftp (and the most other classes).

So if you want to activate this in order to inspect communication problems (after authentication) thats wasted - or you need to add suitable statements to the source yourself (I did not yet).

We encounter complete hangs (job threads get stuck for days/infinite) in put, get and even ls - and of course the server provider claims not to be the problem (and indeed the unix sftp commandline-client works - but not from the appserver host, which we have no access to.. so we would have to check network communication). If someone has an idea, Thanks..

like image 36
hyphan Avatar answered Oct 15 '22 07:10

hyphan