Content deleted Content added
m →External links: Remove blank line(s) between list items per WP:LISTGAP to fix an accessibility issue for users of screen readers. Do WP:GENFIXES and cleanup if needed. Discuss this at Wikipedia talk:WikiProject Accessibility/LISTGAP |
GreenC bot (talk | contribs) Rescued 1 archive link; reformat 1 link. Wayback Medic 2.5 per Category:All articles with dead external links - pass 3 |
||
(34 intermediate revisions by 18 users not shown) | |||
Line 4:
}}
A '''Java logging framework''' is a [[computer data logging]] package for the [[Java platform]]. This article covers general purpose logging frameworks.
Logging refers to the recording of activity
Unfortunately the [[Java Development Kit|JDK]] did not include logging in its original release so by the time the Java Logging API was added several other logging frameworks had become widely used – in particular [http://commons.apache.org/logging Apache Commons Logging] (also known as Java Commons Logging or JCL) and [[Log4j]]. This led to problems when integrating different third-party libraries (JARs) each using different logging frameworks. Pluggable logging frameworks (wrappers) were developed to solve this problem.
==Functionality overview==
Logging is typically broken into three major pieces: the Logger, the Formatter and the
* The Logger is responsible for capturing the message to be logged along with certain metadata and passing it to the logging framework. * After receiving the message, the framework calls the Formatter with the message * The framework then hands the formatted message to the appropriate Appender/Handler for disposition. This might include output to a console display, writing to disk, appending to a database, or generating an email. Simpler logging frameworks, like [https://web.archive.org/web/20020602114537/http://www.theobjectguy.com/javalog/ |Java Logging Framework by the Object Guy], combine the logger and the appender. This simplifies default operation, but it is less configurable, especially if the project is moved across environments.
===Logger===
A Logger is an object that allows the application to log without regard to where the output is sent/stored. The application logs a message by passing an object or an object and an [[Exception handling|exception]] with an optional severity level to the logger object under a given
====Name====
A logger has a name. The name is usually structured hierarchically, with periods (.) separating the levels. A common scheme is to use the name of the class or package that is doing the logging. Both
For example, the logger might be named "<code>com.sun.some.UsefulClass</code>". The handler can be defined for any of the following:
Line 24 ⟶ 30:
* <code>com.sun.some</code>
* <code>com.sun.some.UsefulClass</code>
As long as there is a handler defined somewhere in this stack, logging may occur. For example a message logged to the <code>com.sun.some.UsefulClass</code> logger, may get written by the <code>com.sun</code> handler. Typically there is a global handler that receives and processes messages generated by any logger.
==== Severity level ====
The message is logged at a certain level. Common
{| class="wikitable"
Line 40 ⟶ 48:
|-
|'''WARNING'''
|Use 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.
|-
|'''INFO'''
Line 50 ⟶ 59:
|more detailed information. Expect these to be written to logs only.
|}
The logging framework maintains the current logging level for each logger. The logging level can be set more or less restrictive. For example, if the logging level is set to "WARNING", then all messages of that level or higher are logged, ERROR and FATAL.▼
▲The logging framework maintains the current logging level for each logger. The logging level can be set more or less restrictive. For example, if the logging level is set to "WARNING", then all messages of that level or higher are logged
===Formatters or renderers===▼
A Formatter is an object that formats a given object. Mostly this consists of taking the binary object and converting it to a string representation.▼
Severity levels can be assigned to both loggers and appenders. Both must be enabled for a given severity level for output to be generated. So a logger enabled for debug output will not generate output if the handler that gets the message is not also enabled for debug.
===Filters===
Filters cause a log event to be ignored or logged. The most commonly used filter is the logging level documented in the previous section. Logging frameworks such as Log4j 2 and SLF4J also provide Markers, which when attached to a log event can also be used for filtering. Filters can also be used to accept or deny log events based on exceptions being thrown, data within the log message, data in a ThreadLocal that is exposed through the logging API, or a variety of other methods.
▲===Formatters, Layouts or renderers===
▲A Formatter is an object that formats a given object. Mostly this consists of taking the binary object and converting it to a string representation. Each framework defines a default output format that can be overridden if desired.
===Appenders or handlers===
Line 60 ⟶ 76:
* write to a file or syslog
* append to a database table
* distribute via [[Java messaging service|Java Messaging Services]]
* send via email
* write to a socket
Line 66 ⟶ 82:
=== Feature comparison ===
{| class="wikitable" style="vertical-align: top;"
|+ '''Table 1 - Features'''
! Framework
! Type
! Supported
! Standard
! Comments
! Cost /
|-
! Log4j
! [[Log4J]]▼
| Logging Framework
| <code>FATAL ERROR WARN INFO DEBUG TRACE</code>
| Too many to list: See [http://logging.apache.org/log4j/2.x/manual/appenders.html Appender Documentation]
| Widely used in many projects and platforms▼
| Widely used in many projects and platforms. Log4j 1 was declared "End of Life" in 2015 and has been replaced with Log4j 2 which provides an API that can be used with other logging implementations as well as an implementation of that API.
| Apache License, Version 2.0▼
| <div style="width: 8em;">Apache License, Version 2.0 </div>
|-
! [http://java.sun.com/javase/6/docs/technotes/guides/logging/ Java Logging API]
| Logging Framework
| <code>SEVERE WARNING INFO CONFIG FINE FINER FINEST</code>
| Sun's default Java Virtual Machine (JVM) has the following: ConsoleHandler, FileHandler, SocketHandler, MemoryHandler
|
| Comes with the JRE
|-▼
! [[Apache Commons Logging|Logging]]▼
| FATAL ERROR WARN INFO DEBUG TRACE▼
| Depends on the underlying framework▼
| Widely used, in conjunction with log4j▼
▲| Apache License, Version 2.0
|-▼
| ERROR WARN INFO DEBUG TRACE▼
| [[MIT License]]▼
|-
! [http://www.tinylog.org/ tinylog]
| Logging Framework
| <code>ERROR WARNING INFO DEBUG TRACE</code>
| ConsoleWriter, FileWriter, LogcatWriter, JdbcWriter, RollingFileWriter, SharedFileWriter and ''null'' (discards all log entries) <ref>{{cite web|title=User manual of tinylog|url=http://www.tinylog.org/user-manual|archive-url=https://archive.today/20130415233343/http://www.tinylog.org/user-manual|url-status=dead|archive-date=April 15, 2013}}</ref>
|
| Apache License, Version 2.0
|-
! [http://logback.qos.ch/ Logback]
| Logging Framework
| ERROR WARN INFO DEBUG TRACE▼
▲| <code>ERROR WARN INFO DEBUG TRACE</code>
| Too many to list: see [http://logback.qos.ch/apidocs/ch/qos/logback/core/Appender.html Appender JavaDoc]
|
| [[LGPL]], Version 2.1
▲|-
| Logging Wrapper
▲| <code>FATAL ERROR WARN INFO DEBUG TRACE</code>
▲| Depends on the underlying framework
▲| Apache License, Version 2.0
▲|-
| Logging Wrapper
▲| <code>ERROR WARN INFO DEBUG TRACE</code>
| Depends on the underlying framework, which is pluggable. Provides API compatible [[Shim_(computing)|shims]] for JCL, JDK and Log4j logging packages. It can also use any of them to generate output. Defaults to using Logback for output if available.
▲| Widely used in many projects and platforms, frequently with Logback as the implementation.
▲| [[MIT License]]
|}
==
JCL and Log4j are very common simply because they have been around for so long and were the only choices for a long time. The flexibility of slf4j (using Logback underneath) has made it a popular choice.
SLF4J is a set of logging wrappers (or shims) that allow it to imitate any of the other frameworks. Thus multiple third-party libraries can be incorporated into an application, regardless of the logging framework each has chosen to use. However all logging output is generated in a standard way, typically via Logback.
Log4j 2 provides both an API and an implementation. The API can be routed to other logging implementations equivalent to how SLF4J works. Unlike SLF4J, the Log4j 2 API logs Message<ref>[http://logging.apache.org/log4j/2.x/manual/messages.html Log4j2 API Messages]</ref> objects instead of Strings for extra flexibility and also supports Java Lambda expressions.<ref>[http://logging.apache.org/log4j/2.x/manual/api.html#LambdaSupport Java 8 Lambda support for lazy logging]</ref>
JCL isn't really a logging framework, but a wrapper for one. As such, it requires a logging framework underneath it, although it can default to using its own <code>SimpleLog</code> logger.
JCL, SLF4J and the Log4j 2 API are useful when developing reusable libraries which need to write to whichever underlying logging system is being used by the application. This also provides flexibility in heterogeneous environments where the logging framework is likely to change, although in most cases, once a logging framework has been chosen, there is little need to change it over the life of the project. SLF4J and Log4j 2 benefit from being newer and build on the lessons learned from older frameworks. Moreover JCL has known problems with class-loaders when determining what logging library it should wrap <ref>[https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/overview.html#overview-avoiding-commons-logging Avoiding Commons Logging]</ref> which has now replaced JCL.<ref>[https://docs.spring.io/spring/docs/5.0.0.RC3/spring-framework-reference/overview.html#overview-logging Spring Logging Overview]</ref>
The Java Logging API is provided with Java. Although the API is technically separate from the default implementation provided with Java, replacing it with an alternate implementation can be challenging so many developers confuse this implementation with the Java Logging API. Configuration is by external files only which is not easily changed on the fly (other frameworks support programmatic configuration). The default implementation only provides a few Handlers and Formatters which means most users will have to write their own.<ref>[https://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html java.util.logging Overview]</ref>
==See also==
*[[SLF4J]]
*[[
*[[logback]]
*[[Javolution]] LogContext based on [http://javolution.org/apidocs/javolution/context/package-summary.html#package_description context programming] (actual logging framework selectable at run-time).
Line 132 ⟶ 164:
* [http://java-source.net/open-source/logging Open Source Logging Tools in Java]
* [http://apache.org/licenses/LICENSE-2.0 The Apache 2.0 license.]
* [http://logback.qos.ch/ Logback - A successor to the popular
* [http://www.tinylog.org/ tinylog - Minimalist logging utility with a static logger]
* [http://loggifier.unkrig.de Loggifier] A tool that inserts logging code into .class, .jar and .ear files
Line 138 ⟶ 170:
* [http://www.javavillage.in/perf4j.php Perf4j]
* [http://www.javavillage.in/slf4j.php SLF4J]
* [http://
{{DEFAULTSORT:Java Logging Framework}}
[[Category:Java platform]]
[[Category:Computer logging]]
|