0

I have an Java app that depends on a whole bunch of libraries. I use the assembly maven plugin to package the whole thing into a single JAR.

This library is mostly used a regular JAR dependency, so the client will be controlling logging. However, when it's packaged as a standalone executable JAR, I don't want to show all these logs that you would usually see in the server log.

I obviously don't control what type of logging libraries dependencies use. In my main, I use the JDK java.util.logging.

How do I programmatically turn off all logging (or leave it only at SEVERE level)?

I have tried to do what is suggested here (with the variation that I set the level to SEVERE) at the very start in my main, but all the logs are still showing up.

How do I get the logs to stop showing up on the command-line when I execute the JAR?

user1902183
  • 3,203
  • 9
  • 31
  • 48
  • Did you try including a [production logging.properties file to disable logging](https://stackoverflow.com/questions/6307648/change-global-setting-for-logger-instances/6307666#6307666)? – jmehrens Sep 17 '19 at 03:37
  • Including logging.properties is tricky as I don't want to turn off logging when it's packaged as a regular JAR (not the executable with dependencies). – user1902183 Sep 17 '19 at 19:04

1 Answers1

0

How do I get the logs to stop showing up on the command-line when I execute the JAR?

If you want to be heavy handed you can stop all console output by simply closing the out and error streams:

System.out.close();
System.err.close();

Keep in mind there is no way to re-open those streams.

How do I programmatically turn off all logging (or leave it only at SEVERE level)?

For JUL you can follow the instructions to create a logging.properties file.

I obviously don't control what type of logging libraries dependencies use...

You'll have to have to include configuration files for those frameworks to turn them off.

How do I programmatically turn off all logging (or leave it only at SEVERE level)?

Child loggers will inherit from the root logger. You can set the root logger to severe and set it's handlers to severe:

private static final Logger[] PINS = new Logger[]{Logger.getLogger("")};
static {
   for (Logger l : PINS) {
      l.setLevel(Level.SEVERE);
      for (Handler h : l.getHanders()) {
          h.setLevel(Level.SEVERE);
      }
   }
}
jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • Other than 'heavy handed' approach, which isn't going to work for me since I need to output stuff to the command-line, including logging.properties is tricky as I don't want to turn off logging when it's packaged as a regular JAR (not the executable with dependencies). Also, this isn't programmatic. – user1902183 Sep 17 '19 at 19:07
  • @user1902183 You can try setting the root logger to SEVERE and the assigned handlers to severe. – jmehrens Sep 17 '19 at 20:47
  • Tried it. It's still outputting all the logs from the dependencies. I think I read somewhere that Loggers are immutable once they are instantiated, so I am not sure I can affect them like this. Not sure if this is the issue for why this isn't working. Maybe it's because of multiple logging libraries in the dependencies that are not affected by any of this? Not sure. – user1902183 Sep 18 '19 at 01:45
  • @user1902183 You can try matching the output format to a framework. You could also try running [JDeps](https://docs.oracle.com/javase/8/docs/technotes/tools/windows/jdeps.html) on your libraries to determine the logging frameworks in use. – jmehrens Sep 18 '19 at 15:17