| <!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 2016-05-25 --> |
| <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>Log4j – 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="20160525" /> |
| <meta http-equiv="Content-Language" content="en" /> |
| |
| </head> |
| <body class="composite"> |
| <a href="https://logging.apache.org/"> |
| <img class="logo-left" src="../images/ls-logo.jpg" alt="Apache logging services logo" /> |
| </a> |
| <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 ™</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="../runtime-dependencies.html" title="Runtime Dependencies">Runtime Dependencies</a> |
| </li> |
| <li class="none"> |
| <a href="../articles.html" title="Articles">Articles</a> |
| </li> |
| <li class="none"> |
| <a href="../faq.html" title="FAQ">FAQ</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../performance.html" title="Performance">Performance</a> |
| </li> |
| <li class="none"> |
| <a href="../thanks.html" title="Thanks">Thanks</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/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="collapsed"> |
| <a href="../manual/garbagefree.html" title="Garbage-free Logging">Garbage-free Logging</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/plugins.html" title="Plugins">Plugins</a> |
| </li> |
| <li class="collapsed"> |
| <a href="../manual/customconfig.html" title="Programmatic Log4j Configuration">Programmatic 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#DefiningLevelsInCode" title="In Code">In Code</a> |
| </li> |
| <li class="none"> |
| <a href="../manual/customloglevels.html#DefiningLevelsInConfiguration" title="In Configuration">In Configuration</a> |
| </li> |
| <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-jul/index.html" title="JUL Adapter">JUL Adapter</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 Appender">Apache Flume 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> |
| <li class="none"> |
| <a href="../log4j-iostreams/index.html" title="Log4j IO Streams">Log4j IO Streams</a> |
| </li> |
| <li class="none"> |
| <a href="../log4j-liquibase/index.html" title="Log4j Liquibase Binding">Log4j Liquibase Binding</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><a name="Custom_Log_Levels"></a>Custom Log Levels</h2> |
| <a name="DefiningLevelsInCode"></a> |
| |
| <div class="section"> |
| <h3><a name="Defining_Custom_Log_Levels_in_Code"></a>Defining Custom Log Levels in Code</h3> |
| |
| <p> |
| Log4J 2 supports custom log levels. Custom log levels can be defined in code |
| or in configuration. To define a custom log level in code, use the |
| <tt>Level.forName()</tt> method. This method creates a new |
| level for the specified name. After a log level is defined you can log |
| messages at this level by calling the Logger.log() method and passing |
| the custom log level: |
| </p> |
| |
| <div> |
| <pre class="prettyprint"> |
| // This creates the "VERBOSE" level if it does not exist yet. |
| final Level VERBOSE = Level.forName("VERBOSE", 550); |
| |
| final Logger logger = LogManager.getLogger(); |
| logger.log(VERBOSE, "a verbose message"); // use the custom VERBOSE level |
| |
| // Create and use a new custom level "DIAG". |
| logger.log(Level.forName("DIAG", 350), "a diagnostic message"); |
| |
| // Use (don't create) the "DIAG" custom level. |
| // Only do this *after* the custom level is created! |
| logger.log(Level.getLevel("DIAG"), "another diagnostic message"); |
| |
| // Using an undefined level results in an error: Level.getLevel() returns null, |
| // and logger.log(null, "message") throws an exception. |
| logger.log(Level.getLevel("FORGOT_TO_DEFINE"), "some message"); // throws exception!</pre></div> |
| |
| <p>When defining a custom log level, the <tt>intLevel</tt> parameter (550 and 350 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> |
| </div> |
| <a name="DefiningLevelsInConfiguration"></a> |
| |
| <div class="section"> |
| <h3><a name="Defining_Custom_Log_Levels_in_Configuration"></a>Defining Custom Log Levels in Configuration</h3> |
| |
| <p> |
| Custom log levels can also be defined in configuration. |
| This is convenient for using a custom level in a logger filter |
| or an appender filter. Similar to defining log levels in code, |
| a custom level must be defined first, before it can be used. |
| If a logger or appender is configured with an undefined level, |
| that logger or appender will be invalid and will not process any log events. |
| </p> |
| |
| <p> |
| The <b>CustomLevel</b> configuration element creates a custom level. |
| Internally it calls the same <tt>Level.forName()</tt> method discussed above. |
| </p> |
| |
| <table border="0" class="bodyTable"><caption align="top">CustomLevel Parameters</caption> |
| |
| |
| <tr class="a"> |
| |
| <th>Parameter Name</th> |
| |
| <th>Type</th> |
| |
| <th>Description</th> |
| </tr> |
| |
| <tr class="b"> |
| |
| <td>name</td> |
| |
| <td>String</td> |
| |
| <td>The name of the custom level. Note that level names are case sensitive. |
| The convention is to use all upper-case names.</td> |
| </tr> |
| |
| <tr class="a"> |
| |
| <td>intLevel</td> |
| |
| <td>integer</td> |
| |
| <td>Determines where the custom level exists in relation to the |
| standard levels built-in to Log4J 2 (see the table above).</td> |
| </tr> |
| </table> |
| |
| <p> |
| The following example shows a configuration that defines some custom |
| log levels and uses a custom log level to filter log events sent to the console. |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| <?xml version="1.0" encoding="UTF-8"?> |
| <Configuration status="WARN"> |
| <!-- Define custom levels before using them for filtering below. --> |
| <CustomLevels> |
| <CustomLevel name="DIAG" intLevel="350" /> |
| <CustomLevel name="NOTICE" intLevel="450" /> |
| <CustomLevel name="VERBOSE" intLevel="550" /> |
| </CustomLevels> |
| |
| <Appenders> |
| <Console name="Console" target="SYSTEM_OUT"> |
| <PatternLayout pattern="%d %-7level %logger{36} - %msg%n"/> |
| </Console> |
| <File name="MyFile" fileName="logs/app.log"> |
| <PatternLayout pattern="%d %-7level %logger{36} - %msg%n"/> |
| </File> |
| </Appenders> |
| <Loggers> |
| <Root level="trace"> |
| <!-- Only events at DIAG level or more specific are sent to the console. --> |
| <AppenderRef ref="Console" level="diag" /> |
| <AppenderRef ref="MyFile" level="trace" /> |
| </Root> |
| </Loggers> |
| </Configuration></pre></div> |
| </div> |
| <a name="StandardLoggerInterface"></a> |
| |
| <div class="section"> |
| <h3><a name="Convenience_Methods_for_the_Built-in_Log_Levels"></a>Convenience Methods for the Built-in Log Levels</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 24 <tt>debug()</tt> methods that support the DEBUG level: |
| </p> |
| |
| <div> |
| <pre class="prettyprint"> |
| // 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) |
| // lambda support methods added in 2.4 |
| debug(Marker, MessageSupplier) |
| debug(Marker, MessageSupplier, Throwable) |
| debug(Marker, String, Supplier<?>...) |
| debug(Marker, Supplier<?>) |
| debug(Marker, Supplier<?>, Throwable) |
| debug(MessageSupplier) |
| debug(MessageSupplier, Throwable) |
| debug(String, Supplier<?>...) |
| debug(Supplier<?>) |
| debug(Supplier<?>, Throwable)</pre></div> |
| |
| <p> |
| Similar methods exist for the other built-in levels. |
| Custom levels, in contrast, need to pass in the log level as an extra parameter. |
| </p> |
| |
| <div> |
| <pre class="prettyprint"> |
| // need to pass the custom level as a parameter |
| logger.log(VERBOSE, "a verbose message"); |
| logger.log(Level.forName("DIAG", 350), "another message");</pre></div> |
| |
| <p>It would be nice to have the same ease of use with custom levels, |
| so that after declaring the custom VERBOSE/DIAG levels, we could |
| use code like this: |
| </p> |
| |
| <div> |
| <pre class="prettyprint"> |
| // nice to have: descriptive methods and no need to pass the level as a parameter |
| logger.verbose("a verbose message"); |
| logger.diag("another message"); |
| logger.diag("java 8 lambda expression: {}", () -> someMethod());</pre></div> |
| |
| <p>The standard Logger interface cannot provide convenience methods for |
| custom levels, but the next few sections introduce a code generation tool |
| to create loggers that aim to make custom levels as easy to use |
| as built-in levels.</p> |
| </div> |
| <a name="AddingOrReplacingLevels"></a> |
| |
| <div class="section"> |
| <h3><a name="Adding_or_Replacing_Log_Levels"></a>Adding or Replacing Log Levels</h3> |
| |
| <p> |
| We assume that most users want to <i>add</i> custom level methods 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><a name="Generating_Source_Code_for_a_Custom_Logger_Wrapper"></a>Generating Source Code for a Custom Logger Wrapper</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><a name="Example_Usage_of_a_Generated_Logger_Wrapper"></a>Example Usage of a Generated Logger Wrapper</h3> |
| |
| <p> |
| Here is an example of how one would use a generated logger wrapper with |
| custom levels DIAG, NOTICE and VERBOSE: |
| </p> |
| |
| <div> |
| <pre class="prettyprint linenums"> |
| // 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 demoExtendedLogger() { |
| // ... |
| logger.trace("the built-in TRACE level"); |
| logger.verbose("a custom level: a VERBOSE message"); |
| logger.debug("the built-in DEBUG level"); |
| logger.notice("a custom level: a NOTICE message"); |
| logger.info("the built-in INFO level"); |
| logger.diag("a custom level: a DIAG message"); |
| logger.warn("the built-in WARN level"); |
| logger.error("the built-in ERROR level"); |
| logger.fatal("the built-in FATAL level"); |
| logger.notice("java 8 lambda expression only executed if NOTICE is enabled: {}", () -> someMethod()); |
| // ... |
| } |
| ... |
| }</pre></div> |
| </div> |
| <a name="CodeGen"></a> |
| |
| <div class="section"> |
| <h3><a name="Generating_Extended_Loggers"></a>Generating Extended Loggers</h3> |
| |
| <p> |
| Use the following command to generate a logger wrapper that adds methods to the built-in ones: |
| </p> |
| |
| <div> |
| <pre class="prettyprint"> |
| java -cp log4j-core-2.6.jar org.apache.logging.log4j.core.tools.Generate$ExtendedLogger \ |
| com.mycomp.ExtLogger DIAG=350 NOTICE=450 VERBOSE=550 > 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 " > <i>filename</i>" the output can be redirected to a file. |
| </p> |
| </div> |
| |
| <div class="section"> |
| <h3><a name="Generating_Custom_Loggers"></a>Generating Custom Loggers</h3> |
| |
| <p> |
| Use the following command to generate a logger wrapper that hides the built-in levels and has only custom levels: |
| </p> |
| |
| <div> |
| <pre class="prettyprint"> |
| java -cp log4j-core-2.6.jar org.apache.logging.log4j.core.tools.Generate$CustomLogger \ |
| com.mycomp.MyLogger DEFCON1=350 DEFCON2=450 DEFCON3=550 > 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 " > <i>filename</i>" the output can be redirected to a file. |
| </p> |
| </div> |
| </div> |
| |
| |
| |
| </td> |
| </tr> |
| </table> |
| </div> |
| |
| <div class="footer"> |
| <p>Copyright © 1999-2016 <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> |