java - How to load log4j2 configuration file from JNDI -
i migrating web applications log4j 1.12 log4j2. due company policies our log4j.xml file locations configured urls in application server , applications have them jndi. implemented servletcontextlistener
allowed initialize log4j infraestructure way:
public void contextinitialized(servletcontextevent servletcontextevent) { ... urllogconfig = (url) context.lookup("java:comp/env/"+loglocation); ... //urllogconfig -> file:///somepath/log4j.xml domconfigurator.configureandwatch(urllogconfig.getpath()); } public void contextdestroyed(servletcontextevent servletcontextevent) { servletcontext servletcontext = servletcontextevent.getservletcontext(); servletcontext.log("log4jconfigurationlistener - shutting down log4j"); logmanager.shutdown(); }
however, log4j2 api changes can no longer used. log4j2 provides lo4j2-web.jar module uses log4jservletcontainerinitializer
initialize library. ends calling log4jwebinitializerimpl.getconfiguri()
, responsible of getting uri of config file.
is there way customize log4jwebinitializerimpl's behaviour make resolve xml file jndi name?
how like:
... import org.apache.logging.log4j.core.config.configurator; ... private loggercontext loggercontext; public void contextinitialized(servletcontextevent servletcontextevent) { ... urllogconfig = (url) context.lookup("java:comp/env/"+loglocation); ... //urllogconfig -> file:///somepath/log4j.xml servletcontext servletcontext = servletcontextevent.getservletcontext(); string contextname = servletcontext.getservletcontextname(); classloader classloader = this.getclass().getclassloader(); loggercontext = configurator.initialize(contextname, classloader, urllogconfig.touri()); } public void contextdestroyed(servletcontextevent servletcontextevent) { servletcontext servletcontext = servletcontextevent.getservletcontext(); servletcontext.log("log4jconfigurationlistener - shutting down log4j"); if (loggercontext != null) configurator.shutdown(loggercontext); }
Comments
Post a Comment