spring - log4j2 configuration from file -


i working on existing system uses log4j, want update log4j2.

there custom spring bean loads configuration file. need keep approach. cannot use "log4j.configurationfile" system property.

we have properties file path current log4j.xml specified ( nfs share )

the spring bean has code ...

public class log4jconfigurationbean implements resourceloaderaware,     initializingbean {  private resourceloader resourceloader;  private boolean enabled;  private string location;  /**  * default, no argument constructor.  */ public log4jconfigurationbean() {     enabled = true; }  /**  * sets whether or not bean should load external configuration  * defined {@link #setlocation(resource)}. if <code>false</code>,  * bean nothing.  *   * <p>  * default value <code>true</code>.  * </p>  *   * @param enabled  *            <code>false</code> causes bean nothing  */ public void setenabled(final boolean enabled) {     this.enabled = enabled; }  /**  * sets location of external log4j configuration (xml or properties)  * loaded.  *   * @param location  *            location of external configuration loaded.  * @throws illegalstateexception  *             if there problem resolving location resource  * @throws nullpointerexception  *             if <code>resource</code> <code>null</code>  */ public void setlocation(final string location) {     this.location = stringutils.trimtonull(location); }  @override public void setresourceloader(final resourceloader resourceloader) {     this.resourceloader = resourceloader; }  /**  * @throws illegalstateexception  *             if enabled , no location has set, or if external  *             configuration neither xml or properties.  */ @override public void afterpropertiesset() throws exception {     url configurl = null;     if (null != location) {         try {             final resource resource = resourceloader.getresource(location);             if (null != resource) {                 configurl = resource.geturl();             }         } catch (ioexception e) {             throw new illegalargumentexception(                     "could not resolve configuration location due error: ",                     e);         }     }     if (enabled && null == configurl) {         throw new illegalstateexception(                 "log4j configuration enabled, configuration location not set.");     }     if (enabled) {         if (configurl.getfile().tolowercase().endswith(".xml")) {             domconfigurator.configure(configurl);         } else if (configurl.getfile().tolowercase()                 .endswith(".properties")) {             propertyconfigurator.configure(configurl);         } else {             throw new illegalstateexception(                     "configuration must properties or xml: "                             + configurl.getfile());         }     } } 

}

in log4j2 there no propertyconfigurator. how can load log4j2.xml file same way.

the file path log4j2.xml file specified in spring property file.

the goal have war files contain log4j2.xml file in classpath. used when developing on local box.

when web apps deployed qa environment, there property file containing following key/value pair...

# should external file used log4j configuration log4j.enabled=true log4j.location=file:/paht log4j2.xml 

a spring bean using these values decide if external log4j2.xml file should used instead of 1 on classpath.

i tried spring bean this... code executed, still uses configuration file on classpath.

public class log4j2configurationbean implements resourceloaderaware, initializingbean { private static final logger log = loggerfactory.getlogger(log4j2configurationbean.class); private resourceloader resourceloader; private boolean enabled; private string location;  /**  * default, no argument constructor.  */ public log4j2configurationbean() {     enabled = true; }  /**  * sets whether or not bean should load external configuration defined {@link #setlocation(resource)}. if <code>false</code>, bean nothing.  *   * <p>  * default value <code>true</code>.  * </p>  *   * @param enabled  *            <code>false</code> causes bean nothing  */ public void setenabled(final boolean enabled) {     this.enabled = enabled; }  /**  * sets location of external log4j configuration (xml or properties) loaded.  *   * @param location  *            location of external configuration loaded.  * @throws illegalstateexception  *             if there problem resolving location resource  * @throws nullpointerexception  *             if <code>resource</code> <code>null</code>  */ public void setlocation(final string location) {     this.location = stringutils.trimtonull(location); }  @override public void setresourceloader(final resourceloader resourceloader) {     this.resourceloader = resourceloader; }  /**  * @throws illegalstateexception  *             if enabled , no location has set, or if external configuration neither xml or properties.  */ @override public void afterpropertiesset() throws exception {     url configurl = null;     if (enabled) {         if (stringutils.isblank(location)) {             throw new illegalstateexception("log4j2 configuration enabled, configuration location not set.");         }         try {             system.out.println(this.getclass().getname() + " : loading log4j2 configuration " + location);             final resource resource = resourceloader.getresource(location);             if (null != resource) {                 configurl = resource.geturl();             }         } catch (ioexception e) {             throw new illegalargumentexception("could not resolve configuration location due error: ", e);         }         if (configurl.getfile().tolowercase().endswith(".xml")) {             try {                 system.setproperty("log4jcontextselector", "org.apache.logging.log4j.core.async.asyncloggercontextselector");                 system.setproperty("asynclogger.ringbuffersize", "8192");                 configurationfactory configurationfactory = xmlconfigurationfactory.getinstance();                 configurationsource configurationsource = new configurationsource(configurl.openstream(), configurl);                 configuration configuration = configurationfactory.getconfiguration(configurationsource);                 configuration.start();                 log.info("log4j2 configured {}", location);                 log.info("system property log4jcontextselector set {}", system.getproperty("log4jcontextselector"));                 log.info("system property asynclogger.ringbuffersize set {}", system.getproperty("asynclogger.ringbuffersize"));             } catch (exception e) {                 system.out.println(this.getclass().getname() + " : not initialize log4j2 resource " + location);                 system.out.println(e.getstacktrace());             }         } else {             throw new illegalstateexception("configuration must xml: " + configurl.getfile());         }     } else {         system.out.println(this.getclass().getname() + " : external log4j2 configuration not configured.");     } } 

}

thanks.

check out how configure log4j2 in code without configuration file? section here - http://logging.apache.org/log4j/2.x/faq.html


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -