2017-11-21 7 views
3

콘솔에 아무 것도 표시하지 않기 때문에 파일에 JSch 로그를 저장하려고합니다. 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); 
    } 
} 

. 나는 시도했지만 successed 적이있다 : D

답변

2

사용 Logger.logMyLogger.log에 : JSch의 사용과 로거를 연결하려면

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); 
    } 
} 

:

public void log(int level, String message){ 
    LOGGER.log(loggerlevel, message); 
} 

는 전체 코드가 같이 할 수있다 :

JSch.setLogger(new MyJSchLogger(logger)); 

Java를 가정하면 logger이 존재합니다. 그렇지 않으면

, 당신은 같은 하나 만들 수 있습니다 : 당신은 그냥 파일에 기록해야하는 경우

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); 

비록을, 당신은 직접 작업을 수행 할 수 있습니다

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); 
     } 
    } 
});