Using lo4j2 in Keycloak custom SPI deployed as JBOSS module - java

I am struggling to include log4j2 dependency to simple Keycloak SPI.
In general I have log4j2.xml file which is defining RollingFileAppender to log into $JBOSS_HOME/standalone/log/app.log separately from server.log. I have included in gradle build log4j-core, log4j-api and inside the simple implementation of the SPI I have
private static final Logger logger = LogManager.getLogger(TestLoggingEventListenerProvider.class);
Moreover in jboss-deployment-structure I have prevented the server from automatically adding some dependencies as follows and added log4j2 from my dependencies:
<jboss-deployment-structure>
<deployment>
<exclusions>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<module name="org.apache.logging.log4j" />
<module name="org.apache.commons.logging"/>
</exclusions>
<dependencies>
<module name="org.apache.logging.log4j2" export="true" />
</dependencies>
</deployment>
Have also setup some properties to instruct jboss where to find log4j2.xml file
/subsystem=logging:write-attribute(name=use-deployment-logging-config,value=false)
/system-property=log4j.configurationFile:add(value=$JBOSS_HOME/standalone/configuration/log4j2.xml)
Would much appreciate if someone can point me to a documentation or a way to do that properly.
Thanks.

Related

Jboss EAP 7 - How to exclude implicit modules from deployment (javax.jms)?

I didn't think I would end up here but after a lot of Google and StackOverflow searches here I'm.
This is my exact problem except that I can't afford to make code changes.
The WAR I'm trying to deploy includes a JMS library (i.e. javax.jms, which I cannot exclude form the WAR.) which is already loaded by Jboss EAP 7 by default. The path to jar is something like this jboss/modules/system/layers/base/javax/jms/api/ain/jboss-jms-api_2.0_spec-1.0.0.Final-redhat-1.jar. Because of this two different versions of the same classes loading I'm getting ClassCastException.
org.apache.activemq-ra.ActiveMQConnectionFactory cannot to be cast to javax.jms.ConnectionFactory
So, I want Jboss to NOT load javax.jms so that my application can use the JAR included with the WAR.
So, I was wondering if there was any way to exclude the module globally (for all WAR deployments).
Excluding it per deployment would work too. And I do realize it can be acheivd using jboss-deployment-structure.xml but I can't get it to work.
Here is what I tried:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure
xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclude-subsystems>
<subsystem name="javax" />
<subsystem name="javax.jms" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
and
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure
xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="javax" />
<module name="javax.jms" />
<module name="javax.jms.api" />
</exclusions>
</deployment>
</jboss-deployment-structure>
I placed the file in WEB-INF directory. It didn't work. It still loaded the JMS class from modules folder of Jboss EAP. So, how do I correctly do this?
The correct jboss-deployment-structure.xml is here:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclude-subsystems>
<subsystem name="messaging-activemq"></subsystem>
</exclude-subsystems>
<exclusions>
<module name="javax.jms.api"></module>
</exclusions>
</deployment>
</jboss-deployment-structure>
This way you exclude both messaging subsystem and the JMS api.
You should remove the JMS API JAR from your deployment. You can still keep the JMS implementation JAR in your deployment but that should probably end up in a RAR, preferably outside your deployment.
This link has some things you could try.
Notably:
I think the problem is that activemq-all-5.4.2.jar contains javax.jms.*. Your deployment already gets this implicitly from the javaee.api module (see more information about implicity module dependencies here). I don't think it is appropriate for an application module/jar to package Java EE interfaces. You can try simply deleting the javax directory from activemq-all-5.4.2.jar or using a different set of ActiveMQ jars in your module to limit it to only what you need.
and/or altering your module.xml for ActiveMQ
<module xmlns="urn:jboss:module:1.0" name="activemq">
<resources>
<resource-root path="activemq-all-5.4.2.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
There appears to be a method to embed ActiveMQ in Jboss as well, if you're interested. I won't pull out information from that article, as it doesn't answer the original question.

How to prevent Jboss from using log4j for its logging subsystem?

I have a web application which uses Log4j2 and I am deploying it on JbossEAP 6.0.
However I am using some libraries which still use Log4j. So for this I have added the log4j-1.2 api jar provided for log4j2. The issue I am facing is that my application is logging everything using log4j2 but the libraries are still logging using log4j.
I am guessing that this is because Jboss is adding its own copy of log4j to the classpath. Hence I tried by excluding that module by defining it to be excluded in the jboss-deployment-structure.xml file.
However it is still not working.
Please help.Thanks.
jboss-deployment-structure.xml:
<jboss-deployment-structure>
<deployment>
<!-- Exclusions allow you to prevent the server from automatically adding some dependencies -->
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>

JBoss - How can to exclude javax.validation in jboss-deployment-structure?

I have .war using Jersey REST, and it works in tomCat. But I need to run my .war in JBoss 6.4.0 which causes an exception
java.lang.RuntimeException: java.lang.NoSuchMethodError:
javax.validation.spi.ConfigurationState.getParameterNameProvider()
because JBoss uses old version javax.validation, and I need to exclude javax.validation from deployment of JBoss.
I create jboss-deployment-structure.xml in WEB-INF of .war:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclude-subsystems>
<subsystem name="resteasy" />
<subsystem name="jpa"/>
<subsystem name="org.hibernate" />
<subsystem name="org.hibernate.validator" />
</exclude-subsystems>
<exclusions>
<module name="javaee.api" />
<module name="javax.ws.rs.api"/>
<module name="org.jboss.resteasy.resteasy-jaxrs"/>
<module name="javax.validation.api"/>
<module name="org.hibernate"/>
<module name="org.hibernate.validator"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
This helped me to exclude javax.ws.rs, but How can to exclude javax.validation? Help me, please
Ok, so You need to exclude not only
<module name="javax.validation.api"/>
itself, but also modules that are dependent on javax.validation.api module. The easiest way to see which modules are dependent on javax.validation.api and force it to be included, even though it was excluded, is to search your .xml files in
JBOSS_DIRECTORY/modules for javax.validation.api, the modules that are dependent have something like that in module.xml:
<dependencies>>
<module name="javax.validation.api"/>
...
And those modules need to be excluded as well. For me - I also needed to exclude:
<module name="javax.faces.api"/>
<module name="org.jboss.resteasy.resteasy-hibernatevalidator-provider"/>
And then, javax validation eclusion was working :)
So, it is something! May be help to someone:
Library javax.validation.api in JBoss - belongs to Implicit module, documentation about implicit module: implicit module dependencies
So implicit modules are Automatic Dependencies, and their can exclusion, about this: class lading and automatic dependencies - part about Automatic Dependencies:
Automatic dependencies can be excluded through the use of jboss-deployment-structure.xml. But this is not work! :(, and JBoss has bug with similar library javax.persistence, and it bug open in tasks.
So - what can to do?
Update JBoss to 7.0.0 version, but now just Alpha and Beta versions :(
Replace old javax.validation.api .jar on new version .jar
(in EAP-6.4.0/modules/system/layers/base/javax/faces/api/main)
Add custom version, and it tangled:
change default config in EAP-6.4.0/modules/system/layers/base/javax/faces/api/main module.xml file, in line <module name="javax.validation.api" export="true"/> remove option export="true", result: <module name="javax.validation.api"/> This changed to allow you add a new custom library javax.validation.
And create custom folder with name 1.1 in EAP-6.4.0/modules/system/layers/base/javax/validation/api, put in 1.1 folder new javax.validation .jar and model.xml.
model.xml:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="javax.validation.api" slot="1.1">
<resources>
<resource-root path="validation-api-1.1.0.Final.jar"/>
</resources>
<dependencies>
<module name="org.jboss.logging"/>
</dependencies>
</module>
params:
slot - name custom folder (1.1),
path - path to .jar library
Last: add module to jboss-deployment-structure.xml in project:
<dependencies>
<module name="javax.validation.api" slot="1.1" export="true"/>
</dependencies>

SLF4J logs are not coming in the specific error/trace file. It is coming in server.log

I am programming in Java. I have used executor service. Logs are printing in the specified trace.log file for the main thread. But for the threads created by the executor service, logs are getting printed in the Jboss server.log. How to get these logs in the trace.log instead of in server.log?
I found the concurrency should not affect the functionality of slf4j.
Is SLF4J good to be used in a multithreaded application for logging to a database?
Could somebody please tell what am I missing here?
Thanks in advance.
You have to exclude SLF4J from JBoss. You can do it with jboss-deployment-structure.xml
My jboss-deployment-structure is located in EAR module under resources/META-INF folder and looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
<deployment>
<exclusions>
<module name="org.slf4j" slot="main" />
<module name="org.slf4j.impl" slot="main" />
</exclusions>
</deployment>
<sub-deployment name="some-war.war">
<exclusions>
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
</sub-deployment>
In some-war.war module I can use logback which logs properly into defined files.

Using application's Log4J configuration under JBoss 7.1.1

I'm am having trouble logging using my appenders defined on my XML log4j configuration file.
I created the jboss-deployment-structure.xml on my EAR's META-INF folder with no sucess.
The jboss-deployment-structure.xml structure is:
<jboss-deployment-structure>
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
<deployment>
<exclusions>
<module name="org.apache.log4j" slot="main"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
I have even tried to edit my standalone.conf.bat file adding the following line:
set "JAVA_OPTS=%JAVA_OPTS% -Dorg.jboss.as.logging.per-deployment=false"
My application deployment is like this:
-> MyAppEAR.ear
-> META-INF
-> MANIFEST.MF
-> MyAoo.war
-> META-INF
-> MANIFEST.MF
-> jboss-deployment-structure.xml
-> WEB-INF
-> web.xml
-> lib
-> log4j-1.2.17.jar
-> ---
-> classes
-> log4j.xml
-> ...
I've noticed the following error:
jboss-deployment-structure.xml in subdeployment ignored. jboss-deployment-structure.xml is only parsed for top level deployments.
I even tried migrating from JBOSS 7.1.0 to 7.1.1
Some help please!
Thanks
Thanks for the repply James.
I did what you said and moved the jboss-deployment-structure.xml file to MyAppEAR.ear/META-INF.
I've noticed that this way the exception:
jboss-deployment-structure.xml in subdeployment ignored. jboss-deployment-structure.xml is only parsed for top level deployments.
... doesn't occur. I don't know if that means that the file was parsed... how can I tell?
Despite of this new behaviour my log4j.xml configuration file still isn't loaded and the logger used still is Log4J's.
I know this becaused I wrote to the console:
System.out.println(Logger.getRootLogger().getClass().toString())
...and got:
class org.jboss.logmanager.log4j.BridgeLogger
I've also tried:
moving my log4j.xml to MyAppEAR.ear/META-INF.
removing the unecessary -Dorg.jboss.as.logging.per-deployment=false from standalone.conf.bat
removing slot from my jboss-deployment-structure.xml
Any more ideas?
Thanks
Hi RedEagle see the following configuration which i have tested and its working fine...
Step-1
Create a new module as
jboss-as-7.1.1.Final/modules/com/company/mylog/main/
-module.xml
-log4j-1.2.14.jar
Content of module.xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.company.mylog">
<resources>
<resource-root path="log4j-1.2.14.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
Step-2
Now IN my.ear/META-INF/
-jboss-deployment-structure.xml
-MANIFEST.MF
content of jboss-deployment-structure.xml
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</deployment>
<sub-deployment name="MyWeb.war">
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</sub-deployment>
<sub-deployment name="MyBeans.jar">
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</sub-deployment>
</jboss-deployment-structure>
Content of MANIFEST.MF
Manifest-Version: 1.0
Dependencies: com.company.mylog
Step-3
Content of MyLogger.java
public static Logger getLogger(String name) {
Logger logger= LogManager.getLogger(name);
PropertyConfigurator.configure("log4j.properties"); //Path to log4j.properties as many option available in my case for testing i used static path /home/gyani/log4j.properties
return logger;
}
Step-4
Here is log4j.properties
log4j.rootLogger=info,gyani
log4j.appender.gyani=org.apache.log4j.RollingFileAppender
log4j.appender.gyani.File=/home/gyani/myserverlog.log
log4j.appender.gyani.Append=true
log4j.appender.gyani.MaxFileSize=100000KB
log4j.appender.gyani.MaxBackupIndex=10
log4j.appender.gyani.layout=org.apache.log4j.PatternLayout
log4j.appender.gyani.layout.ConversionPattern=[%d{MMM d HH:mm:ss yyyy}] [%-5p] [%c]: %m%n
Your jboss-deployment-structure.xml needs to be in your MyAppEAR.ear/META-INF directory. There is also no need for the slot attribute for this case.
Also the -Dorg.jboss.as.logging.per-deployment=false isn't used in JBoss AS 7.1.1.Final, but it doesn't hurt anything either.

Categories