Using Log4j2.10 and slf4j-api 1.8 in OSGI/Eclipse - osgi

I am trying to use Log4j2.10 and slf4j-api 1.8 beta1 in my eclipse project.
I cannot get the binding to work,slf4j complains with:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
It worked with Log4j 2.9.1 and slf4j-api 1.7.24 which was using the StaticLoggerBinder mechanism which is no longer supported in slf4j 1.8
My eclipse plugin depends on
org.apache.logging.log4j.osgi,
org.apache.logging.log4j.api,
org.apache.logging.log4j.core,
org.apache.logging.log4j.slf4j-impl,
slf4j.api
The problem is that in the LoggerFactory (slf4j-api) the ServiceLoader.load(SLF4JServiceProvider.class) call returns any empty list, so the spi file of the org.apache.logging.log4j.slf4j-impl is not found or not loaded.
Is there anything I am missing to get this working?
Thanks in advance,
Rob

You can use org.apache.logging.log4j: log4j-slf4j18-impl for binding with SLF4J 1.8.x, as mentioned in this official documentation.

Log4j2 does is not yet compatible with slf4j-api version 1.8.x which now uses the ServiceLoader mechanism as enforced by JPMS/jigsaw/Java9 modules.

Related

Change ActiveMQ Log4j version

I have a Gradle project that uses activemq-all as a dependency, and within the project I use Log4j that comes with ActiveMQ jar. The latest ActiveMQ version is still using Log4j 1.2.x, and I want to use the latest version of Log4j.
I included Log4j as a separate dependency, but Gradle won't detect it. Any suggestions?
If the program is a client-side only, you should depend on activemq-client instead of activemq-all.
activemq-client depends on slf4j-api, and you can use the slf4j bridge to log4j or log4j2.

java.lang.ExceptionInInitializerError is produced when lombok is used within a Spring-boot application

When I am using lombok within a spring-boot application java: java.lang.ExceptionInInitializerError is produced. What can I do to prevent this error?
What have I done?
I've installed lombok plugin in IntelliJ and restarted it after installing plugin.
I've enabled Annotation Processors from both
File->Setting and Filer->Other Setting->Default-> setting->Build, Execution and deployment->Compiler->Annotation Processors
File->Setting and Filer->Setting->Build, Execution and deployment->Compiler->Annotation Processors
I've also tried to change the scope of dependency in the pom.xml to 'provided'
I tried other versions of lombok such as 1.16.21, 1.14 and ... but when I change the version other errors occur (For example when I change the version to the 1.16.16, Compiler says 'java package lombok dose not exists.')
Errors with different versions:
1.16.21: java.lang.ExceptionInInitializerError
1.16.20: java.lang.ExceptionInInitializerError
1.16.19: java.lang.ExceptionInInitializerError
ver < 1.16.18: java: package lombok does not exist
other info:
JDK: 10
Spring-boot: 2.0.2.RELEASE
OS: Windows and Linux
The amount of boilerplate code (setter and getters) is really high in my code, and I really need lombok what can I do to use it?
.
The problem has been solved in Lombok version 1.18.0
There is known issue with lombok and Java 10: https://github.com/rzwitserloot/lombok/issues/1572. Should be fixed with 1.16.22 version.

slf4j Logger, LoggerFactory not working at runtime from a child jar

I am a facing an issue while using slf4j Logger and LoggerFactory in a child jar (suppose CHILD.jar).
I am deploying one EAR in WebSphere. The EAR structure is like below-
Parent ear>
META-INF
Project Specific dependency jars
Project Sub Modules bundled as jars
commons-logging-1.0.4.jar
common-logging-api-1.1.jar
CHILD.jar
In CHILD.jar's MANIFEST.MF we are not giving any CLASSPTH entry as this CHILD.jar is used by several other applications and those other applications may not use any same slf4j implementor.
Below is the MANIFEST file structure-
Manifest-Version: 1.0
Build-Jdk: 1.7.0
Built-By: C12560
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.0.4
In our application, we are keeping all the jar at the same hierarchy where the CHILD.jar is present.
Now while deploying, no exception is thrown. At runtime, it is throwing InvocationTargetException (and NoClassDefFoundError) in all those classes where we have used org.slf4j.Logger and org.slf4j.LoggerFactory.
If we comment out all these logger entries, the application is working fine and no exception is thrown at runtime.
Even if we use org.apache.log4j.Logger instead of org.slf4j.Logger, the code is working at runtime(Note- log4j-1.2.13.jar is present in the same hierarchy as CHILD.jar).
Queries-
Is slf4j not getting its implementor?
Any jar I have to include in the hierachy same as CHILD.jar?
Please note- I have tried including jcl-over-slf4j jar and log4j-over-slf4j.jar(and removed common-logging.jar) in the same hierarchy as CHILD.jar but it didn't work.
Any help is highly appreciated.
if you want to use jcl or log4j as implementor of slf4j, what you need is slf4j-jcl.jar or slf4j-log4j.jar.
Besides, when getting a NoClassDefFoundError, you should go through the full stacktrace. Usually it is caused by some other exception.

Maven Plugin log4j logging with slf4j does not log in file

I have an issue with logging in a maven plugin for my applicaton. The maven plugin uses log4j with slf4j as facade, and I have configured (in the plugin) a special file logger for outputs.
When I run my plugin application (local, not as a plugin), the output is written to the file as intended.
However, when I use the maven plugin in another project and build it with mvn clean install, the output is only on the console. It seems not to be a problem of configuration as I do find my logger and (file-)appender.
My assumption is that maven absorbs the slf4j output to display it on its own console. Can anybody confirm or determine this respective tell me how to fix it? I appreciate any help.
It would be helpful to see your actual POM file. However, there are a few potential problems that I know about.This dependency is required because the
The maven-javadoc-plugin:2.10.3 has a dependency on maven-core:2.2.1, which has direct or indirect dependencies on sl4j-nop:1.5.3 and slf4j-jdk14:1.5.6. I introduced a dependency on maven-core:3.3.9, which didn't have this problem. The artifact maven-javadoc-plugin:2.10.3 also has a dependency on log4j:log4j. I placed an exclusion on the dependency for maven-javadoc-plugin so that it didn't include log4j:log4j.
See https://bradleyaross.wordpress.com/2016/05/05/java-logging-frameworks/ and the referenced GitHub libraries, especially the pom.xml files for the parent module and tutorials-common.

Maven - Failed to load class "org.slf4j.impl.StaticLoggerBinder"

I have recently installed Maven on my windows based laptop. But getting following error when running mvn just to test my installation:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Whoever when running mvn -version I am getting appropriate response:
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:29:23+05:00)
Maven home: C:\apps\Maven
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_25\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
Note that I am getting exactly same error when running mvn install, mvn package, mvn clean or any other Maven target in my maven project, while project doesn't have any dependency for SLF4J.
This error usually means that you have the slf4j-api library in your classpath without an SLF4J binding associated with it. Make sure to have exactly one of these binding jars in your classpath alongside the slf4j-api jar (also make sure the versions are compatible):
slf4j-nop.jar,
slf4j-simple.jar,
slf4j-log4j12.jar,
slf4j-jdk14.jar or
logback-classic.jar
The SLF4J lists all such error codes on its FAQ page. You can find this issue and resolution documented here.
IIRC, this is merely a warning and won't affect your build process - or does it? If you didn't really mean to include the SLF4J libraries in your classpath, they would have been pulled in transitively. Use the mvn dependency:tree or the m2eclipse's Dependency Hierarchy view to find the source of these the SLF4J related libraries and exclude them.

Resources