CAMEL-9054: sftp - Reduce logging noise from JSCH

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/79e2f135
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/79e2f135
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/79e2f135

Branch: refs/heads/master
Commit: 79e2f1354314b01a780314f0ed56b5dd5f05b422
Parents: 82e9fd3
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Aug 5 13:00:56 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Aug 5 13:00:56 2015 +0200

----------------------------------------------------------------------
 .../java/org/apache/camel/LoggingLevel.java     | 20 +++++++++++++-
 .../file/remote/SftpConfiguration.java          | 16 +++++++++++
 .../component/file/remote/SftpOperations.java   | 29 ++++++++++++--------
 3 files changed, 53 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/79e2f135/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/LoggingLevel.java 
b/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
index a8958bf..434c9dd 100644
--- a/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
+++ b/camel-core/src/main/java/org/apache/camel/LoggingLevel.java
@@ -25,5 +25,23 @@ import javax.xml.bind.annotation.XmlEnum;
  */
 @XmlEnum
 public enum LoggingLevel {
-    DEBUG, ERROR, INFO, TRACE, WARN, OFF
+    DEBUG, ERROR, INFO, TRACE, WARN, OFF;
+
+    /**
+     * Is the given logging level equal or higher than the current level.
+     */
+    public boolean isGE(LoggingLevel level) {
+        if (this == OFF) {
+            return false;
+        } else if (level == WARN) {
+            return this == WARN || this == ERROR;
+        } else if (level == INFO) {
+            return this == INFO || this == WARN || this == ERROR;
+        } else if (level == DEBUG) {
+            return this == DEBUG || this == INFO || this == WARN || this == 
ERROR;
+        } else if (level == TRACE) {
+            return this != OFF;
+        }
+        return false;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/79e2f135/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
index ac24e7c..8d56d5d 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.file.remote;
 import java.net.URI;
 import java.security.KeyPair;
 
+import org.apache.camel.LoggingLevel;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 
@@ -59,6 +60,8 @@ public class SftpConfiguration extends 
RemoteFileConfiguration {
     private int compression;
     @UriParam
     private String preferredAuthentications;
+    @UriParam(defaultValue = "WARN")
+    private LoggingLevel jschLoggingLevel = LoggingLevel.WARN;
 
     public SftpConfiguration() {
         setProtocol("sftp");
@@ -245,4 +248,17 @@ public class SftpConfiguration extends 
RemoteFileConfiguration {
     public String getPreferredAuthentications() {
         return preferredAuthentications;
     }
+
+    public LoggingLevel getJschLoggingLevel() {
+        return jschLoggingLevel;
+    }
+
+    /**
+     * Sets the logging level of JSCH activity.
+     * <p/>
+     * Because JSCH is verbose by default at INFO level, the default value is 
<tt>WARN</tt>
+     */
+    public void setJschLoggingLevel(LoggingLevel jschLoggingLevel) {
+        this.jschLoggingLevel = jschLoggingLevel;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/79e2f135/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
index dab4b8d..752a678 100644
--- 
a/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
+++ 
b/components/camel-ftp/src/main/java/org/apache/camel/component/file/remote/SftpOperations.java
@@ -46,6 +46,7 @@ import com.jcraft.jsch.UserInfo;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.LoggingLevel;
 import org.apache.camel.component.file.FileComponent;
 import org.apache.camel.component.file.GenericFile;
 import org.apache.camel.component.file.GenericFileEndpoint;
@@ -163,7 +164,7 @@ public class SftpOperations implements 
RemoteFileOperations<ChannelSftp.LsEntry>
 
     protected Session createSession(final RemoteFileConfiguration 
configuration) throws JSchException {
         final JSch jsch = new JSch();
-        JSch.setLogger(new JSchLogger());
+        JSch.setLogger(new 
JSchLogger(endpoint.getConfiguration().getJschLoggingLevel()));
 
         SftpConfiguration sftpConfig = (SftpConfiguration) configuration;
 
@@ -332,19 +333,25 @@ public class SftpOperations implements 
RemoteFileOperations<ChannelSftp.LsEntry>
 
     private static final class JSchLogger implements com.jcraft.jsch.Logger {
 
+        private final LoggingLevel loggingLevel;
+
+        private JSchLogger(LoggingLevel loggingLevel) {
+            this.loggingLevel = loggingLevel;
+        }
+
         public boolean isEnabled(int level) {
             switch (level) {
             case FATAL:
                 // use ERROR as FATAL
-                return LOG.isErrorEnabled();
+                return loggingLevel.isGE(LoggingLevel.ERROR) && 
LOG.isErrorEnabled();
             case ERROR:
-                return LOG.isErrorEnabled();
+                return loggingLevel.isGE(LoggingLevel.ERROR) && 
LOG.isErrorEnabled();
             case WARN:
-                return LOG.isWarnEnabled();
+                return loggingLevel.isGE(LoggingLevel.WARN) && 
LOG.isWarnEnabled();
             case INFO:
-                return LOG.isInfoEnabled();
+                return loggingLevel.isGE(LoggingLevel.INFO) && 
LOG.isInfoEnabled();
             default:
-                return LOG.isDebugEnabled();
+                return loggingLevel.isGE(LoggingLevel.DEBUG) && 
LOG.isDebugEnabled();
             }
         }
 
@@ -352,19 +359,19 @@ public class SftpOperations implements 
RemoteFileOperations<ChannelSftp.LsEntry>
             switch (level) {
             case FATAL:
                 // use ERROR as FATAL
-                LOG.error("JSCH -> " + message);
+                LOG.error("JSCH -> {}", message);
                 break;
             case ERROR:
-                LOG.error("JSCH -> " + message);
+                LOG.error("JSCH -> {}", message);
                 break;
             case WARN:
-                LOG.warn("JSCH -> " + message);
+                LOG.warn("JSCH -> {}", message);
                 break;
             case INFO:
-                LOG.info("JSCH -> " + message);
+                LOG.info("JSCH -> {}", message);
                 break;
             default:
-                LOG.debug("JSCH -> " + message);
+                LOG.debug("JSCH -> {}", message);
                 break;
             }
         }

Reply via email to