Apache Commons Logging + Log4J + Maven Hello World!

Log4J scope: runtime!

Directory Layout

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>log4pro</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>log4pro</name>
    <url>http://www.tugay.biz</url>
 
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>log4pro</finalName>
    </build>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

log4j.properties
# suppress inspection "UnusedProperty" for whole file
 
# logger level configurations
log4j.rootLogger=WARN, console
log4j.logger.biz.tugay.log4pro.FooBar=INFO
 
# configuration for console outputs
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=[%t] %-5p %c - %m%n

FooBar.java
package biz.tugay.log4pro;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 12/8/2016
 * Time: 18:07 PM
 */
public class FooBar {
 
    final Log logger = LogFactory.getLog(FooBar.class);
 
    public FooBar() {
        logger.info("FooBar object created!");
    }
}

TestClass.java
package biz.tugay.log4pro;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 12/8/2016
 * Time: 18:09 PM
 */
public class TestClass {
 
    final static Log logger = LogFactory.getLog(TestClass.class);
 
    public static void main(String[] args) {
        logger.warn("This will be printed!");
        logger.info("This will not be printed!");
        new FooBar();
    }
}

..so?

It seems like the logging implementation we provide to runtime will automatically be discovered and used by Commons Logging! Great!