logger(java.util.logging) suddenly stops printing the output into the console though it printing in the file

738 views Asked by At

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.

1

There are 1 answers

0
jmehrens On

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.