JSch 登录文件 [英] JSch Logs in files

查看:21
本文介绍了JSch 登录文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 JSch 日志保存在文件中,因为它在控制台中没有显示任何内容.

I want to save JSch logs in file because it does not show anything in the console.

这是我的代码:

public boolean openConnection() throws ItsSshException {
    boolean connectSuccess = false;

    JSch.setLogger(new MyLogger());

    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    jschSSH.setConfig(config);
    try {
        sshSession = jschSSH.getSession(username, hostname, port);
        sshSession.setPassword(password);
        sshSession.connect(connectionTimeout);
        LOGGER.info("Connection timeout : " + connectionTimeout);
        Thread.sleep(1000);
        sshHChannel = sshSession.openChannel("shell");
        sshHChannel.connect();
        in = sshHChannel.getInputStream();
        out = new PrintStream(sshHChannel.getOutputStream());
        clearInitialSocketState();
        connectSuccess = true;
    } catch (Exception e) {
        LOGGER.error("Error during connectiong to host: " + hostname +
                     ", port: " + port + "!", e);
        throw new ItsSshException("Error during connectiong to host: " + e.getMessage());
    }
    LOGGER.info("connectSuccess : " + connectSuccess);
    return connectSuccess;
}

public static class MyLogger implements com.jcraft.jsch.Logger {
    static java.util.Hashtable name=new java.util.Hashtable();
    static{
        name.put(new Integer(DEBUG), "DEBUG: ");
        name.put(new Integer(INFO), "INFO: ");
        name.put(new Integer(WARN), "WARN: ");
        name.put(new Integer(ERROR), "ERROR: ");
        name.put(new Integer(FATAL), "FATAL: ");
    }
    public boolean isEnabled(int level){
        return true;
    }
    public void log(int level, String message){
        System.err.print(name.get(new Integer(level)));
        System.err.println(message);
    }
}

在哪里放置 jsch 记录器以获取文件中的一些信息.我尝试过但从未成功 :D

Where to put the jsch logger to get some informations in file. I have tried but never successed :D

推荐答案

使用 Logger.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);
    }
}

要将记录器与 JSch 相关联,请使用:

To associate the logger with JSch use:

JSch.setLogger(new MyJSchLogger(logger));

假设 Java 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);
        }
    }
});

这篇关于JSch 登录文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆