Log4j2 not getting initialized in EJB contexts

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Log4j2 not getting initialized in EJB contexts

Jayabharatha Reddy
> We are facing an issue with Log4j2 migration, provided more details in
> below.
>
> *Issue*: EJB's are not recognizing log4j2 configurations. Note that we
> don’t have any issue with web services, servlets and normal class logging.
>
> *Environment*: apache-tomcat-7.0.67 with tomee-plus-webapp-1.7.3
>
> The *root cause* for the above issue is configuration of log4j in a J2EE
> environment is different, EJBs don't see the classes loaded by a relevant
> webapp--EJBs are loaded by a different class loader. Following is the
> related post provided for reference.
> http://www.onjava.com/pub/a/onjava/2003/04/02/log4j_ejb.html
>
> Log4j2 should take care of loading configurations in all contexts but due
> to an open issue the configurations are not loaded in ejb contexts.
> Provided log4j open jira for reference.
> https://issues.apache.org/jira/browse/LOG4J2-1468
>
> We have enabled log4j2 in our environments by copying 2.6.2 related jars
> into application server lib folder and loading each ear specific log4j xml
> into classpath through normal spring bean.
>
> *Step 1*:
>
> [image: enter image description here]
>
>
>
> *Step 2*:
> In each EAR called below initialization method from spring bean
>
>     public void init() throws Exception {
>     String resolvedFileName = StringServices.resolvePatternForEnvironment(
> getFileName(),
>     new HashMap<String, String>());
>     File file = new File(resolvedFileName);
>     if (file.exists()) {
>     Configurator.initialize(getAppName(), file.getAbsolutePath());
>     }
>     }
>
>     <bean id="log4jPropertiesConfigurator" class="com.abc.log.impl.Log4jPropertiesConfigurator"
> scope="singleton" init-method="init">
>     <property name="fileName" value="$LOG_DIR./config/appWorkflowEar.xml"
> />
>     <property name="appName" value="appEar" />
>     </bean>
>
> Also tried by changing the above step2  call to following options but no
> luck
>
> *Option 1*
>
>
>
>     LoggerContext ctx = (LoggerContext) org.apache.logging.log4j.
> LogManager.getContext(false);
>                             ctx.setConfigLocation(file.toURI());
>                             ctx.updateLoggers();
>
> AND
>
> *option 2*
>
>
>
>       ctx = (LoggerContext) org.apache.logging.log4j.
> LogManager.getContext(true);
>                         ctx.setConfigLocation(file.toURI());
>                         ctx.updateLoggers();
>
> *NOTE*: If I invoke Log4jPropertiesConfigurator bean method from an EJB
> then log4j2 working fine but only for one configuration file not for
> multiple configuration xmls i.e. not for ear specific XMLs.
>
> Log4jPropertiesConfigurator bean method that consists of below code
>
>
>     public void initForEjb() throws Exception {
>     String resolvedFileName = StringServices.resolvePatternForEnvironment(
> getFileName(),
>     new HashMap<String, String>());
>     File file = new File(resolvedFileName);
>
>     if (file.exists()) {
>     final Class<?> clazz = ReflectionUtil.getCallerClass(
> Log4jPropertiesConfigurator.class.getName());
>     LoggerContext ctx =Configurator.initialize(getAppName(),
> clazz.getClassLoader().getParent(), file.getAbsolutePath());
>     Configuration config = ConfigurationFactory.getInstance().getConfiguration(getAppName(),
> file.toURI());
>     ctx.start(config);
>     ctx.updateLoggers();
>
>     } else {
>
>     System.out.println(resolvedFileName + " not found - using default
> log4j settings");
>     }
>     }
>
Loading...