java JSch 记录器 - 我在哪里可以配置级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26826894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSch logger - where can I configure the level
提问by Alexey
How can I configure the level of JSch logger?
如何配置 JSch 记录器的级别?
Is it like Log4J configurable via XML?
是否像通过 XML 配置的 Log4J?
回答by xav
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:
JSch 似乎没有使用任何已知的日志框架(我使用 JSch v0.1.49,但最后一个版本是 v0.1.51)或任何 XML 配置文件。所以这就是我所做的:
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 之前:
JSch.setLogger(new JSCHLogger());
Note that instead of MyLevel
and MyLogger
, you can use any logging framework classes you want (Log4j, Logback, ...)
请注意,除了MyLevel
and 之外MyLogger
,您还可以使用任何您想要的日志框架类(Log4j、Logback 等)
You can get a complete example here: http://www.jcraft.com/jsch/examples/Logger.java.html
你可以在这里得到一个完整的例子:http: //www.jcraft.com/jsch/examples/Logger.java.html
回答by hyphan
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).
日志激活以这种方式工作,它可以为您提供有关连接过程(密钥交换等)的大量信息。但实际上,在身份验证后,核心功能的调试输出实际上没有这样的东西,至少对于 SFTP。查看源代码显示/确认 ChannelSftp(以及大多数其他类)中没有日志记录。
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..
我们在 put、get 甚至 ls 中遇到了完全挂起(作业线程卡住了几天/无限)——当然,服务器提供商声称这不是问题(事实上,unix sftp 命令行客户端可以工作——但不是来自 appserver主机,我们无权访问......所以我们必须检查网络通信)。如果有人有想法,谢谢..