0

Looking for log4j.appender.console.target equivalent in java.util.logging framework in config properties file (not using code).

I have more than a thousand Java classes. Almost all of them have their own Logger instance something like:

import java.util.logging.Logger;
Logger logger = Logger.getLogger(MyClass.class.getName());

My problem here is, logger prints all the log messages to System.err stream. But, I want it to be printed on System.out.

I know that I can add handler like below to use a different stream.

logger.addHandler(new StreamHandler(System.out, new SimpleFormatter()))

But I have quite a few Java classes to be changed. which I don't want to do.

How can I make java.uitl.logging framework to print log messages to System.out stream for all the classes?

I'm looking for a configuration setting in properties file which is same for all the classes.

Like in case of log4j, we have log4j.properties file where I can set the log4j.appender.console.target=System.out, which tells all the log4j loggers to print log messages to System.out. I want same thing to be done with java.util.logging framework as well.

gom
  • 23
  • 5
  • Simple java util logger logs to console, no customization is required. Write a simple java program and check. – Sambit Jun 08 '19 at 06:57
  • @Sambit, Thanks for your quick response. I did check with sample Java code.. java util logger logs to console in RED.. which means, it is printed on standard error stream (i.e., ```System.err```). – gom Jun 08 '19 at 09:46

1 Answers1

0

How can I make java.uitl.logging framework to print log messages to System.out stream for all the classes?

There is no ConsoleHandler target equivalent in java.util.logging. The topic of directing ConsoleHandler output to System.out is cover in another SO question. Most of your options are enumerated in that question.

The ConsoleHandler will grab a snapshot of System.err during construction. One option would be to swap the global error stream with the global out stream. After all of the ConsoleHandler objects are constructed you can swap the global streams back to the correct location.

jmehrens
  • 10,580
  • 1
  • 38
  • 47