简体   繁体   English

logger(java.util.logging)尽管在文件中打印输出,但突然停止将输出打印到控制台中

[英]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); 我正在使用java.util.logging来设置记录器,下面是代码: 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"); 记录器可以很好地与控制台和文件配合使用,但是在下面的JSch代码段`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 JSch channel.disconnect中所述, 防止打印日志

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. 否则,关闭通道时将关闭System.err ,这将在退出时发生。 Closing System.err will prevent you from seeing output on the console. 关闭System.err将阻止您在控制台上看到输出。

Another option would be to wrap System.err in a stream that ignores calls to close. 另一种选择是将System.err包装在忽略调用close的流中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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