JSch logger - where can I set the level

How to adjust JSch logger level?

How is Log4J configured through XML?

+6
source share
2 answers

JSch does not seem to use any known logging structure (I use JSch v0.1.49, but latest version 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()); 

Please note that instead of MyLevel and MyLogger you can use any classes of the log framework (Log4j, Logback, ...)

Here you can get the full example: http://www.jcraft.com/jsch/examples/Logger.java.html

+11
source

I just wanted to add a small comment to the accepted answer, but the reputation does not allow. Sorry if this path through another answer is evil, but really want to mention the following.

Activating the log works this way, and you can get a lot of information about the connection process (key exchange, etc.). But after authentication, there is practically no such thing as debug output for basic functionality, at least for SFTP. And look at the source showing / confirming that there is no registration in ChannelSftp (and other classes).

So, if you want to activate this in order to check communication problems (after authentication) that were wasted, or you need to add the appropriate statements to the source yourself (I have not done this yet).

We are faced with complete freezes (worker threads get stuck for days / endlessly) in put, get and even ls - and, of course, the server provider claims that this is not a problem (and the unix sftp commandline-client really works, but not from the host appserver, to which we do not have access ... so we will need to check the network communication). If anyone has an idea, thanks ..

+7
source

Source: https://habr.com/ru/post/977937/


All Articles