Apache Commons Logging

Last updated
Apache Commons Logging
Developer(s) Apache Software Foundation
Stable release
1.2 / July 2014;9 years ago (2014-07)
Repository github.com/apache/commons-logging
Written in Java
Operating system Cross-platform
Type Logging Tool
License Apache License 2.0
Website commons.apache.org/proper/commons-logging/

Apache Commons Logging (previously known as Jakarta Commons Logging or JCL) is a Java-based logging utility and a programming model for logging and for other toolkits. It provides APIs, log implementations, and wrapper implementations over some other tools. [1] [2] [3]

Contents

Log level

The following table defines the log levels and messages in Apache Commons Logging, in decreasing order of severity. The left column lists the log level designation in and the right column provides a brief description of each log level.

LevelDescription
fatalSevere errors that cause premature termination. Expect these to be immediately visible on a status console.
errorOther runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
warnUse of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
infoInteresting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
debugDetailed information on the flow through the system. Expect these to be written to logs only.
traceMost detailed information. Expect these to be written to logs only.

[3] [4]

Configuration

Two basic abstractions, Log and LogFactory, are used in Apache Commons Logging. [3]

Example

Sample code may look like as follows:

packagecom.cascadetg.ch09;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.apache.commons.logging.impl.Jdk14Logger;publicclassLogGenerator{// Note that you pass in an instance of this class to the// log generator. This allows you to find the messages// generated by this class.privatestaticLoglog=LogFactory.getLog(LogGenerator.class);publicstaticvoidconfigJDKLogger(){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(Exceptione){System.out.println("Unable to load JDK 1.4 logging.");e.printStackTrace();}}publicstaticvoidmain(String[]args){configJDKLogger();System.setErr(System.out);System.out.println();System.out.println("Test fatal log");try{Stringfoo=null;intx=0/(newInteger(foo)).intValue();}catch(Exceptione){log.fatal(e.getMessage(),e);}System.out.println();System.out.println("Test error log");try{Objectfoo=null;foo.toString();}catch(Exceptione){log.error(e.getMessage(),e);}System.out.println();System.out.println("Test warn log");try{Class.forName("com.cascadetg.NonexistantClass");}catch(Exceptione){log.warn("Can't find a non-existent class!");}System.out.println();System.out.println("Test info log");log.info("Starting app!");log.info("Quitting app!");System.out.println();System.out.println("Test debug log");if(1>2){log.debug("1 > 2 evaluated true");if(10%2==0)log.debug("10 % 2 is 0");elselog.debug("10 % 2 is not 0");}else{log.debug("1 > 2 evaluated false");}System.out.println();System.out.println("Test trace log");log.trace("Calling trace method.");log.trace("Calling trace method.");log.trace("Calling trace method.");log.trace("Calling trace method.");log.trace("Calling trace method.");System.out.println();System.out.println("Log test complete.");}}

[4]

See also

Related Research Articles

Java Platform, Standard Edition is a computing platform for development and deployment of portable code for desktop and server environments. Java SE was formerly known as Java 2 Platform, Standard Edition (J2SE).

In computing, the Java Remote Method Invocation is a Java API that performs remote method invocation, the object-oriented equivalent of remote procedure calls (RPC), with support for direct transfer of serialized Java classes and distributed garbage-collection.

In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior.

In software design and engineering, the observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this capability.

This article compares two programming languages: C# with Java. While the focus of this article is mainly the languages and their features, such a comparison will necessarily also consider some features of platforms and libraries. For a more detailed comparison of the platforms, see Comparison of the Java and .NET platforms.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

TestNG is a testing framework for the Java programming language created by Cédric Beust and inspired by JUnit and NUnit. The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-end, integration, etc., with more powerful and easy-to-use functionalities.

Apache Harmony is a retired open source, free Java implementation, developed by the Apache Software Foundation. It was announced in early May 2005 and on October 25, 2006, the board of directors voted to make Apache Harmony a top-level project. The Harmony project achieved 99% completeness for J2SE 5.0, and 97% for Java SE 6. The Android operating system has historically been a major user of Harmony, although since Android Nougat it increasingly relies on OpenJDK libraries.

<span class="mw-page-title-main">Scala (programming language)</span> General-purpose programming language

Scala is a strong statically typed high-level general-purpose programming language that supports both object-oriented programming and functional programming. Designed to be concise, many of Scala's design decisions are intended to address criticisms of Java.

<span class="mw-page-title-main">Log4j</span> Java-based logging software

Apache Log4j is a Java-based logging utility originally written by Ceki Gülcü. It is part of the Apache Logging Services, a project of the Apache Software Foundation. Log4j is one of several Java logging frameworks.

Exception handling syntax is the set of keywords and/or structures provided by a computer programming language to allow exception handling, which separates the handling of errors that arise during a program's operation from its ordinary processes. Syntax for exception handling varies between programming languages, partly to cover semantic differences but largely to fit into each language's overall syntactic structure. Some languages do not call the relevant concept "exception handling"; others may not have direct facilities for it, but can still provide means to implement it.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

A Java logging framework is a computer data logging package for the Java platform. This article covers general purpose logging frameworks.

Jakarta Mail is a Jakarta EE API used to send and receive email via SMTP, POP3 and IMAP. Jakarta Mail is built into the Jakarta EE platform, but also provides an optional package for use in Java SE.

The Java programming language and the Java virtual machine (JVM) is designed to support concurrent programming. All execution takes place in the context of threads. Objects and resources can be accessed by many separate threads. Each thread has its own path of execution, but can potentially access any object in the program. The programmer must ensure read and write access to objects is properly coordinated between threads. Thread synchronization ensures that objects are modified by only one thread at a time and prevents threads from accessing partially updated objects during modification by another thread. The Java language has built-in constructs to support this coordination.

Generics are a facility of generic programming that were added to the Java programming language in 2004 within version J2SE 5.0. They were designed to extend Java's type system to allow "a type or method to operate on objects of various types while providing compile-time type safety". The aspect compile-time type safety was not fully achieved, since it was shown in 2016 that it is not guaranteed in all cases.

<span class="mw-page-title-main">SLF4J</span>

Simple Logging Facade for Java (SLF4J) provides a Java logging API by means of a simple facade pattern. The underlying logging backend is determined at runtime by adding the desired binding to the classpath and may be the standard Sun Java logging package java.util.logging, Log4j, Reload4j, Logback or tinylog.

The Java Development Kit (JDK) is a distribution of Java technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java Application Programming Interface (API). It is derivative of the community driven OpenJDK which Oracle stewards. It provides software for working with Java applications. Examples of included software are the Java virtual machine, a compiler, performance monitoring tools, a debugger, and other utilities that Oracle considers useful for Java programmers.

Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation. It provides methods to query and update data in a database, and is oriented toward relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.

References

  1. "commons logging". Apache.org. Apache. Retrieved 12 February 2016.
  2. Zavala, D.A.; Lau, Y.C. (2004). Integrating Jakarta Commons Logging with IBM WebSphere Application Server V5. IBM corporation. p. 2.
  3. 1 2 3 "contents". Apache.org. Apache. Retrieved 12 February 2016.
  4. 1 2 Iverson, W. (2005). Apache Jakarta Commons - Reusable Java Components. Crawfordsville, Indiana, USA: Pearson Education, Inc. pp. 120–122.