Showing posts with label log4j. Show all posts
Showing posts with label log4j. Show all posts

Sunday, September 21, 2014

Log4j 2 Migration Tutorial : Solve ERROR StatusLogger Could not search jar file 'C:\EAP-6.2.0\jboss-eap-6.2\standalone\deployments\myPorject.war\WEB-INF\lib\log4j-core-2.0.2.jar\org\apache\logging\log4j\core

I was working with Log4j-1.2, but have decided to move to Log4j-2.0. I have hence removed the old dependencies and add the new ones.





<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-core</artifactId>
 <version>2.0.2</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.0.2</version>
</dependency>

Once done, I deployed my application but got this error:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

 ERROR StatusLogger Could not search jar file 'C:\EAP-6.2.0\jboss-eap-6.2\standalone\deployments\myProject.war\WEB-INF\lib\log4j-core-2.0.2.jar\org\apache\logging\log4j\core' for classes matching criteria: annotated with @Plugin file not found java.io.FileNotFoundException: C:\EAP-6.2.0\jboss-eap-6.2\standalone\deployments\drl_m2m.war\WEB-INF\lib\log4j-core-2.0.2.jar\org\apache\logging\log4j\core (The system cannot find the path specified)


In fact, using Log4j 2.0, we need to make some changes on the Log4j file :

- Step 1 : Rename it to log4j2.xml. Change also the file structure.



<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="com.myproject" level="trace">
      <AppenderRef ref="Console"/>
    </Logger>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>


- Step 2 : Change the dependencies to use the 2.0 and no more 1.2 libraries.

- Step 3: Remove all the log4j filters and listeners from the web.xml According to this JIRA.

- Step 4: Exclude any dependency to old versions of log4j.



<dependency>
 <groupId>common_m2m</groupId>
 <artifactId>common_m2m</artifactId>
 <version>${project.version}</version>
 <exclusions>
  <exclusion>
   <artifactId>log4j</artifactId>
   <groupId>log4j</groupId>
  </exclusion>
 </exclusions>
</dependency>

- Step 5: Change the call in the java code.



import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class MyClass {

 private Logger logger = LogManager.getLogger(MyClass.class);

}

That's all for this post, hope you enjoyed it and thanks for my friend Hassen Kalaldeh for his help.

Tuesday, May 6, 2014

Resolved : Logging in Jboss EAP 6 (JBoss AS7), Spring using Log4j

I was for a good time wondering why my Log4j is logging only the messages coming from my classes but not frameworks like  Spring, JDBC Template, CSveed. Every think was well configured, no error messages but I see framework messages only in the server.log.





So finally, I found the right steps to do.

Step 1:


Exclude Logging from JBoss. This previous article explains how to do.
So here is my jboss-deployment-structure.xml :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<jboss-deployment-structure>
 <deployment>
 <!-- Add this line in the server lauch parameter : -Dorg.jboss.as.logging.per-deployment=false -->
  <exclusions>
   <module name="org.apache.log4j"/>
   <module name="org.apache.commons.logging"/>
              <module name="org.slf4j" />
             <module name="org.slf4j.impl" />
  </exclusions>   
 </deployment>
</jboss-deployment-structure>

Step 2:


Put you log4j.xml or log4j.properties in your classpath. In a Maven project, it should be under "\src\main\resources". Othrwhise, you can fix the classpath in the web.xml



1
2
3
4
5
6
7
8
<listener>
 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<context-param>
 <param-name>log4jConfigLocation</param-name>
 <param-value>classpath:/main/resources/META-INF/log4j.xml</param-value>
</context-param>


Step 3:

So Now, we need to add the required jar, AND Exclude some Jars. In the pom.xml, we need to exclude commons-logging from Spring-context jar and we need to add slf4j-api, slf4j-log4j12, jcl-over-slf4j, log4j.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>${spring.version}</version>
 <exclusions>
  <exclusion>
   <artifactId>commons-logging</artifactId>
   <groupId>commons-logging</groupId>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>${slf4j-log4j12.version}</version>
 <scope>runtime</scope>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>${slf4j-log4j12.version}</version>
 <scope>runtime</scope>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>jcl-over-slf4j</artifactId>
 <version>${slf4j-log4j12.version}</version>
 <scope>runtime</scope>
</dependency>
<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>${log4j.version}</version>
</dependency>

So this step was the latest and Important step that I have discovered and which blocked my logging before.

Thursday, April 24, 2014

Resolved : log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.SpringBeanAutowiringSupport

I was fixing my Log4j.xml on my web project developed with Spring and running on JBoss.


Deploying my application, It catches my eyes an error message although my application was deployed.
it was this error:

4:27:14,323 ERROR [stderr] (MSC service thread 1-2) log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.SpringBeanAutowiringSupport).
14:27:14,323 ERROR [stderr] (MSC service thread 1-2) log4j:WARN Please initialize the log4j system properly.

It was really strange as I declare a logger for org.springframework.
But it seems that the porblem is coming from SpringBeanAutowiringSupport. Although I have declared also a logger fro this method. It didn't work and I get the same error.

What is special for this class is that I am using it to extend my web service class an that it will be initialized and managed by Spring.
So my code was like this :

@WebService(serviceName = "UserService")
public class UserService extends SpringBeanAutowiringSupport {
// Here the service implementation
...
}

It was a normal code. And it works fine. But my log4j seems not very happy.
After some search, I have seen by chance in one blog how to use the SpringBeanAutowiringSupport differently.
Based on that, I have modified my code and now it looks like this :

@WebService(serviceName = "UserService")
public class UserService extends SpringBeanAutowiringSupport {

   @PostConstruct
    public void init() {
              SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

...
}

Surprise, now every think is working nice and I have no more error messages while deploying.  This is nice :D

Surprise, When testing the Web service. I have a NullPointerException. So now I know that the Error comes from the extends. But to make my web service work, I need to use it.

Sunday, April 20, 2014

Resolved : log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader)

I was using log4j in one of my spring maven projects. although the log4j.xml were well done, I get a warning then an error during deployment :

Initializing Spring root WebApplicationContext
 [stderr] (ServerService Thread Pool -- 74) log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
 ERROR [stderr] (ServerService Thread Pool -- 74) log4j:WARN Please initialize the log4j system properly.
 ERROR [stderr] (ServerService Thread Pool -- 74) log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

My log4j.xml was under src/main/resources. bUT EVEN BY SPECIFYING THE log4jConfigLocation, didn't resolve the problem.
So my Web.xml were like this:





After some invetigation, I have found that we should add a Log4jConfigListener in the first line (with the log4jConfigLocation) :

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

So the Web.xml become:





Wasn't easy to find but at least now it is done :)

Articles les plus consultés