Log4JLogger Logger and log4j.properties File

Q

How to use Log4JLogger Logger and log4j.properties File?

✍: FYIcenter.com

A

The suggested way to use the Log4JLogger Logger and log4j.properties File is to do the following:

1.Create commons-logging.properties file with one entry:

# commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

2.Create log4j.properties file:

# log4j.properties
# Copyright (c) 2016 FYIcenter.com

# Set log level and define my appender
log4j.rootLogger=TRACE, MyConsole

# Configure my appender 
log4j.appender.MyConsole=org.apache.log4j.ConsoleAppender
log4j.appender.MyConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.MyConsole.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n

3. Download and install Log4j JAR at C:\local\log4j-1.2.17\log4j-1.2.17.jar. Add Log4j JAR to the JVM class path.

4. Run the HelloCommonsLogging example code with both properties files in the current folder, which is included in the class path:

C:\fyicenter>java -cp .;C:\local\commons-logging-1.2\commons-logging-1.2.jar;
   C:\local\log4j-1.2.17\log4j-1.2.17.jar
   HelloCommonsLogging
   
Log class: org.apache.commons.logging.impl.Log4JLogger
0    FATAL [main]                             MyLogging     - Hello - fatal
16   ERROR [main]                             MyLogging     - Hello - error
16   WARN  [main]                             MyLogging     - Hello - warn
16   INFO  [main]                             MyLogging     - Hello - info
16   DEBUG [main]                             MyLogging     - Hello - debug
16   TRACE [main]                             MyLogging     - Hello - trace

 

FAQ for Apache commons-logging.jar

Jdk14Logger Logger and logging.properties File

Using commons-logging.jar in Java Code

⇑⇑ FAQ for Apache commons-logging.jar

2016-11-16, 898👍, 0💬