javasshj

Need information related to sshj library. How logging works?


I'm trying to transfer a file from one server to other server using sftp protocol. So, I'm trying to use sshj library for this. Now before transferring the file I'm performing several activities such as zipping and after transferring unzipping . So, I'm logging this details to successLog and if ay error to FailureLog.

here is my log4j properties:-

# Define the root logger
log4j.rootLogger = DEBUG, toConsole

# Define the console appender
log4j.appender.toConsole=org.apache.log4j.ConsoleAppender
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.toConsoleAppender.layout.ConversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n

# Define the file appender
log4j.appender.success=org.apache.log4j.FileAppender
log4j.appender.success.File=logs/success.log
log4j.appender.success.Append=false
log4j.appender.success.layout=org.apache.log4j.PatternLayout
log4j.appender.success.layout.conversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n

# Define the file appender
log4j.appender.failure=org.apache.log4j.FileAppender
log4j.appender.failure.File=logs/failure.log
log4j.appender.failure.Append=false
log4j.appender.failure.layout=org.apache.log4j.PatternLayout
log4j.appender.failure.layout.conversionPattern=%d{HH:mm:ss} %5p [%t] - %c.%M - %m%n

log4j.category.successLogger=DEBUG, success
log4j.additivity.successLogger=true

log4j.category.failureLogger=WARN, failure
log4j.additivity.failureLogger=false

and in java code I'm using this as :-

static final Logger successLog = Logger.getLogger("successLogger");
    static final Logger failureLog = Logger.getLogger("failureLogger");

and example failureLog.error("exception occured due to so an so reason",e);

now I would like to store sshj library log in some file called sftp.log. How can I do that??so far I was manually logging into success and failure logs but sshj log is by default printing on console which I want to write it to some file.


Solution

  • Here's my example of writing the SFTP-session FINE-level log to file:

    private void enableFineLogging() {
        try {
            fileHandler = new FileHandler(
                    "./logs/fine_sshj.log",
                    10000000, 1000, true);
            fileHandler.setLevel(Level.FINER);
            fileHandler.setFormatter(new SimpleFormatter());
    
            final Logger app = Logger.getLogger("net.schmizz");
            app.setLevel(Level.FINER);
            app.addHandler(fileHandler);
            app.setUseParentHandlers(false);
            app.info(
                    "########################################################################   "
                            + "START LOGGING NEW SSHJ SESSION   "
                            + "########################################################################");
    
        } catch (Exception e) {
            // Do something...
        }
    }
    

    Though I'm not sure if are able to separate the log levels so that successLogger would have only the success cases... I think there will be also WARNING and SEVERE level messages.