Java logging framework: Difference between revisions

Content deleted Content added
No edit summary
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
 
(22 intermediate revisions by 12 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. Loggingby an application and is a common issue for development teams. SeveralLogging frameworks ease and standardize the process of logging for the Java platform. ThisIn articleparticular coversthey generalprovide purposeflexibility loggingby frameworksavoiding explicit output to the console (see Appender below). Where logs are written becomes independent of the code and can be customized at runtime.
 
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 [[log4jLog4j]]. 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==
Line 17:
* 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 a name/identifier.
 
====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 [[log4j]]Log4j and the Java logging [[API]] support 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 64:
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.
 
===Formatters or renderersFilters===
 
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.
 
Line 72 ⟶ 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 87 ⟶ 91:
! Cost / Licence
|-
! 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. 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.
| Widely used in many projects and platforms
| <div style="width: 8em;">Apache License, Version 2.0 </div>
|-
Line 104 ⟶ 108:
| 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]
Line 112 ⟶ 116:
| <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]
| Developed as a replacement for log4jLog4j, with many improvements. Used by numerous projects, typically behind slf4j, for example [[Akka (toolkit)|Akka]], [[Apache Camel]], [[Apache Cocoon]], [[Artifactory]], [[Gradle]], [[Lift (web framework)|Lift Framework]], [[Play Framework]], [[Scalatra]], [[SonarQube]], [[Spring_Framework#Spring_Boot|Spring Boot]], ...
| [[LGPL]], Version 2.1
|-
! [[Apache Commons Logging]] (JCL)
| Logging Wrapper
| <code>FATAL ERROR WARN INFO DEBUG TRACE</code>
| Depends on the underlying framework
| Widely used, often in conjunction with log4jLog4j
| Apache License, Version 2.0
|-
Line 125 ⟶ 129:
| 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 log4jLog4j 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, typicallyfrequently inwith conjunctionLogback withas the slf4jimplementation.
| [[MIT License]]
|}
 
==SummaryConsiderations==
 
JCL and log4jLog4j are very common simply because they have been around for so long and were the only choices for a long time. Increasingly theThe flexibility of slf4j (using Logback underneath) makeshas made it thea popular choice.
 
slf4jSLF4J 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.
JCL and log4j are very common simply because they have been around for so long and were the only choices for a long time. Increasingly the flexibility of slf4j (using Logback underneath) makes it the popular choice.
 
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>
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.
 
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.
 
Both JCL, SLF4J and slf4jthe 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. sjf4jSLF4J benefitsand Log4j 2 benefit from being a newer and buildsbuild 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 seeAvoiding forCommons exampleLogging]</ref> which has now replaced JCL.<ref>[httphttps://staticdocs.springsourcespring.orgio/spring/docs/35.0.x0.RC3/spring-framework-reference/html/overview.html#overview-logging Spring Logging Overview].</ref>
 
The Java Logging API is alsoprovided not a logging framework, but a standard API for accessing awith logging frameworkJava. Compatible frameworks can be loaded into JVM and accessed viaAlthough the API. There is alsotechnically aseparate loggingfrom the default implementation suppliedprovided with theJava, Sunreplacing JVMit whichwith isan thealternate defaultimplementation loggingcan frameworkbe accessedchallenging by the API.so Manymany 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]]
*[[log4jLog4j]]
*[[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 158 ⟶ 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 log4jLog4j project]
* [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