Netty in Spring and slf4j Logger Factory - spring

My basic question is. How do I configure Netty to use slf4j in Spring? I keep getting the error below in Spring but not eclipse using the slf4j jars with the log4j bridge and api in the path. I am using Spring Tools suite, spring 3.2.3, and Netty 4 (post jboss).
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
The program fails at the line where I instantiate the connection group after I try setting the default logging factory.
I am really new to Netty and slf4j. I have been banging my head against this one for a while and done a bit of research on Stack Overflow and the internet. I managed to get Netty working with slf4j in eclipse and used the Internal Logger Factory. I tried configuring slf4j in a similar way to log4j (A properties file with appenders).
I used the following code for the InternalLoggerFactory in both of the following ways with the appropriate imports.
InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
and
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.getDefaultFactory());
These lines are placed immediately before my initialization of the connection group but I tried them in my Main App/Driver's main method as well.
Thanks for any help. I would and do really appreciate it.

´org.slf4j.LoggerFactory´ is part of ´slf4j-api-1.75.jar´, so it is likely that you have some deployment problem. -- Check that this jar is really in the classpath of you application, or if you have an web application, then check that this jar is deployed to you application server.
One other thing (that is maybe not the cause of your problem) is that you have log4j-1.2.16.jar and log4j-to-slf4j-2.0-beta8.jar. This is will lead to other problems, because log4j-to-slf4j is a log4j-to-slf4j bridge, that forward log4j loggers to slf4j. On the ohter hand you have the real log4j in your classpath too. I would remove log4j-1.2.16.16. I hope this slf4j docu describe it a bit better than me.
An other point that brothers me, is that you use different version of slf4j. I strongly recommend to use the same version for all slf4j libs!

Related

Application using jboss-logging when it should use log4j

I'm upgrading a Spring MVC application from Spring 3.1 to 4.3, and Hibernate 3.6 to 5.2. I'll be running this in Wildfly 8. Dependencies are managed by Maven.
Spring uses commons-logging, which looks at the classpath of the application and attempts to choose a suitable logging framework. In my case, it seems to be choosing the wrong one. I have included log4j in my pom.xml, and checking the dependency hierarchy I can see that jboss-logging is not there. Here is the error message I'm getting:
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.jboss.logging.Logger.debugf
So even though I don't have jboss logging in my project, commons-logging is finding it somewhere and attempting to use it, but unsuccessfully. After some searching I found jboss-logging-3.1.4.GA.jar in Wildfly.
The reason for the error is that this is an old version of jboss-logging. I added a newer jar to the server and edited module.xml to point to it, and the error went away. That proves that commons-logging is finding jboss-logging on the server.
The problem is that I don't want jboss-logging, I want log4j. How do I force commons-logging to use log4j and ignore what is on the server?
Edit: I followed the link chrisharm put up and added these lines to the standalone.xml file :
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
I also changed the module.xml to point to the older jar again, since I would rather see an error if jboss is used. When I run now I get this:
Caused by: java.lang.NoClassDefFoundError: org/jboss/logging/Logger
This may be a step in the right direction, since now Spring doesn't have access to jboss-logging, but it is still trying to use it for some reason.
https://docs.jboss.org/author/display/WFLY8/How+To#HowTo-HowdoIusemyownversionoflog4j%3F
If you need/want to include your version of log4j then you need to do the following two steps.
Disable the adding of the logging dependencies to all your deployments with the add-logging-api-dependencies attribute and disable the use-deployment-logging-config attribute OR exclude the logging subsystem in a jboss-deployment-structure.xml.
Then need to include a log4j library in your deployment.
This only works for logging in your deployment. Server logs will continue to use the logging subsystem configuration.

Jetty 8, Weld - Injection isn't working

I'm trying to setup a project using Jetty 8 as the server (jetty-maven-plugin:8.1.11.v20130520), Weld for CDI (weld-servlet:2.0.1.Final), Hibernate for JPA (hibernate-entitymanager:4.2.2.Final, hibernate-jpa-2.0-api:1.0.1.Final) and Jersey for JAX-RS.
It seems that injections isn't working at all.
I tested whether the EntityManager is being injected, and the value was null.
The log doesn't show any errors or warnings while executing the server.
Am I missing something ?
If needed, I will post the log file to gist.
Here is the gist of all relevant files: https://gist.github.com/StalkingKillah/e986474b6885d83c7f8d
This is how I got it working for Jetty 9, and it should be basically the same for Jetty 8.
http://www.eclipse.org/jetty/documentation/current/framework-weld.html
It is easier to have the weld jar outside of the webapp as then you don't have to mess with tweaking the classes exposed to the webapp classloader like you are here (with the -org.eclipse.jetty bit for the ServletContext). I suspect there are more classes that would have to be added to that in order to make it work. You should get a note in the logging that says it is all enabled and active though.

OpenJPA logging with slf4j on WebSphere

My Java EE 6 application uses slf4j with logback as logging framework.
Now I want to add the SQL traces of OpenJpa to my log files. The OpenJpa-Documentation says, I can use a parameter to this:
<property name="openjpa.Log" value="slf4j"/>
I use the WebSphere Application Server v8.0.0.1 as Java EE container. If I deploy my application to the server, this parameter does not change anything. I can change log levels in WebSphere Admin Console and this works fine. But a cannot bypass the OpenJpa logging to my slf4j framework.
Does anyone uses such configuration and solved the problem?
Btw. I know, that the InformationCenter-Article http://publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=%2Fcom.ibm.websphere.nd.doc%2Finfo%2Fae%2Fae%2Ftejb_jpatroubleshoot.html
says, that the parameter will be ignored, but everything should be possible, eh?
I don't know what version of OpenJPA is embedded in WAS 8.0.
In OpenJPA 1.x there was no possibility to use "slf4j". A workaround was copying the class org.apache.openjpa.lib.log.SLF4JLogFactory from OpenJPA 2.x sources to your application and using it directly:
<property name="openjpa.Log" value="org.apache.openjpa.lib.log.SLF4JLogFactory"/>
You can always specify the factory class name directly, the short name is only a convenience trick.
In OpenJPA 2.x the SLF4JLogFactory is present, so it should work with your current settings; perhaps you set it in a place that is overridden by other configuration? For example, if you configure JPA through EntityManagerFactory's propertyMap, it takes precedence over the settings in persistence.xml.
I solved it.
There are four things to do:
Say that OpenJpa logs the statements. That is done by the property entry in the persistence.xml.
Say Slf4j that it has to fetch the logs from JUL too. This can be done by installing the SLF4JBridgeHandler by SLF4J. I installed the bridge via a ServletContextListener which is called at application start.
Configure Logback that it does not log ALL logs from JUL but only the needed Logs. This is done by adding the LevelChangePropagator in the logBack-test.xml
Ask the WebSphere TraceService to hand down the logs from OpenJPA to the application. This is a setting in the WebSphere Administration Console

using slf4j to log in spring when I can't get rid of commons-logging

I'm using some a component other than Spring that has a commons-logging dependency.
So, excluding commons-logging from the spring dependency doesn't get rid of it, and so the instructions here aren't helping. Is there some more declarative way to push Spring 3.0.5 to talk to slf4j?
I had an issue on Tomcat 6 where Commons Logging wouldn't play nice with SLF4J which got dragged in from Hibernate. We had configured Commons Logging to go to Log4J anyway. After explicitly including "slf4j-log4j 1.6.1" and its "jcl-over-slf4j 1.6.1" component to take out Commons Logging, I still got errors from Commons Logging. The answer lied in this blog post: You explicitly include Commons Logging at version "99.0-does-not-exist". This includes an empty JAR, so that only the SLF4J code gets executed now.
If you are using Maven, please read the FAQ entry about excluding commons-logging.
Use the SLF4j bridge

Spring, log4j and JBoss4

I'm running JBoss 4.2.3.GA, and have a web app that uses Spring and Log4j. I've set up Spring's Log4jConfigurer, but am having a problem since JBoss's log4j file is not on the classpath. It's at jboss.home/server/defaul/conf/jboss-log4j.xml. So, without hard-coding the path in Log4jConfigurer, how can I get Spring to use JBoss's log4j file?
You don't generally modify JBoss's log4j config via the applications, that's a route to madness. JBoss uses a shared configuration amongst all apps.
You can, however, programmatically alter log4j's runtime configuration, such as adding custom appenders or loggers. This isn't what Spring's Log4jConfigurer is for, however - you use that to configure a standalone log4j config.
Do answer your question: JBoss usually sets additional system properties that point to the server directory and configuration directories. You should be able to access these from your application or let them be resolved from the spring contexts.
In your case, it should be ok to access ${jboss.server.home.dir}/conf/jboss-log4j.xml
As skaffman correctly pointed out in a comment, there is no need to configure Spring to read JBoss' log4j configuration unless you have otherwise adapted something in the packaging. You will only have to take care that no log4j.[properties,xml] lies somewhere in the root of the classpath or that a component tries to configure Log4J "manually".
Hope this could help,
Kariem

Resources