Java logging framework: Difference between revisions

Content deleted Content added
copy-edits
Line 10:
 
== Functionality overview ==
Logging a message is broken into three major pieces: the Logger, Formatter and the AppenderHandler (HandlerAppender). The Logger is responsible for capturing the message to be logged, along with certain meta-data like level,metadata and passing thatit to the logging framework. After receiving the message, the logging framework calls the Formatter onwith the message. The Formatter accepts anthe message object and formats it for proper loggingoutput. The logging framework then hands the formatted message to the appropriate Appender for disposition of the message. This might include displaying on a console display, writing to disk, appending to a database, or notification via email.
 
Simpler logging frameworks, like Java Logging Framework by the Object Guy, combine the logger and the appender together. This makessimplifies fordefault simpleoperation, initialbut configuration,it butis less configurable, especially asif the project is moved across environments.
Logging a message is broken into three major pieces: the Logger, Formatter and the Appender (Handler). The Logger is responsible for capturing the message to be logged, along with certain meta-data like level, and passing that to the logging framework. After receiving the message, the logging framework calls the Formatter on the message. The Formatter accepts an object and formats it for proper logging. The logging framework then hands the formatted message to the appropriate Appender for disposition of the message. This might include displaying on a console, writing to disk, appending to a database, or notification via email.
 
Simpler logging frameworks, like Java Logging Framework by the Object Guy, combine the logger and the appender together. This makes for simple initial configuration, but less configurable, especially as the project is moved across environments.
 
=== Logger ===
Most frameworks support the notion of a Logger. A Logger is an object that allows the application to log data without regard to where the dataoutput is actually loggedsent/stored. The application logs a message inby the form ofpassing an object or an object and an [[exception.]] with Whenan aoptional Loggerseverity islevel created,to itthe islogger givenobject a name or an identifier. When loggingunder a message, it is logged atgiven a certain level or priorityname/identifier.
 
Most frameworks support the notion of a Logger. A Logger is an object that allows the application to log data without regard to where the data is actually logged. The application logs a message in the form of an object or an object and exception. When a Logger is created, it is given a name or an identifier. When logging a message, it is logged at a certain level or priority.
 
==== Name ====
A logger has a name. The name is usually hierarchicalstructured hierarchically, with periods (.) separating the levels. A common naming scheme is to use the name of the class or package that is doing the loggingslogging. Both [[log4j]] and the Java logging [[API]] supportedsupport defining Handlers higher up the hierarchy.
 
A logger has a name. The name is usually hierarchical, with periods (.) separating the levels. A common naming scheme is to use the name of the class or package that is doing the loggings. Both [[log4j]] and the Java API supported defining Handlers higher up the hierarchy.
 
For example, the logger might be named "<code>com.sun.some.UsefulClass</code>". The handler can be defined for any of the following:
Line 30 ⟶ 27:
* <code>com.sun.some.UsefulClass</code>
 
==== LevelSeverity level ====
The message is logged at a certain level. The commonCommon levels are copied from [http://commons.apache.org/logging/guide.html#Message%20Priorities/Levelss Apache Commons Logging]:
 
The message is logged at a certain level. The common levels are copied from [http://commons.apache.org/logging/guide.html#Message%20Priorities/Levelss Apache Commons Logging]:
 
{| class="wikitable"
Line 46 ⟶ 42:
|-
|'''WARNING'''
|Use of deprecated APIs, poor use of API, 'almost'near errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
|-
|'''INFO'''
Line 60 ⟶ 56:
 
=== Formatters or renderers ===
A Formatter is an object that formats a given object for logging by the Appender. Mostly this consists of taking the binary object and converting it to a string representation.
 
A Formatter is an object that formats a given object for logging by the Appender. Mostly this consists of taking the object and converting it to a string representation.
 
=== Appenders or handlers ===
The appenders are configured toAppenders listen for messages ofat or above a certainspecified minimum logseverity level or above. The Appender takes the message it is passed and disposesposts ofit the messagesappropriately. Some messageMessage dispositions include:
 
The appenders are configured to listen for messages of a certain log level or above. The Appender takes the message it is passed and disposes of the messages. Some message dispositions include:
 
* display on the console
* write to a file or syslog
* append to a database table
* distribute via JMSJava Messaging Services
* send via email
* write to a listening socket
* discard to the "bit-bucket" (/dev/null)
 
=== Comparison of features ===
 
=== Feature comparison ===
{| class="wikitable"
|+ '''Features'''
Line 93 ⟶ 86:
! [http://java.sun.com/javase/6/docs/technotes/guides/logging/ Java Logging API]
| SEVERE WARNING INFO CONFIG FINE FINER FINEST
| Depends on the underlying framework; Default Sun's JVMdefault implementationJava Virtual Machine (JVM) has the following: ConsoleHandler, FileHandler, SocketHandler, MemoryHandler
| Not widely used{{Citation needed|date=August 2009}}
| Comes with the JRE
Line 100 ⟶ 93:
| FATAL ERROR WARN INFO DEBUG TRACE
| Depends on the underlying framework
| UsedWidely by manyused, in conjunction with log4j
| Apache License, Version 2.0
|-