How to configure the logging framework in Spring-Boot? - java

How can I tell Spring-Boot which logging framework to use? (I want to use log4j2).
In 1.1.9.RELEASE I just had a log4j2.xml in my classpath and the logging worked.
Now I upgraded to spring-boot-1.2.0.RELEASE, and my loggers do not work anymore! Maybe I have to configure the logging framework to be used explicit?
I'm using org.apache.logging.log4j.LogManager.getRootLogger() for logging, maybe this is wrong?

See the docs: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging
Just change the POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

You should be using SFL4J which is a level of abstraction around the actual logging implementation used. if you configure your POM as above, and include the appropriate log4j configuration in src/main/resources, and log using a org.slf4j.Logger then you should be fine

Related

Maven use compile dependency instead of updates

I am not sure if I am framing this correctly
I am using following spring-boot-starter-data-elasticsearch. As you can see the version is 2.5.7.
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.5.7</version>
</dependency>
As you can see there are 2 compile dependencies. One is spring-boot-starter and another is spring-data-elastic-search. For spring-data-elastic-search compile dependency, I want to use 4.2.7 instead of updated version 4.4.2. But, no matter what I do it always picks up version 4.4.2
Is there a way to use the version needed instead of the updated version? Same with internal dependency.
For example since spring-data-elastic-search is 4.4.2, I want internal dependency like rest-high-level-client to follow the actual version instead of updated version.
You should:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.7</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>4.2.7</version>
</dependency>

Project inside SpringBoot 2.6.0 working, forced to use Log4J1 and JCL redirect working but JUL not

I have a project whereby the latest build is now SpringBoot 2.6.0 (and older one is a WAR for Tomcat 8). I have a sub-project/component that connects to stuff that forces me to use Log4J1. Logback was working beautifully but I've had to disable it (due to Log4J2 eclipsing and breaking the code that requires Log4J1 and also a new vendor component requiring Log4J2 as well).
I've:
disabled Logback by excluding spring-boot-starter-logging
Added a dependency for spring-boot-starter-log4j
Via the Dependency hierarchy tab (in Eclipse) I can see the jcl and jul libraries and in my logs (Console and File appenders) it is clear that SpringBoot (jcl stuff) and any of my project jcl calls are logged, but any JUL are not.
I've read tons of different tickets on here, gleaned together the info and got to this point. So it's all good except I cannot seem to pipe the JUL calls to the Log4J1 setup.
I can see jul-to-slf4j brought in via spring-boot-starter-log4j.
I thought I'd write here as I'm sure it's something simple I've missed but I think I've been staring at it for too long this afternoon! :)
What have I missed?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.32</version>
</dependency>
I got the JUL bits piping via adding this:
SLF4JBridgeHandler.install();
java.util.logging.LogManager.getLogManager().getLogger("").setLevel( Level.INFO);
Which I found on question JUL to SLF4J Bridge

How to use spring-jdbc in Quarkus?

Could you please let me know how to use spring-jdbc in Quarkus, as I am converting my application from spring to Quarkus, for now I required to use JdbcTemplate but I don't see how to use it.
I am using below dependencies:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-spring-web</artifactId>
</dependency>
But I didn't find anything for spring-jdbc
There isn't such thing as JdbcTemplate in Quarkus, nor a support for spring-jdbc.
So the answer is that you cannot use them, you need to convert the usage to Spring Data (or HIbernate with Panache), or inject a DataSource object and directly work with it.
We found that past version works perfectly fine with native compilation. Hope that's good enough in your case.
Replacing logging is also required for native compilation, because of Class.forName usages.
<spring.jdbc.version>4.3.30.RELEASE</spring.jdbc.version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.jdbc.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>commons-logging-jboss-logging</artifactId>
</dependency>

Is there a way in POM to specify a higher version for dependent package?

I am using Maven to set up dependency in my app.
I am using Spring Boot v2.1.12.RELEASE which brings in Spring Core v5.1.13.
But there also a library Spring Integration v5.1.9 (which is latest) and brings Spring Core v5.1.11.RELEASE
As you can see that I want Spring Integration to not resolve to v5.1.11 of Spring Core as it has some vulnerabilities.
Is there any way to specify in POM for Spring Integration to resolve to 5.1.13 of Spring Core (instead of 5.1.11) ?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>5.1.9</version>
</dependency>
P.S I do not want to upgrade to the latest release of Spring Boot.
Use maven exclusion tag to exclude the transitive dependency, make sure the excluded library is directly added to pom or it's pulled in by some other dependency.
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>5.1.9</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.11.RELEASE</version>
</exclusion>
</exclusions>
</dependency>
DISCLAIMER: This is just a work around solution for your immediate need, use it only when no other options are possible as managing spring managed dependencies ourself is not maintainable in long run.
I used the recommendation in the post Dependency Management to overcome my challenge.
So I excluded the spring-core dependency from spring integration and also added the spring core library using below code
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.13.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>

why i need to exclude "spring-boot-starter-logging" to be able to use spring-geode?

to be able to use spring-geode, i need to exclude starter-logging, otherwise i run into:
Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLogger cannot be cast to org.apache.logging.log4j.core.Logger
at org.apache.geode.internal.logging.LogService.init(LogService.java:81) ~[geode-core-1.2.1.jar:na]
at org.apache.geode.internal.logging.LogService.<clinit>(LogService.java:72) ~[geode-core-1.2.1.jar:na]
... 41 common frames omitted
my POM now looks like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.geode</groupId>
<artifactId>spring-geode-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-geode</artifactId>
<version>2.0.10.RELEASE</version>
</dependency>
what should i do?
Geode introduces logging dependencies (log4j) which collide with spring logging:
https://mvnrepository.com/artifact/org.apache.geode/geode-core/1.7.0
Please see https://lists.apache.org/thread.html/311d0e4bf742a2477f337f1028bdf991d16560c1bb0e7cd0f0ea9c1d#<user.geode.apache.org>
Spring uses Logback by default. You'll need to change the logging framework to log4j https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging

Categories

Resources