HelloWorld in Log4j

Background

As we flesh our understanding and requirements for Log4J, let us ride out a basic HelloWorld application that uses the Log4J library.

 

Lineage

  1. Java:- JDK – Installing Java JDK on MS-Windows
    Link
  2. HelloWorld In Java
    Link
  3. Log4J – Preparation
    Link

Log4J – Library ( Jar ) and Configuration Files

For our beginning Log4J sample application, we need a minimal set of Log4J libraries.

A configuration file will be needed for anything beyond a simple Lab exercise.

Library ( Jar ) Files

Jar File Usage Purpose JAR File Name Syntax JAR File Name ( Version Specific )
Log4J API File Provides the adapter components required for implementers to create a logging implementation. log4j-api-[major]-minor-[subminor].jar log4j-api-2.17.1.jar
Log4J Core File  Core Log4j Implementation classes log4j-core-[major]-minor-[subminor].jar log4j-core-2.17.1.jar
Bridge File Provides Bridge from v1.2 to v2.* log4j-1.2-api-[major]-minor-[subminor].jar log4j-1.2-api-2.17.1.jar

Configuration

File Type File Name
XML Configuration log4j2.xml

 

 

Compilation and Deployment Pre-requisites

Log4J – Library ( Jar )

To compile our source code files ( .java) to class files ( .class), we will need to avail Log4J JAR Files.

Again in our case, we will avail just the minimum required files.

File Layout

Images

 

Source Code


/*
    Import Apache log4j Packages
*/
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class helloLog4j
{

    /*
        Declare Variables
    */   
    static Logger log = null;
   
    public static void main(String[] args)
    {
       
        /*
            Declare Variables
        */
        Exception objExceptionNull = null;

        // configures the root logger
        Logger log = LogManager.getRootLogger();    
    
        objExceptionNull = new NullPointerException("NullError");
        
        
        log.debug("Hello this is a debug message");

        log.info("Hello this is an info message");
        
        log.error
        (
              "Error Message Logged !!!"
            , objExceptionNull
        );
                
    } // function main
   
    
    static void metadata()
    {
       
        /*
            Declare Variables
        */
        String strLog4JVersion = null;
        String FORMAT_LOG4J_VERSION = "Log4j Version Number is %s %n";;

        /*
            How can I check the version number of Log4j in a .jar file?
            Bhushan
            https://stackoverflow.com/questions/70333560/how-can-i-check-the-version-number-of-log4j-in-a-jar-file
        */
        
        /*
            Get log4J Version Number
        */
        strLog4JVersion
            = org.apache.log4j.Layout.class.getPackage().getImplementationVersion();
        
        /*
            If we have log4J Version Number, Log the retrieved version number
        */
        if (strLog4JVersion != null)
        {
                           
            System.out.printf
            (
                  FORMAT_LOG4J_VERSION
                , strLog4JVersion
            );
            
        }
       
   } //metadata
   
   
}

 

Compile

Compiler Tool

JAVAC

Prologue

Sorry, guys no Java IDE.

No, build tool. i.e. Maven.

Nothing but command line.

Compilation Step


setlocal
set "jar_log4j_version=2.17.1"
set "jar_log4j_explicit=lib/log4j/*"
set "jar_log4j_implicit=lib/log4j/log4j-api-%jar_log4j_version%.jar;lib/log4j/log4j-core-%jar_log4j_version%.jar;lib/log4j/log4j-1.2-api-%jar_log4j_version%.jar"
set "jar_log4j=%jar_log4j_implicit%"
javac -cp .;%jar_log4j% helloLog4j.java
endlocal

view raw

compile.cmd

hosted with ❤ by GitHub

Output – Image

 

Invoke

Environment

OS

Prologue

Thank goodness, Java is OS Agnostic.

Invocation Step

 


setlocal
set "jar_log4j_version=2.17.1"
set "jar_log4j_explicit=lib/log4j/*"
set "jar_log4j_implicit=lib/log4j/log4j-api-%jar_log4j_version%.jar;lib/log4j/log4j-core-%jar_log4j_version%.jar;lib/log4j/log4j-1.2-api-%jar_log4j_version%.jar"
set "jar_log4j=%jar_log4j_implicit%"
java -cp .;%jar_log4j% helloLog4j
endlocal

view raw

invoke.cmd

hosted with ❤ by GitHub

Output – Image

 

Source Code Control

GitHub

  1. GitHub
    • DanielAdeniji/helloWorldInLog4J
  2. Gist
    • DanielAdeniji/helloLog4j.java

Summary

As this is a HelloWorld application went with the bare minimum.

Said No:-

  1. IDE
    • No IDE
  2. Build Environment
    • No Build Tool
  3. Environment Customization
    • No Environment Customization
      • CLASSPATH
        • No setting of CLASSPATH thru system variable
        • CLASSPATH passed to javac and java thru CP option
  4. Log4J
    • Configuration File
      • No configuration file ( log4j2.xml )

 

Referenced Work

  1. Stack Overflow
    • Bhushan
      • How can I check the version number of Log4j in a .jar file?
        Link

One thought on “HelloWorld in Log4j

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 )

Facebook photo

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

Connecting to %s