简体   繁体   中英

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

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); 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM