Background
As we flesh our understanding and requirements for Log4J, let us ride out a basic HelloWorld application that uses the Log4J library.
Lineage
- Java:- JDK – Installing Java JDK on MS-Windows
Link - HelloWorld In Java
Link - 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Output – Image
Invoke
Environment
OS
Prologue
Thank goodness, Java is OS Agnostic.
Invocation Step
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Output – Image
Source Code Control
GitHub
Summary
As this is a HelloWorld application went with the bare minimum.
Said No:-
- IDE
- No IDE
- Build Environment
- No Build Tool
- Environment Customization
- No Environment Customization
- CLASSPATH
- No setting of CLASSPATH thru system variable
- CLASSPATH passed to javac and java thru CP option
- CLASSPATH
- No Environment Customization
- Log4J
- Configuration File
- No configuration file ( log4j2.xml )
- Configuration File
Referenced Work
- Stack Overflow
- Bhushan
- How can I check the version number of Log4j in a .jar file?
Link
- How can I check the version number of Log4j in a .jar file?
- Bhushan
[…] HelloWorld In Log4J Link […]