猿问

JSch登录文件

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


这是我的代码:


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


繁花不似锦
浏览 269回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答