如何在使用 Java 日志 API 时禁用默认的控制台处理程序?

嗨,我正在尝试在我的应用程序中实现 Java 日志。我想使用两个处理程序。 一个文件处理程序和我自己的控制台处理程序。我的两个管理员都工作得很好。我的日志被发送到一个文件和控制台。 我的日志也被发送到默认的控制台处理程序,这是我不想要的。如果你运行我的代码,你会看到额外的两行发送到控制台。我不想使用默认的控制台处理程序。有人知道如何禁用默认的控制台处理程序吗。 我只想使用我创建的两个处理程序。

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. ??");
}
}
107517 次浏览

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.

This is strange but Logger.getLogger("global") does not work in my setup (as well as Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)).

However Logger.getLogger("") does the job well.

Hope this info also helps somebody...

Just do

LogManager.getLogManager().reset();

Do a reset of the configuration and set the root level to OFF

LogManager.getLogManager().reset();
Logger globalLogger = Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME);
globalLogger.setLevel(java.util.logging.Level.OFF);

You must instruct your logger not to send its messages on up to its parent logger:

...
import java.util.logging.*;
...
Logger logger = Logger.getLogger(this.getClass().getName());
logger.setUseParentHandlers(false);
...

However, this should be done before adding any more handlers to logger.