Disabling awt/swing debug (fine) log messages
Asked Answered
J

5

9

I'm using java logging to write log messages of my application to a log file and other destinations. Having set the log level to FINE, I also get (unwanted) messages from AWT/Swing, such as:

{0}, when grabbed {1}, contains {2}

and others. Looking at the JDK source code (see e.g. here), one sees that the name of the corresponding logger is sun.awt.X11.grab.XWindowPeer.

What I understood from the Java logging framework is that this logging handler should inherit its loglevel from its parents like sun.awt.

I tried the following:

Logger.getLogger("sun.awt").setLevel(Level.OFF);

but the AWT/Swing debug messages still appear in the log output.

What's the recommended way of programmatically disabling these log messages (while still allowing FINE messages from other sources) ?

Jaworski answered 17/5, 2011 at 10:24 Comment(2)
Please update your question to show how you are obtaining your logger instance and how you are calling it.Ufo
@AndreHolzner Did you find what you want? Would you please share?Telescopic
N
5

If you just want to log the messages of your own application, you can disable all messages and then explicitly enable messages for your application:

Logger.getRootLogger().setLevel(Level.OFF);
Logger.getLogger("package.of.your.application").setLevel(Level.ALL);

Inside the properties-file for logging (e.g. logging.properties) this would be:

.level = OFF
package.of.your.application.level = ALL
Neutrino answered 30/5, 2012 at 18:4 Comment(0)
W
2

I could not find the getRootLogger() method in the Logger class any more. This is what works for me:

logger = Logger.getLogger("my.package");
Logger l = logger;
while (l != null) {
    l.setLevel(Level.OFF);
    l = l.getParent();
}            
logger.setLevel(Level.FINER);
Whomsoever answered 31/1, 2014 at 15:20 Comment(0)
N
1

I had the same problem today. Searching on google this is the top url and I don't find a good source to a answer, so I will post mine :)

Assuming that Andre use the java.util.logging API, it's possible to add a Handler that will control the format of your log, using setFormatter(Formatter newFormatter).

So I extended Formatter and checked if the class of the log contains java.awt, javax.swing or sun.awt.

class MyFormatLog extends Formatter {

    @Override
    public String format(LogRecord record) {
        if( !record.getSourceClassName().contains("java.awt") &&
        !record.getSourceClassName().contains("javax.swing.") &&
        !record.getSourceClassName().contains("sun.awt.") ) {
          //format my output...
        } else {
          return "";
        }
    }

}
Nath answered 10/2, 2012 at 13:52 Comment(0)
F
1
    Logger.getLogger("java.awt").setLevel(Level.OFF);
    Logger.getLogger("sun.awt").setLevel(Level.OFF);
    Logger.getLogger("javax.swing").setLevel(Level.OFF);
Fabrikoid answered 28/8, 2014 at 9:25 Comment(0)
D
0

Try Logger.getRootLogger().setLevel(Level.OFF);

Drunken answered 30/5, 2011 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.