All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.jsch.JSchSlf4jLogger Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.jsch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Custom logger implementing Jsch's {@link com.jcraft.jsch.Logger} class.
 * 

* Some implementation notes: *

    *
  • JSch's INFO level logging is very verbose, so we are treating it like DEBUG level
  • *
  • Jsch has both ERROR and FATAL levels, but SLF4J only has ERROR, so we treat both as ERROR in SLF4J
  • *
*/ public class JSchSlf4jLogger implements com.jcraft.jsch.Logger { private final Logger slf4jLogger; /** * Construct an instance with a default logger, e.g. for {@code org.kiwiproject.jsch.JSchSlf4jLogger}. *

* Usually you should use {@link #JSchSlf4jLogger(Logger)} in order to specify the SLF4J {@link Logger} explicitly * to control the logging level and logger name. */ public JSchSlf4jLogger() { this.slf4jLogger = LoggerFactory.getLogger(JSchSlf4jLogger.class); } /** * Construct an instance using the given SLF4J logger. * * @param slf4jLogger the SLF4J {@link Logger} that will be the destination for JSch logging output. */ public JSchSlf4jLogger(Logger slf4jLogger) { this.slf4jLogger = slf4jLogger; } /** * Is the given level enabled? * * @param level one of the public constants in {@link com.jcraft.jsch.Logger} * @return true if the level is enabled in the underlying SLF4J logger. */ @Override public boolean isEnabled(int level) { switch (level) { case DEBUG: case INFO: return slf4jLogger.isDebugEnabled(); case WARN: return slf4jLogger.isWarnEnabled(); case ERROR: case FATAL: return slf4jLogger.isErrorEnabled(); default: slf4jLogger.error("Was passed invalid level: {}", level); return false; } } /** * Log the given message at the given level. *

* If provided an invalid level, a message will be logged about the invalid level, along with the given message, * at the SLF4J ERROR level. This is intended to provide both information about the problem as well as provide * the original message, rather than throwing an exception or suppressing the message. * * @param level one of the public constants in {@link com.jcraft.jsch.Logger} * @param message the message to log */ @Override public void log(int level, String message) { // Probably don't need to check this again, but it is not clear since there is no documentation // on proper usage in JSch. So just being safe and checking again. if (isValidLevel(level) && isNotEnabled(level)) { return; } switch (level) { case DEBUG: case INFO: slf4jLogger.debug(message); break; case WARN: slf4jLogger.warn(message); break; case ERROR: case FATAL: slf4jLogger.error(message); break; default: slf4jLogger.error("Was passed invalid level: {}. (Message the caller wanted to log: {})", level, message); break; } } private boolean isValidLevel(int level) { switch (level) { case DEBUG: case INFO: case WARN: case ERROR: case FATAL: return true; default: return false; } } private boolean isNotEnabled(int level) { return !isEnabled(level); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy