如何在使用java logging API时禁用默认控制台处理程序? [英] How can I disable the default console handler, while using the java logging API?

查看:77
本文介绍了如何在使用java logging API时禁用默认控制台处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试在我的应用程序中实现java日志记录。我想使用两个处理程序。
文件处理程序和我自己的控制台处理程序。我的两个处理程序都运行良好。我的日志记录将发送到文件和控制台。
我的日志记录也被发送到我不想要的默认控制台处理程序。如果您运行我的代码,您将看到发送到控制台的额外两行。我不想使用默认的控制台处理程序。有谁知道如何禁用默认控制台处理程序。
我只想使用我创建的两个处理程序。

Hi I am trying to implement the java logging in my application. I want to use two handlers. A file handler and my own console handler. Both of my handlers work fine. My logging is send to a file and to the console . My logging is also sent to the default console handler, which i do not want. If you run my code you will see extra two line sent to the console. I don't want to use the default console handler. Does anyone know how to disable the default console handler. I only want to use the two handlers I have created.

Handler fh = new FileHandler("test.txt");
fh.setFormatter(formatter);
logger.addHandler(fh);







Handler ch = new ConsoleHandler();
ch.setFormatter(formatter);
logger.addHandler(ch);







import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class LoggingExample {
    private static Logger logger = Logger.getLogger("test");

    static {
        try {
            logger.setLevel(Level.INFO);

            Formatter formatter = new Formatter() {

                @Override
                public String format(LogRecord arg0) {
                    StringBuilder b = new StringBuilder();
                    b.append(new Date());
                    b.append(" ");
                    b.append(arg0.getSourceClassName());
                    b.append(" ");
                    b.append(arg0.getSourceMethodName());
                    b.append(" ");
                    b.append(arg0.getLevel());
                    b.append(" ");
                    b.append(arg0.getMessage());
                    b.append(System.getProperty("line.separator"));
                    return b.toString();
                }

            };

            Handler fh = new FileHandler("test.txt");
            fh.setFormatter(formatter);
            logger.addHandler(fh);

            Handler ch = new ConsoleHandler();
            ch.setFormatter(formatter);
            logger.addHandler(ch);

            LogManager lm = LogManager.getLogManager();
            lm.addLogger(logger);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        logger.info("why does my test application use the standard console logger ?\n" + " I want only my console handler (Handler ch)\n " + "how can i turn the standard logger to the console off. ??");
    }
}


推荐答案

默认控制台处理程序附加到根记录程序,它是包括您的所有其他记录程序的父记录程序。所以我看到两种方法来解决你的问题:

The default console handler is attached to the root logger, which is a parent of all other loggers including yours. So I see two ways to solve your problem:

如果这只影响你的特定类,最简单的解决方案是禁用将日志传递给父类记录器:

If this is only affects this particular class of yours, the simplest solution would be to disable passing the logs up to the parent logger:

logger.setUseParentHandlers(false);

如果要为整个应用程序更改此行为,可以从中删除默认控制台处理程序在添加自己的处理程序之前完全根记录器:

If you want to change this behaviour for your whole app, you could remove the default console handler from the root logger altogether before adding your own handlers:

Logger globalLogger = Logger.getLogger("global");
Handler[] handlers = globalLogger.getHandlers();
for(Handler handler : handlers) {
    globalLogger.removeHandler(handler);
}

注意:如果你想在其他类中使用相同的日志处理程序,最好的方法是从长远来看将日志配置移动到配置文件中。

Note: if you want to use the same log handlers in other classes too, the best way is to move the log configuration into a config file in the long run.

这篇关于如何在使用java logging API时禁用默认控制台处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆