blob: 9f94e2243ac30561ca6b47e6492569ac834fb693 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Generated by Apache Maven Doxia at Aug 17, 2014 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
Custom Log Levels - Apache Log4j 2</title>
<link rel="stylesheet" href="../css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../css/site.css" type="text/css" />
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/prettify.min.js"></script>
<script type="text/javascript" src="../js/site.js"></script>
<meta name="author" content="Remko Popma" />
<meta name="Date-Revision-yyyymmdd" content="20140817" />
<meta http-equiv="Content-Language" content="en" />
</head>
<body class="composite">
<img class="logo-left" src="../images/ls-logo.jpg" alt="Apache logging services logo" />
<img class="logo-right" src="../images/logo.jpg" alt="Apache log4j logo" />
<div class="clear"></div>
<div class="navbar">
<div class="navbar-inner">
<div class="container-fluid">
<a class="brand" href="http://logging.apache.org/log4j/2.x/">Apache Log4j 2 &trade;</a>
<ul class="nav">
<li>
<a href="http://wiki.apache.org/logging" class="external" target="_blank" title="Logging Wiki">Logging Wiki</a>
</li>
<li>
<a href="http://www.apache.org/" class="external" target="_blank" title="Apache">Apache</a>
</li>
<li>
<a href="../../../" title="Logging Services">Logging Services</a>
</li>
<li>
<a href="https://analysis.apache.org/dashboard/index/org.apache.logging.log4j:log4j" class="external" target="_blank" title="Sonar">Sonar</a>
</li>
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<table class="layout-table">
<tr>
<td class="sidebar">
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-home"></i>Apache Log4j™ 2</li>
<li class="none">
<a href="../index.html" title="About">About</a>
</li>
<li class="none">
<a href="../download.html" title="Download">Download</a>
</li>
<li class="none">
<a href="../maven-artifacts.html" title="Maven and Ivy">Maven and Ivy</a>
</li>
<li class="none">
<a href="../build.html" title="Build">Build</a>
</li>
<li class="none">
<a href="../guidelines.html" title="Guidelines">Guidelines</a>
</li>
<li class="none">
<a href="../javastyle.html" title="Style Guide">Style Guide</a>
</li>
<li class="none">
<a href="../changelog.html" title="Changelog">Changelog</a>
</li>
<li class="collapsed">
<a href="../javadoc.html" title="Javadoc">Javadoc</a>
</li>
<li class="none">
<a href="../faq.html" title="FAQ">FAQ</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-book"></i>Manual</li>
<li class="none">
<a href="../manual/index.html" title="Introduction">Introduction</a>
</li>
<li class="none">
<a href="../manual/architecture.html" title="Architecture">Architecture</a>
</li>
<li class="none">
<a href="../manual/migration.html" title="Log4j 1.x Migration">Log4j 1.x Migration</a>
</li>
<li class="collapsed">
<a href="../manual/api.html" title="API">API</a>
</li>
<li class="collapsed">
<a href="../manual/configuration.html" title="Configuration">Configuration</a>
</li>
<li class="collapsed">
<a href="../manual/webapp.html" title="Web Applications and JSPs">Web Applications and JSPs</a>
</li>
<li class="collapsed">
<a href="../manual/plugins.html" title="Plugins">Plugins</a>
</li>
<li class="collapsed">
<a href="../manual/lookups.html" title="Lookups">Lookups</a>
</li>
<li class="collapsed">
<a href="../manual/appenders.html" title="Appenders">Appenders</a>
</li>
<li class="collapsed">
<a href="../manual/layouts.html" title="Layouts">Layouts</a>
</li>
<li class="collapsed">
<a href="../manual/filters.html" title="Filters">Filters</a>
</li>
<li class="collapsed">
<a href="../manual/async.html" title="Async Loggers">Async Loggers</a>
</li>
<li class="none">
<a href="../manual/jmx.html" title="JMX">JMX</a>
</li>
<li class="none">
<a href="../manual/logsep.html" title="Logging Separation">Logging Separation</a>
</li>
<li class="collapsed">
<a href="../manual/extending.html" title="Extending Log4j">Extending Log4j</a>
</li>
<li class="collapsed">
<a href="../manual/customconfig.html" title="Extending Log4j Configuration">Extending Log4j Configuration</a>
</li>
<li class="expanded active">
<a href="../manual/customloglevels.html" title="Custom Log Levels">Custom Log Levels</a>
<ul>
<li class="none">
<a href="../manual/customloglevels.html#AddingOrReplacingLevels" title="Adding or Replacing Levels">Adding or Replacing Levels</a>
</li>
<li class="none">
<a href="../manual/customloglevels.html#CustomLoggers" title="Custom Loggers">Custom Loggers</a>
</li>
<li class="none">
<a href="../manual/customloglevels.html#ExampleUsage" title="Custom Logger Example">Custom Logger Example</a>
</li>
<li class="none">
<a href="../manual/customloglevels.html#CodeGen" title="Code Generation Tool">Code Generation Tool</a>
</li>
</ul>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-cog"></i>Components</li>
<li class="none">
<a href="../log4j-api/index.html" title="API">API</a>
</li>
<li class="none">
<a href="../log4j-core/index.html" title="Implementation">Implementation</a>
</li>
<li class="none">
<a href="../log4j-jcl/index.html" title="Commons Logging Bridge">Commons Logging Bridge</a>
</li>
<li class="none">
<a href="../log4j-1.2-api/index.html" title="Log4j 1.2 API">Log4j 1.2 API</a>
</li>
<li class="none">
<a href="../log4j-slf4j-impl/index.html" title="SLF4J Binding">SLF4J Binding</a>
</li>
<li class="none">
<a href="../log4j-to-slf4j/index.html" title="Log4j 2 to SLF4J Adapter">Log4j 2 to SLF4J Adapter</a>
</li>
<li class="none">
<a href="../log4j-flume-ng/index.html" title="Apache Flume NG Appender">Apache Flume NG Appender</a>
</li>
<li class="none">
<a href="../log4j-taglib/index.html" title="Log4j Tag Library">Log4j Tag Library</a>
</li>
<li class="none">
<a href="../log4j-jmx-gui/index.html" title="Log4j JMX GUI">Log4j JMX GUI</a>
</li>
<li class="none">
<a href="../log4j-web/index.html" title="Log4j Web Application Support">Log4j Web Application Support</a>
</li>
<li class="none">
<a href="../log4j-nosql/index.html" title="Log4j NoSQL support">Log4j NoSQL support</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-info-sign"></i>Project Information</li>
<li class="none">
<a href="../dependencies.html" title="Dependencies">Dependencies</a>
</li>
<li class="none">
<a href="../dependency-convergence.html" title="Dependency Convergence">Dependency Convergence</a>
</li>
<li class="none">
<a href="../dependency-management.html" title="Dependency Management">Dependency Management</a>
</li>
<li class="none">
<a href="../team-list.html" title="Project Team">Project Team</a>
</li>
<li class="none">
<a href="../mail-lists.html" title="Mailing Lists">Mailing Lists</a>
</li>
<li class="none">
<a href="../issue-tracking.html" title="Issue Tracking">Issue Tracking</a>
</li>
<li class="none">
<a href="../license.html" title="Project License">Project License</a>
</li>
<li class="none">
<a href="../source-repository.html" title="Source Repository">Source Repository</a>
</li>
<li class="none">
<a href="../project-summary.html" title="Project Summary">Project Summary</a>
</li>
</ul>
<ul class="nav nav-list">
<li class="nav-header"><i class="icon-cog"></i>Project Reports</li>
<li class="none">
<a href="../changes-report.html" title="Changes Report">Changes Report</a>
</li>
<li class="none">
<a href="../jira-report.html" title="JIRA Report">JIRA Report</a>
</li>
<li class="none">
<a href="../surefire-report.html" title="Surefire Report">Surefire Report</a>
</li>
<li class="none">
<a href="../rat-report.html" title="RAT Report">RAT Report</a>
</li>
</ul>
</div>
<div id="poweredBy">
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img class="poweredBy" alt="Built by Maven" src="../images/maven-feather.png" />
</a>
</div>
</td>
<td class="content">
<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<a name="top"></a>
<div class="section"><h2>Custom Log Levels<a name="Custom_Log_Levels"></a></h2>
<p>
Log4J 2 supports custom log levels. The <tt>Level.forName()</tt> method creates a new
level for the specified name. By calling the Logger.log() method and passing this
custom log level you can log messages at this level:
</p>
<div class="prettyprint"><pre>
final Logger logger = LogManager.getLogger();
final Level VERBOSE = Level.forName(&quot;VERBOSE&quot;, 550);
logger.log(VERBOSE, &quot;a verbose message&quot;);
logger.log(VERBOSE, &quot;another message&quot;);</pre></div>
<p>When defining a custom log level, the <tt>intLevel</tt> parameter (550 in the example above) determines
where the custom level exists in relation to the standard levels built-in to Log4J 2.
For reference, the table below shows the <tt>intLevel</tt> of the built-in log levels.
</p>
<table border="0" class="bodyTable" style="width: 30%"><caption align="top">Standard log levels built-in to Log4J</caption>
<tr class="a">
<th>Standard Level</th>
<th>intLevel</th>
</tr>
<tr class="b">
<td>OFF</td>
<td align="right">0</td>
</tr>
<tr class="a">
<td>FATAL</td>
<td align="right">100</td>
</tr>
<tr class="b">
<td>ERROR</td>
<td align="right">200</td>
</tr>
<tr class="a">
<td>WARN</td>
<td align="right">300</td>
</tr>
<tr class="b">
<td>INFO</td>
<td align="right">400</td>
</tr>
<tr class="a">
<td>DEBUG</td>
<td align="right">500</td>
</tr>
<tr class="b">
<td>TRACE</td>
<td align="right">600</td>
</tr>
<tr class="a">
<td>ALL</td>
<td align="right"><tt>Integer.MAX_VALUE</tt></td>
</tr>
</table>
<a name="StandardLoggerInterface"></a>
<div class="section"><h3>Convenience Methods for the Built-in Log Levels<a name="Convenience_Methods_for_the_Built-in_Log_Levels"></a></h3>
<p>
The built-in log levels have a set of convenience methods on the Logger
interface that makes them easier to use. For example, the Logger interface
has fourteen <tt>debug()</tt> methods that support the DEBUG level:
</p>
<div class="prettyprint"><pre>
// convenience methods for the built-in DEBUG level
debug(Marker, Message)
debug(Marker, Message, Throwable)
debug(Marker, Object)
debug(Marker, Object, Throwable)
debug(Marker, String)
debug(Marker, String, Object...)
debug(Marker, String, Throwable)
debug(Message)
debug(Message, Throwable)
debug(Object)
debug(Object, Throwable)
debug(String)
debug(String, Object...)
debug(String, Throwable)</pre></div>
<p>
Similar methods exist for the other built-in levels.
</p>
<p>It would be nice to have the same ease of use with custom levels,
so that after declaring a custom VERBOSE level, we could
use code like this:
</p>
<div class="prettyprint"><pre>
logger.verbose(&quot;a verbose message&quot;); // no need to pass the VERBOSE level as a parameter
logger.verbose(&quot;another message&quot;);</pre></div>
</div>
<a name="AddingOrReplacingLevels"></a>
<div class="section"><h3>Adding or Replacing Log Levels<a name="Adding_or_Replacing_Log_Levels"></a></h3>
<p>In the above example, a convenience method was <i>added</i> to the Logger interface,
in addition to the existing trace(), debug(), info(), ... methods for the built-in log levels.
</p>
<p>There is another use case, Domain Specific Language loggers, where we want to <i>replace</i>
the existing trace(), debug(), info(), ... methods with all-custom methods.
</p>
<p>
For example, for medical devices we could have only <tt>critical()</tt>, <tt>warning()</tt>,
and <tt>advisory()</tt> methods.
Another example could be a game that has only <tt>defcon1()</tt>, <tt>defcon2()</tt>,
and <tt>defcon3()</tt> levels.
</p>
<p>
If it were possible to hide existing log levels, users could customize the
Logger interface to match their requirements. Some people may not want to
have a FATAL or a TRACE level, for example. They would like to be able to
create a custom Logger that only has debug(), info(), warn() and error() methods.
</p>
</div>
<a name="CustomLoggers"></a>
<div class="section"><h3>Generating Source Code for a Custom Logger Wrapper<a name="Generating_Source_Code_for_a_Custom_Logger_Wrapper"></a></h3>
<p>
Common Log4J usage is to get an instance of the <tt>Logger</tt> interface from the
<tt>LogManager</tt> and call the methods on this interface.
However, the custom log Levels are not known in advance, so Log4J cannot provide
an interface with convenience methods for these custom log Levels.
</p>
<p>
To solve this, Log4J ships with a tool that generates source code for a
Logger wrapper. The generated wrapper class has convenience methods for each
custom log level, making custom levels just as easy to use as the built-in levels.
</p>
<p>
There are two flavors of wrappers: ones that <i><b>extend</b></i> the Logger
API (adding methods to the built-in levels) and ones that <i><b>customize</b></i>
the Logger API (replacing the built-in methods).
</p>
<p>When generating the source code for a wrapper class, you need to specify:</p>
<ul>
<li>the fully qualified name of the class to generate
</li>
<li>the list of custom levels to support and their <tt>intLevel</tt> relative strength
</li>
<li>whether to extend <tt>Logger</tt> (and keep the existing built-in methods)
or have only methods for the custom log levels
</li>
</ul>
<p>You would then include the generated source code in the project
where you want to use custom log levels.</p>
</div>
<a name="ExampleUsage"></a>
<div class="section"><h3>Example Usage of a Generated Logger Wrapper<a name="Example_Usage_of_a_Generated_Logger_Wrapper"></a></h3>
<p>
Here is an example of how one would use a generated logger wrapper with
custom levels DIAG, NOTICE and VERBOSE:
</p>
<div class="prettyprint linenums"><pre>
// ExtLogger is a generated logger wrapper
import com.mycompany.myproject.ExtLogger;
public class MyService {
// instead of Logger logger = LogManager.getLogger(MyService.class):
private static final ExtLogger logger = ExtLogger.create(MyService.class);
public void someMethod() {
// ...
logger.trace(&quot;the built-in TRACE level&quot;);
logger.verbose(&quot;a custom level: a VERBOSE message&quot;);
logger.debug(&quot;the built-in DEBUG level&quot;);
logger.notice(&quot;a custom level: a NOTICE message&quot;);
logger.info(&quot;the built-in INFO level&quot;);
logger.diag(&quot;a custom level: a DIAG message&quot;);
logger.warn(&quot;the built-in WARN level&quot;);
logger.error(&quot;the built-in ERROR level&quot;);
logger.fatal(&quot;the built-in FATAL level&quot;);
// ...
}
...
}</pre></div>
</div>
<a name="CodeGen"></a>
<div class="section"><h3>Generating Extended Loggers<a name="Generating_Extended_Loggers"></a></h3>
<p>
Use the following command to generate a logger wrapper that adds methods to the built-in ones:
</p>
<div class="prettyprint"><pre>
java -cp log4j-core-2.0.2.jar org.apache.logging.log4j.core.tools.Generate$ExtendedLogger \
com.mycomp.ExtLogger DIAG=350 NOTICE=450 VERBOSE=550 &gt; com/mycomp/ExtLogger.java</pre></div>
<p>
This will generate source code for a logger wrapper that has the convenience methods
for the built-in levels <i>as well as</i> the specified custom levels. The tool prints the
generated source code to the console.
By appending &quot; &gt; <i>filename</i>&quot; the output can be redirected to a file.
</p>
</div>
<div class="section"><h3>Generating Custom Loggers<a name="Generating_Custom_Loggers"></a></h3>
<p>
Use the following command to generate a logger wrapper that hides the built-in levels and has only custom levels:
</p>
<div class="prettyprint"><pre>
java -cp log4j-core-2.0.2.jar org.apache.logging.log4j.core.tools.Generate$CustomLogger \
com.mycomp.MyLogger DEFCON1=350 DEFCON2=450 DEFCON3=550 &gt; com/mycomp/MyLogger.java</pre></div>
<p>
This will generate source code for a logger wrapper that <i>only</i> has convenience
methods for the specified custom levels, <i>not</i> for the built-in levels.
The tool prints the generated source code to the console.
By appending &quot; &gt; <i>filename</i>&quot; the output can be redirected to a file.
</p>
</div>
</div>
</td>
</tr>
</table>
</div>
<div class="footer">
<p>Copyright &copy; 1999-2014 <a class="external" href="http://www.apache.org">Apache Software Foundation</a>. All Rights Reserved.</p>
<p>Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, and the Apache Logging project logo are trademarks of The Apache Software Foundation.</p>
<p>Site powered by <a class="external" href="http://getbootstrap.com/">Twitter Bootstrap</a>. Icons from <a class="external" href="http://glyphicons.com/">Glyphicons Free</a>.</p>
</div>
</div>
</body>
</html>