javasshj库并启用日志记录

xpszyzbs  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(461)

下面是我的Java1.8程序的一个示例部分。尝试验证时似乎失败。它经历了许多身份验证方法,然后声明其耗尽。我想查看sshj库中的调试信息,以帮助我确定失败的地方:-用户名、密钥交换或其他。我对log4j很熟悉,我可以在代码中放日志语句,但是我找不到一个示例(简单易懂)来演示如何将log4j连接到sfl4j,然后告诉sshj使用日志。
'''

SSHClient sshClient = new SSHClient();

        try
        {

            String username = "testuser";
            File privateKey = new File("/mykeys/keyname");
            KeyProvider keys;

            sshClient.addHostKeyVerifier(new PromiscuousVerifier());

            keys = sshClient.loadKeys(privateKey.getPath());

            sshClient.connect("1.2.3.4", 22);

            sshClient.authPublickey(username, keys);

            SFTPClient sftpClient = sshClient.newSFTPClient();

            sftpClient.put("./send/file1.xml", "file1.xml");

            sshClient.close();
        }

        catch (UserAuthException e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }
        catch (TransportException e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }

'''

kgqe7b3p

kgqe7b3p1#

添加

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.6</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.6</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>

到pom.xml
创建log4j.proerties文件对我来说是个窍门:


# Define the root logger with appender file

log = ssh-test.log
log4j.rootLogger = DEBUG, FILE  

# Define the file appender

log4j.appender.FILE=org.apache.log4j.FileAppender  
log4j.appender.FILE.File=${log}/log.out  

# Define the layout for file appender

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout  
log4j.appender.FILE.layout.conversionPattern=%m%n

相关问题