0

I'm using java.util.logging to set up the logger and here's the code : fh = new FileHandler(file.getAbsolutePath()); logger.addHandler(fh); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); logger.setLevel(Level.ALL); The logger works fine with console and file .but after the below JSch code snippet `Channel channel = session.openChannel("exec");

    ((ChannelExec) channel).setCommand(cmd1);

    channel.setInputStream(null);
    ((ChannelExec) channel).setErrStream(System.err);

    logger.info("creating tar ,executing command :"+cmd1);

    channel.connect();

    while (true) {
        if (channel.isClosed()) {
            System.out.println( logger.getHandlers());
            // doesn't log afterwards
             logger.info("exit-status: " + channel.getExitStatus());
            if (channel.getExitStatus() == 0)
                logger.info("tar file with " + tarFileName
                        + "has been created successfully at " + path);
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
        }
    }`

I guess the handler has changed when I'm executing the command.How can I reset the handler such that it starts printing into the console as well.

Skull
  • 11
  • 4
  • Have you declared all of your loggers as [`private static final` so they are not garbage collected?](http://stackoverflow.com/questions/22689831/log-file-not-being-updated-after-few-seconds-while-using-logger-and-filehandler) – jmehrens Dec 23 '16 at 20:08
  • yes , The logger is private static and is defined as : – Skull Dec 26 '16 at 11:23
  • private static Logger logger; – Skull Dec 26 '16 at 11:25
  • Possible duplicate of [JSch channel.disconnect prevent logs from printing](http://stackoverflow.com/questions/20229001/jsch-channel-disconnect-prevent-logs-from-printing) – jmehrens Dec 26 '16 at 15:32

1 Answers1

1

As described in JSch channel.disconnect prevent logs from printing

You have to change:

((ChannelExec) channel).setErrStream(System.err);

to

((ChannelExec) channel).setErrStream(System.err, true);

Otherwise System.err is closed when the channel is closed which will happen on exit. Closing System.err will prevent you from seeing output on the console.

Another option would be to wrap System.err in a stream that ignores calls to close.

Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47