简体   繁体   中英

Easily disable log4j for applet?

I am building Java Desktop and Java Applet from the same code. Log4j is in use so almost every class in desktop version (which is original) has line like

private static final Logger LOG = LoggerFactory.getLogger(EachClassName.class);

Because Applet is not supposed to perform any io operations I had to exclude this configuration code:

InputStream is = Log4jConfigurator.class.getClassLoader().getResourceAsStream(path);
properties.load(is);
is.close();
PropertyConfigurator.configure(properties);

But now I get warnings because getLogger is still in place

log4j:WARN No appenders could be found for logger (org.game.client.Client).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

What would be the most elegant solution to avoid this warnings and use logger only in desktop version?

In your "Applet App" try to configure log4j programmatically:

  1. turn off logging

    LogManager.getRootLogger().setLevel(Level.OFF);

  2. Add NullAppender (if setting Level.OFF is not enough)

    LogManager.getRootLogger().addAppender(new NullAppender())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM