Does log4j requires JSF jars? - java

I build a REST web service (using JAX-RS, Spring, Spring JMS, and ActiveMQ). I'm surprised that when I deploy it to Tomcat 5.5.23 I get an exception that JSF jars are required?!
Error configuring application listener of class org.apache.myfaces.webapp.StartupServletContextListener
java.lang.ClassNotFoundException: org.apache.myfaces.webapp.StartupServletContextListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1359)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3712)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
The web service was working fine until I added log4j functionality in different classes, here's my log4j.properties file (I placed it in WEB-INF/classes):
log4j.rootCategory=INFO, S
log4j.logger.com.dappit.Dapper.parser=ERROR
log4j.logger.org.w3c.tidy=FATAL
#------------------------------------------------------------------------------
#
# The following properties configure the console (stdout) appender.
# See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.S = org.apache.log4j.ConsoleAppender
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
Any idea how to resolve this?

I would expect to see this if you have a reference to org.apache.myfaces.webapp.StartupServletContextListener in your web.xml, like:
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
Can you verify that there is no such element?

Sounds like a classloader hierarchy issue. You should enclose log4j in your WAR file, not put it in the shared libraries of Tomcat.
Also, I had some problems with Tomcat 5.5 that was fixed in Tomcat 6.0. You may want to consider upgrading.

Make the following dependencies(if present) in maven as provided:
javax javaee-api 6.0 scope : provided
javax.servlet javax.servlet-api 3.0.1 scope : provided

Related

The import org.apache.log4j cannot be resolved

Eclipse, Selenium, Windows
The import org.apache.log4j cannot be resolved
I have followed all the documented steps I can find, i.e.:
1) the following two jar files are in the project build-path:
log4j-core-2.6.2.jar and log4j-api-2.6.2.jar
2) the above two jar files are located in the following folder:
C:\eclipse\apache-log4j-2.6.2-bin
3) The above folder is in my system CLASSPATH environment variable C:\eclipse\apache-log4j-2.6.2-bin
4) The project contains the following log4j.properties file in the project root
# This sets the global logging level and specifies the appenders
log4j.rootLogger=INFO, theConsoleAppender
# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
5) I have cleaned the project
6) failing import is: import org.apache.log4j.Logger;
Intermittently I am also getting this error message
"The type org.apache.log4j.Logger cannot be resolved. It is indirectly referenced from required .class files" at the first line:
package com.example;
This error disappears when I do a project -> Clean
but the error on import of log4j persists.
Any thoughts would be greatly appreciated.
If you are getting this error it means your application or one of its dependencies uses the Log4j 1.2 API.
The org.apache.log4j package is the Log4j 1.2 namespace. Log4j 2 uses the org.apache.logging.log4j namespace. Log4j 1.2 became End of Life in 2015 and users are recommended to upgrade to Log4j 2.
You already have the correct log4j-api-2.x.jar and log4j-core-2.x.jar in your classpath, the only thing missing is the log4j-1.2-api-2.x.jar. log4j-1.2-api is an adapter module that ensures your application's calls to the Log4j 1.2 API are delegated to the Log4j 2 implementation.
Please ensure the old log4j-1.2.x.jar is removed from the classpath.
Finally, the configuration you showed seems to be for Log4j 1.2 and will not work with Log4j 2. The Log4j 2 manual has many example configurations. (The documentation alone is reason to upgrade, it is so much better in Log4j 2.) See for example the section on the Console appender or the File appender.
Log4j 2 also supports a properties configuration format (but the syntax differs from the log4j 1.2 properties format).
Check your Eclipse version and log4j dependency version. If 1.2.17 is not working then check using 1.2.16.

Wildfly 10 not showing debug messages

I'm testing my ear application on the container and I need to see some debugging messages I spread on my application. I'm using slf4j-api with log4j as logging framework.
During my test phase (out of the container) all logging was working perfectly, so the configuration is fine. But now I've deployed the application with the same configuration but my messages are not showing. Here is my log4j's config:
#rootLogger config
log4j.rootLogger=INFO, console
#appender config
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console..threshold=DEBUG
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} [%t] %p %l - %m%n
# Log JDBC bind parameter runtime arguments
log4j.logger.org.hibernate.type=INFO
#application logger config
log4j.logger.ar.edu.unt.sigea=DEBUG, console
As I said, when I run my #Test methods, all my logger.debug() messages are shown correctly, but now that I'm running on the container with the same configuration, no debug message is shown.
I found this post and added the line log4j.appender.console..threshold=DEBUG as suggested by the answer but didn't work.
I'm deploying on Wildfly-10.0.0.Final Application Server and I'm using this logging dependencies:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
What am I missing? Where should I look for? Thanks in advance for your answers
You don't need to use the log4j binding unless you want to use your own log4j configuration. From the configuration file it looks like you're just using console appender which is already provided by the WildFly logging subsystem.
All you need to do to see debug messages with your current configuration is to remove the log4j.properties from your deployment and remove the org.slf4j:slf4j-log4j12 dependency from your pom. Then you can use the logging subsystem to configure logging and turn on/off debug logging. If you use CLI or the web console you can change logging levels without restarting the server.
To add a debug level and change the default console-handler level to DEBUG. The following two CLI commands are all you need to configure debug logging.
/subsystem=logging/logger=ar.edu.unt.sigea:add(level=DEBUG)
/subsystem=logging/console-handler=CONSOLE:write-attribute(name=level, value=DEBUG)
looking at this Wildfly Documentation I realized that my log4j.properties file was located in a wrong place: it was in a submodule of my project and must be in the META-INF folder of the EAR module.
By default, Wildfly takes the deployment's logging configuration, so no extra configuration is needed in standalone.xml (or standalone-full.xml depending on what profile are you using, which is my case).
switch the log level to DEBUG on console
{wildfly}/bin/jboss-cli.sh --connect
[standalone#localhost:9990 /] /subsystem=logging/console-handler=CONSOLE:write-attribute(name=level,value=DEBUG)
[standalone#localhost:9990 /] /subsystem=logging/root-logger=ROOT:write-attribute(name=level,value=DEBUG)
switch it back to whatever it was initial configuration (here it is INFO)
[standalone#localhost:9990 /] /subsystem=logging/console-handler=CONSOLE:write-attribute(name=level,value=INFO)
[standalone#localhost:9990 /] /subsystem=logging/root-logger=ROOT:write-attribute(name=level,value=INFO)
From: https://gist.github.com/lfryc/aae879ceb5534292e150

What is missing from my log4j configuration that org.hibernate.cfg.annotations.Version requires?

I'm trying to figure out why our Hibernate code is not logging anything to log4j. I put together a simple project that calls one Hibernate service method that logs a log4j message as the first line.
Both the project and the Hibernate service have the following log4j specific code:
private static Logger NTEVENT_LOG = Logger.getLogger("NTEVENT");
NTEVENT_LOG.debug("==== LOG4J logging");
When I run the project (Eclipse->run) I get the following messages in red:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
But immediately after I see the logging from the application itself so some part of log4j did get correctly initialized. However, nothing is being logged by the Hibernate service.
[main] DEBUG NTEVENT - ==== LOG4J logging
This is my log4j.xml
<appender name="NTEVENT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="NTEVENT" />
</root>
I must be missing something that Hibernate is expecting but I have no idea what.
To configure log4j using xml file you need to use DOMConfigurator.configure() method in your main.
Also for this method you need to import org.apache.log4j.xml.DOMConfigurator package.
public static void main(String[] args) {
DOMConfigurator.configure("log4j.xml");
..//your code
..
}
Hope this helps..!!
The problem was as ThrashBean implied, logging for org.hibernate was not enabled. If you want to see Hibernate debugging output, queries for instance, you need:
I finally got what was happening in my case. It might also help those in Netbeans users facing the same problem coz i have lost a lot of hair for quite some months now. My application had just stopped logging to console so i couldnt debug since the errors were not being displayed.
The answer "There are two log4j jar files in you libraries at different location." was true. Initially i had expected to find 2 files with the name log4xxx.jar of course with different version numbers but no file like that at all was there. You see Netbeans comes prepackaged with Hibernate libraries and many other libraries of course. So when developing in hibernate applications in Netbeans you DON'T need to manually add your own Hibernate Jars unless of course you need a specific version etc, you can just add the Hibernate library to your application via that right click project - Properties - Libraries - Add Library button . This will add many hibernate Jars in the background/classpath when running and also on Compiling in the dist/lib folder BUT NOT in the lib of the application. In my case i had added both the Hibernate Library AND also copied the generated hibernate jars from dist/lib to lib. This had happened by mistake as i copied libraries from previous application to include in my new application for use there.
So take home point, if in Netbeans, go to your lib folder, if you see jars like
hibernate-commons-annotations-x.x.x.Final.jar, hibernate-core-x.x.x.Final.jar,hibernate-entitymanager-x.x.x.Final.jar, hibernate-jpa-2.0-api-x.x.x.Final.jar, hibernate-tools-x.x.x.CR1.jar
Remove them and check if error resolves.

Logging from Spring using Log4j under WebSphere

I'm using log4j in my WebSphere application. I need to debug class org.springframework.ejb.access.AbstractRemoteSlsbInvokerInterceptor, so I've created logger in my log4j.xml file:
<logger name="org.springframework">
<level value="INFO" />
</logger>
<logger name="org.springframework.ejb.access">
<level value="TRACE" />
</logger>
I've created also commons-logging.properties in src/main/resources of the web project (in maven):
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
However, Spring is NOT logging using log4j. I see no springframework logs in my debug file, but I can see some of them (INFO) in the console. Therefore I assume, the apache commons logging used by Spring is NOT logging using log4j.
Is it possible (and how) to redirect logging used by Spring to Log4j engine under WebSphere?
WebSphere 7.0, Spring 3.1.2, log4j 1.2.6, commons-logging 1.1 provided in shared library.
Try the following:
Consider using slf4j for your logging. Add log4j to your dependencies in maven.
Add jcl-over-slf4j to your maven dependencies. This redirectes each request from jcl to slf4j.
Search for the dependency commons-logging inside your maven dependencies. Exclude it, so it does not get to your deployment archive.
Delete the "commons-logging.properties"-file.
I hope that everythings works out fine now.
This technique to route JCL (including Spring) logging to your log4j configuration still works.
For the apps where we also have slf4j already (the Spring WebFlow ones), that can also be routed to your log4j configuration.
However, if you just want to see the Spring log information, you can increase its level of logging to WebSphere's own logs (SystemOut.log) via the console's Troubleshooting > Logs and Trace > server-name > Change log level details.
Add something like : org.springframework.ejb.access.*=fine (colon is the separator).
I don't know what the exact WAS levels correspond to, but fine, finer, and finest are listed under the "Trace" levels if you expand the Components and Groups area just to see what is available.
(I don't think it matters that your particular Spring package is not listed under there, BTW. I believe it should still successfully control your logging.)

EclipseLink and log4j: how to use both

I'm using Eclipselink JPA provider, and noticed that it writes in console only.
I configured both the console and file appenders, but EclipseLink log entries (SQL queries for example) are appeared only in the console log.
How to fix it?
Here is my log4j configuration:
log4j.rootLogger=ALL, FILE, CONSOLE
log4j.logger.uk.co.mycompany=DEBUG
log4j.logger.org.eclipse.persistence=ALL
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${catalina.base}/logs/application.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss, SSS} %t [%p] %c{1} - %m%n
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%p] %m%ne
In the persistence.xml:
...
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
Eclipse Link doesn't use log4j by default. This page describes how you can integrate it:
http://wiki.eclipse.org/EclipseLink/Foundation/Logging
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging
Make sure you initialize EclipseLink logging facilities (by doing some action that normally requires logging) before you add your own handlers to the system. I think that EclipseLink overrides the root logger settings and maybe destroys your configuration.
If you are using EclipseLink with JBoss AS7 i recommend Step 5: Configure EclipseLink logging (optional) in the following page on how to get logging correct:
https://community.jboss.org/wiki/HowToUseEclipseLinkWithAS7
In short you will need to:
Add JBossLogger.java file to you project (attached to the article)
Add dependency to library jboss-logging
Set property eclipselink.logging.logger in persistence.xml

Categories

Resources