Camel log component with log4j2 - spring-boot

I have a spring-boot application with apache camel and log4j2 as the underlying log provider. Is it possible to use camel log component to write to the log files as configured by the log4j2.xml?
Below jar files are there in the classpath:-
log4j-over-slf4j-1.7.16.jar
logback-classic-1.1.5.jar
logback-core-1.1.5.jar
log4j-api-2.4.1.jar
log4j-core-2.4.1.jar
jcl-over-slf4j-1.7.16.jar
jul-to-slf4j-1.7.16.jar
slf4j-api-1.7.16.jar

You currently have two logger implementations configured: Log4j 2 and Logback. If you want to do all your logging with Log4j2 then you need to add the Log4j SLF4J bridge and remove the Logback jars. I also recommend you use the latest Log4j 2 version (2.6.1). That gives the following dependencies:
log4j-api-2.6.1.jar
log4j-core-2.6.1.jar
log4j-slf4j-impl-2.6.1.jar (routes SLF4J API calls to Log4j 2)
log4j-jcl-2.6.1.jar (routes Commons Logging API calls to Log4j 2)
log4j-jul-2.6.1.jar (also set system property java.util.logging.manager to org.apache.logging.log4j.jul.LogManager)
slf4j-api-1.7.16.jar
You don't need:
log4j-over-slf4j-1.7.16.jar
logback-classic-1.1.5.jar
logback-core-1.1.5.jar
jcl-over-slf4j-1.7.16.jar
jul-to-slf4j-1.7.16.jar
About configuration, by default Log4j2 looks for a file called log4j2.xml in the classpath, but you can also specify the location with a system property.
The Log4j 2 manual is much better than the Log4j 1 documentation and shows many example configurations.

Related

Log4j2 configuration location with JVM arguments for Web application that uses Spring Framework 5.2

I am upgrading log4j 1.2 to log4j 2.17.1 for a Spring Web application Servlet - 3.1 and Spring 5.2.2
1- The log4j2.xml file with all the configurations for Appenders etc is in a file outside the classpath(external file in the deployed server) - example - /tomcat/conf/log4j2.xml
Previously log4j was configured using this following JVM argument -
-Dlog4j.configuration = file:/opt/tomcat/conf/log4j.xml
I am just gonna change this to -
-Dlog4j.configuration = file:/opt/tomcat/conf/log4j2.xml
Question is, is this change enough for the new log4j2.xml to be used now. All the other stuff in the project is setup.
Do I need any more changes in web.xml etc, If I am already using this JVM argument ?

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.

Gradle and slf4j / logback

Is it possible to redirect Gradle logs to slf4j/logback.
I know you can use a custom SLF4J logger https://docs.gradle.org/current/userguide/logging.html#sec:sending_your_own_log_messages, but where should the logback.properties should sit?
Also, besides your messages can you redirect all Gradle messages?
You can see the rules for loading config in the logback documentation here https://logback.qos.ch/manual/configuration.html
Specifically:
Logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, logback tries to find a file called logback.groovy in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath..

spring-boot - External log4j configuration not working

I am trying to do a setup for production environment, so I wanted to keep log4j.properties out of my application jar file.
For that I have kept my application.properties and log4j.properties files parallel to my spring-boot jar file. And I have given logging.config: file:log4j.properties in my application.properties. This doesn't seem to work. Spring is still picking up the log4j.properties file which is placed inside the jar file.
I have tried giving the absolute path in logging.config property like C:\Users\furquan\project\jars\log4j.properties, but it still the log4j.properties that was being used was the one inside the jar file.
Please help, its important !!!
-Dlogging.config=/path/to/log4j.properties
Or you can specify it in your application.properties or bootstrap.properties.
From 26.5 Custom log configuration
And just a note:
After spring boot 1.4, Log4j 1 support has been removed. So maybe you can consider use the default logback, or use log4j2 instead.

Springboot externalizing log4j configuration

In a springboot application, I have a single jar, and then a subdirectory config with application.properties, applicationContext.xml, and log4j...properties file.
I am trying to externalize the log4j config. The application.properties is externalized this way.
But, when springboot runs it is using the log4j config file from within the jar file. By using the -Dlog4j.debug option I can see that log4j uses my external file first, but when spring starts it overrides the log4j setting with the one in the jar.
here is an example startup (with all options)
java -Dlog4j.debug
-Dlogging.config="file:/opt/config/log4j-qa.properties"
-Dlog4j.configuration="file:/opt/config/log4j-qa.properties"
-jar /opt/myjarName.jar
--spring.config.location=/opt/config/
on first startup log4j states
log4j: Reading configuration from URL file:/opt/config/log4j-qa.properties
then on springboot start
log4j: Reading configuration from URL jar:file:/opt/dms-events-api.jar!/log4j-qa.properties
but I want it to read only the external file file:/opt/config/log4j-qa.properties
resolution:
In our application we had line
#ImportResource("classpath:applicationContext.xml")
which then defined the log4j properties file from the classpath:
the simple solution
1. create a /config directory at the root of the api application and put the properties files there
2. remove the ImportResource line, it isn't needed now
3. add a line to the the application.properties file
logging.config=file:config/log4j-${our environment var}.properties
the explanation
By creating a /config directory at the root of the project then
we can work in eclipse as usual and find our properties files.
--and then to externalize configs
simply add a config directory off of where the application jar is and put properties files there.
Two problems are there:
Configuration for externalise: - Tried and works below one for me in spring boot jar
-Dlog4j.configuration=file:/Users/test/Any-Folder/log4j.properties
Spring logging takes over - for that you need to exclude the logging module. PFB the config for Gradle build.
configurations {
all*.exclude module : 'spring-boot-starter-logging'
}

Resources