java – Log4j

Background

Playing around with Hibernate got me staring at the screen reviewing console messages; especially SQL statements.

Log4j

Time to implement better logging; mostly instead of having it show up on the console, placing it in log files.

Download

Artifacts

log4j is available here ( https://logging.apache.org/log4j/2.x/download.html ).

Version

The current version is 2.11.2

Download

Please download and extract the files.

I am on Windows and chose apache-log4j-2.11.2-bin.zip.

Project Configuration

Eclipse

This is a simple test and so started a new project.

Project Configuration

In Eclipse, please access the Java Build Path.

Files

  1. log4j-api-2.11.2.jar
  2. log4j-core-2.11.2.jar

Image

Configuration Files

Log4j

Sample

log4j2.property.20190518.0147PM

Java

Outline

  1. Import packages/classes
    • Apache – log4j
      • org.apache.logging.log4j
        • import org.apache.logging.log4j.LogManager;
        • import org.apache.logging.log4j.Logger;
    • Network
      • java.net
        • Import java.net.InetAddress
        • Import java.net.UnknownHostException
  2. Instantiate Logger
    • LogManager.getLogger
  3. Use Logger
    • Invoke Log
      • log.info
      • log.error

Sample


package log4j2;

import java.util.Map;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class hello
{

	private static final Logger log
				= LogManager.getLogger
					(
							log4j2.hello.class
					);

	public static void main(String[] args)
	{

		// TODO Auto-generated method stub
		String computerName = null;

		computerName = getComputerName();

		if (computerName != null)
		{

			computerName = getHostName();

		};

		//computerName = null;

		if (computerName != null)
		{
			log.info("Computer :- " + computerName);
		}
		else
		{

			log.error("Unknown Computer!");

		}

	}

	static private String getHostName()
	{

		String hostname = null;

		try
		{
		    InetAddress addr;
		    addr = InetAddress.getLocalHost();
		    hostname = addr.getHostName();
		}
		catch (UnknownHostException ex)
		{
		    System.out.println("Hostname can not be resolved");
		}

		return (hostname);

	}

	/*
	 *
	 * https://stackoverflow.com/questions/7883542/getting-the-computer-name-in-java
	 */
	static private String getComputerName()
	{
	    Map env = System.getenv();
	    if (env.containsKey("COMPUTERNAME"))
	        return env.get("COMPUTERNAME");
	    else if (env.containsKey("HOSTNAME"))
	        return env.get("HOSTNAME");
	    else
	        return null;
	}

}

Output

Source Control

Git

log4j.2.sample
Link

Summary

A couple of quick points.

A lot of the Log4j sample configuration files available on the Internet are based on the original implementation.

The current version of Log4j is version 2, Log4j2.

Its XML Schema is quite different than Version 1.

Please look for samples that explicitly mention and reference, Log4j2.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s