How to configure log4j2 to log for HikariCP

10.7k views Asked by At

I have written a small Gradle project so that I can learn how to configure HikariCP and JDBC and Log4j2 all together in the same project. I have placed the below log4j2 config in the src/main/resources directory in my project. When I execute the project using gradle run I get a warning about not configuring an appender for the com.zaxxer.hikari.HikariConfig logger.

Would some one kindly tell me what i am doing wrong?

log4j2.xml

<?xml version="1.0" encoding="utf-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="com.zaxxer.hikari.HikariConfig" level="trace">
      <AppenderRef ref="Console"/>
    </Logger>
    <Root level="TRACE">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'application'

targetCompatibility = 1.8
sourceCompatibility = 1.8
version = '0.0.1.0'
group = 'com.s2d'

mainClassName = "com.s2d.jdbctest.DemoJDBC"
applicationDefaultJvmArgs = ["-Djdbc.drivers=org.apache.derby.jdbc.EmbeddedDriver"]

task wrapper(type: Wrapper) {
  gradleVersion = '1.4'
}

repositories {
  mavenCentral()
}

dependencies {
  compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.0.2'
  compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.0.2'
  compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.10'
  compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.10'
  compile group: 'com.zaxxer', name: 'HikariCP', version: '2.3.7'
  runtime group: 'org.apache.derby', name: 'derby', version: '10.11.1.1'
  testCompile group: 'junit', name: 'junit', version: '4.11'
}

test.scanForTestClasses(false)

javadoc.options.links("http://docs.oracle.com/javase/7/docs/api/")

task sourcesJar(type: Jar, dependsOn: classes) {
  classifier = 'sources'
  from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
  classifier = 'javadoc'
  from javadoc.destinationDir
}

artifacts {
  archives sourcesJar
  archives javadocJar
}

Execution output

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      run
[sts] -----------------------------------------------------
:compileJava UP-TO-DATE
:processResources
:classes
:run
20:27:11.247 [main] INFO  com.s2d.jdbctest.DemoJDBC - Starting demo
log4j:WARN No appenders could be found for logger (com.zaxxer.hikari.HikariConfig).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Column Count: 1
Column 1 name: 1 type: DECIMAL
Inserted ID: 1005
20:27:12.350 [main] INFO  com.s2d.jdbctest.DemoJDBC - Demo complete

BUILD SUCCESSFUL

Total time: 2.017 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 2 sec
[sts] -----------------------------------------------------
1

There are 1 answers

0
JB Nizet On BEST ANSWER

You're using slf4j-log4j12, which allows using slf4j to log messages with log4j 1.2. But your goal is to log messages using slf4j over log4j 2.0.2. So you need another binding library: https://logging.apache.org/log4j/2.0/log4j-slf4j-impl/index.html.

The Maven coordinates of this binding library are in the log4j2 documentation.