Sunday, August 14, 2016

Java Util Logging (jul)

Java Util Logging(jul) is the logging tool which is naturally supported by JRE. It's out of box feature of JRE. So it's very convenient for those applications don't care logging records.
  • JUL API and implementation are inside Java SE API
  • JUL default configuration is inside JRE

Sample code


import java.util.logging.Logger;

public class Demo {
   
    private static final Logger LOGGER = Logger.getLogger(Demo.class.getName());
   
    public static void main(String[] args){
        LOGGER.info("Current Logger = " + LOGGER.getClass().getName());
       
        LOGGER.finest("finest log");
        LOGGER.finer("finer log");
        LOGGER.fine("fine log");
        LOGGER.config("config log");
        LOGGER.info("info log");
        LOGGER.warning("warning log");
        LOGGER.severe("severe log");
    }
   
}

You can directly execute this segment of code.

Default configuration

JUL default configuration adopted by JRE is /$JAVA_HOME/jre/lib/logging.properties. It output messages to console and only output INFO and above levels messages.

Using your own configuration

Create a properties file, named logging.properties

handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=FINEST

java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=%h/jul%u.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

Pass this configuration file by jvm system property option, -Djava.util.logging.config.file=/<your_dir>/logging.properties.

Saturday, July 23, 2016

Springframework 1 - How to unify springframework JARs' version in your project?

It is possible to accidentally mix different versions of spring JARs when using maven. For example, you may find that a third-party library, or another spring project, pulls in a transitive dependency to an older release. If you forget to explicitly declare a direct dependency yourself, all sorts of unexpected issues can arise.

To overcome such problems maven supports the concept a "Bill of Materials" dependency or "BOM" dependency. You can import the spring-framework-pom in your dependencyManagement section to ensure that all spring dependencies (both direct and transitive) are at the same version.
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

After using this BOM you can find a spring-framework-bom-4.3.1.RELEASE.pom inside directory, .m2/repository/org/springframework/spring-framework-bom/4.3.1.RELEASE. This file contains all springframework components of this version.

What does 'import' scope of dependency tag mean?
  • import (only available in Maven 2.0.9 or later)
    This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

You can add this BOM into parent pom to unify all sub-projects. I added it for my project I'm creating for fun.

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
   
    <groupId>com.martian.apps.springlab</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>parent</name>
    <description>Springframework Laboratory :: parent pom</description>

    <modules></modules>

    <properties>
        <springframework.version>4.3.1.RELEASE</springframework.version>
        <slf4j.version>1.7.12</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <!-- jstl -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${springframework.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


            <!-- log -->
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jul-to-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>log4j-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>apache-log4j-extras</artifactId>
                <version>${log4j.version}</version>
            </dependency>
           
           
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
   
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>