Apache Commons Logging
| Company type | Software framework |
|---|---|
| Industry | Software development |
| Founder | Apache Software Foundation |
| Products | Java logging framework and programming interface |
| Owner | Apache Software Foundation |
| Website | https://commons.apache.org/proper/commons-logging/ |
Apache Commons Logging—previously known as Jakarta Commons Logging or JCL—is a logging framework and programming interface for Java, created by the Apache Software Foundation. It provides APIs, various logging implementations, and adapter wrappers that facilitate seamless integration with multiple logging systems within Java applications.
Log level
The following table presents the log levels defined by Apache Commons Logging, arranged from highest to lowest severity:
| Level | Explanation |
|---|---|
| fatal | Critical errors that cause immediate shutdown; these are prominently displayed on status consoles. |
| error | Runtime exceptions or unforeseen conditions; also shown on status consoles. |
| warn | Usage of deprecated APIs, improper API utilization, or runtime events that are undesirable but not critical; visible on status consoles. |
| info | Significant runtime occurrences such as application startup or shutdown; visible on consoles but should be kept minimal. |
| debug | Comprehensive details about system operations; generally recorded in logs only. |
| trace | The most granular level of information; logged exclusively. |
Configuration
The architecture of Apache Commons Logging revolves around two primary abstractions: Log and LogFactory.
Example
Below is an example in Java illustrating a common usage pattern:
<syntaxhighlight lang="Java"> package com.cascadetg.ch09;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.impl.Jdk14Logger;
public class LogGenerator {
private static Log log = LogFactory.getLog(LogGenerator.class);
public static void configureJDKLogger()
{
try
{
((Jdk14Logger)log).getLogger().setLevel(java.util.logging.Level.ALL);
((Jdk14Logger)log).getLogger().addHandler(
(java.util.logging.FileHandler)Class.forName("java.util.logging.FileHandler").newInstance());
System.out.println("Added JDK 1.4 file handler");
} catch (Exception e)
{
System.out.println("Failed to load JDK 1.4 logging.");
e.printStackTrace();
}
}
public static void main(String[] args)
{
configureJDKLogger();
System.setErr(System.out);
System.out.println("\nTesting fatal log");
try
{
String foo = null;
int x = 0 / (new Integer(foo)).intValue();
} catch (Exception e)
{
log.fatal(e.getMessage(), e);
}
System.out.println("\nTesting error log");
try
{
Object foo = null;
foo.toString();
} catch (Exception e)
{
log.error(e.getMessage(), e);
}
System.out.println("\nTesting warn log");
try
{
Class.forName("com.cascadetg.NonexistentClass");
} catch (Exception e)
{
log.warn("Unable to locate nonexistent class!");
}
System.out.println("\nTesting info log");
log.info("Application starting!");
log.info("Application shutting down!");
System.out.println("\nTesting debug log");
if (1 > 2)
{
log.debug("Condition 1 > 2 evaluated as true");
if (10 % 2 == 0)
log.debug("10 modulo 2 equals 0");
else
log.debug("10 modulo 2 does not equal 0");
} else
{
log.debug("Condition 1 > 2 evaluated as false");
}
System.out.println("\nTesting trace log");
for (int i = 0; i < 5; i++)
log.trace("Executing trace method.");
System.out.println("\nLogging test completed.");
}
} </syntaxhighlight>
See also
- log4j
- Chainsaw (log viewer)