You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2018/04/08 21:03:45 UTC
[4/7] logging-log4j2 git commit: LOG4J2-1802: Convert log4j 1.x
migration page to asciidoc
LOG4J2-1802: Convert log4j 1.x migration page to asciidoc
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/8dea0f4f
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/8dea0f4f
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/8dea0f4f
Branch: refs/heads/master
Commit: 8dea0f4fb1eef58b266874e5b14cf3955f4d739e
Parents: d7f2fd7
Author: Matt Sicker <bo...@gmail.com>
Authored: Sun Apr 8 14:47:56 2018 -0500
Committer: Matt Sicker <bo...@gmail.com>
Committed: Sun Apr 8 16:03:36 2018 -0500
----------------------------------------------------------------------
src/site/asciidoc/manual/migration.adoc | 359 +++++++++++++++++++++++++++
src/site/xdoc/manual/migration.xml | 351 --------------------------
2 files changed, 359 insertions(+), 351 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8dea0f4f/src/site/asciidoc/manual/migration.adoc
----------------------------------------------------------------------
diff --git a/src/site/asciidoc/manual/migration.adoc b/src/site/asciidoc/manual/migration.adoc
new file mode 100644
index 0000000..9d217b4
--- /dev/null
+++ b/src/site/asciidoc/manual/migration.adoc
@@ -0,0 +1,359 @@
+= Migrating from Log4j 1.x
+Ralph Goers <rg...@apache.org>
+
+[#Log4j1.2Bridge]
+== Using the Log4j 1.x bridge
+
+Perhaps the simplest way to convert to using Log4j 2 is to replace the
+log4j 1.x jar file with Log4j 2's `log4j-1.2-api.jar`. However, to use
+this successfully applications must meet the following requirements:
+
+1. They must not access methods and classes internal to the Log4j 1.x
+implementation such as `Appender`s, `LoggerRepository` or `Category`'s
+`callAppenders` method.
+2. They must not programmatically configure Log4j.
+3. They must not configure by calling the classes `DOMConfigurator` or
+`PropertyConfigurator`.
+
+== Converting to the Log4j 2 API
+
+For the most part, converting from the Log4j 1.x API to Log4j 2 should
+be fairly simple. Many of the log statements will require no
+modification. However, where necessary the following changes must be
+made.
+
+. The main package in version 1 is `org.apache.log4j`, in version 2 it
+is `org.apache.logging.log4j`
+. Calls to `org.apache.log4j.Logger.getLogger()` must be modified to
+`org.apache.logging.log4j.LogManager.getLogger()`.
+. Calls to `org.apache.log4j.Logger.getRootLogger()` or
+`org.apache.log4j.LogManager.getRootLogger()` must be replaced with
+`org.apache.logging.log4j.LogManager.getRootLogger()`.
+. Calls to `org.apache.log4j.Logger.getLogger` that accept a
+`LoggerFactory` must remove the `org.apache.log4j.spi.LoggerFactory` and
+use one of Log4j 2's other extension mechanisms.
+. Replace calls to `org.apache.log4j.Logger.getEffectiveLevel()` with
+`org.apache.logging.log4j.Logger.getLevel()`.
+. Remove calls to `org.apache.log4j.LogManager.shutdown()`, they are
+not needed in version 2 because the Log4j Core now automatically adds a
+JVM shutdown hook on start up to perform any Core clean ups.
+.. Starting in Log4j 2.1, you can specify a custom
+link:../log4j-core/apidocs/org/apache/logging/log4j/core/util/ShutdownCallbackRegistry.html[`ShutdownCallbackRegistry`]
+to override the default JVM shutdown hook strategy.
+.. Starting in Log4j 2.6, you can now use
+`org.apache.logging.log4j.LogManager.shutdown()` to initiate shutdown
+manually.
+. Calls to `org.apache.log4j.Logger.setLevel()` or similar methods are
+not supported in the API. Applications should remove these. Equivalent
+functionality is provided in the Log4j 2 implementation classes, see
+`org.apache.logging.log4j.core.config.Configurator.setLevel()`, but may
+leave the application susceptible to changes in Log4j 2 internals.
+. Where appropriate, applications should convert to use parameterized
+messages instead of String concatenation.
+. http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html[`org.apache.log4j.MDC`]
+and
+http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html[`org.apache.log4j.NDC`]
+have been replaced by the link:thread-context.html[Thread Context].
+
+== Configuring Log4j 2
+
+Although the Log4j 2 configuration syntax is different than that of
+Log4j 1.x, most, if not all, of the same functionality is available.
+
+Note that system property interpolation via the `${foo}` syntax has been
+extended to allow property lookups from many different sources. See the
+link:lookups.html[Lookups] documentation for more details. For example,
+using a lookup for the system property named `catalina.base`, in Log4j
+1.x, the syntax would be `${catalina.base}`. In Log4j 2, the syntax
+would be `${sys:catalina.base}`.
+
+Log4j 1.x has a XMLLayout which is different from the XmlLayout in Log4j
+2, the log4j-1.2-api module contains a `Log4j1XmlLayout` which produce
+output in the format as in Log4j 1.x. The Log4j 1.x `SimpleLayout` can
+be emulated with PatternLayout "%level - %m%n". The Log4j 1.x
+`TTCCLayout` can be emulated with PatternLayout "%r [%t] %p %c
+%notEmpty\{%ndc }- %m%n".
+
+Both `PatternLayout` and `EnhancedPatternLayout` in Log4j 1.x can be
+replaced with `PatternLayout` in Log4j 2. The log4j-1.2-api module
+contains two pattern conversions "%ndc" and "%properties" which can be
+used to emulate "%x" and "%X" in Log4j 1.x PatternLayout ("%x" and %X"
+in Log4j 2 have a slightly different format).
+
+Below are the example configurations for Log4j 1.x and their
+counterparts in Log4j 2.
+
+=== Sample 1 - Simple configuration using a Console Appender
+
+Log4j 1.x XML configuration
+
+.log4j.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
+<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
+ <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </layout>
+ </appender>
+ <category name="org.apache.log4j.xml">
+ <priority value="info" />
+ </category>
+ <Root>
+ <priority value ="debug" />
+ <appender-ref ref="STDOUT" />
+ </Root>
+</log4j:configuration>
+----
+
+Log4j 2 XML configuration
+
+.log4j2.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration>
+ <Appenders>
+ <Console name="STDOUT" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Logger name="org.apache.log4j.xml" level="info"/>
+ <Root level="debug">
+ <AppenderRef ref="STDOUT"/>
+ </Root>
+ </Loggers>
+</Configuration>
+----
+
+=== Sample 2 - Simple configuration using a File Appender, XMLLayout and SimpleLayout
+
+Log4j 1.x XML configuration
+
+.log4j.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+ <appender name="A1" class="org.apache.log4j.FileAppender">
+ <param name="File" value="A1.log" />
+ <param name="Append" value="false" />
+ <layout class="org.apache.log4j.xml.XMLLayout" />
+ </appender>
+ <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
+ <layout class="org.apache.log4j.SimpleLayout" />
+ </appender>
+ <category name="org.apache.log4j.xml">
+ <priority value="debug" />
+ <appender-ref ref="A1" />
+ </category>
+ <root>
+ <priority value ="debug" />
+ <appender-ref ref="STDOUT" />
+ </Root>
+</log4j:configuration>
+----
+
+Log4j 2 XML configuration
+
+.log4j2.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration>
+ <Appenders>
+ <File name="A1" fileName="A1.log" append="false">
+ <Log4j1XmlLayout />
+ </File>
+ <Console name="STDOUT" target="SYSTEM_OUT">
+ <PatternLayout pattern="%level - %m%n"/>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Logger name="org.apache.log4j.xml" level="debug">
+ <AppenderRef ref="A1"/>
+ </Logger>
+ <Root level="debug">
+ <AppenderRef ref="STDOUT"/>
+ </Root>
+ </Loggers>
+</Configuration>
+----
+
+=== Sample 3 - SocketAppender
+
+Log4j 1.x XML configuration. This example from Log4j 1.x is misleading.
+The SocketAppender does not actually use a Layout. Configuring one will
+have no effect.
+
+.log4j.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
+ <appender name="A1" class="org.apache.log4j.net.SocketAppender">
+ <param name="RemoteHost" value="localhost"/>
+ <param name="Port" value="5000"/>
+ <param name="LocationInfo" value="true"/>
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%t %-5p %c{2} - %m%n"/>
+ </layout>
+ </appender>
+ <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </layout>
+ </appender>
+ <category name="org.apache.log4j.xml">
+ <priority value="debug"/>
+ <appender-ref ref="A1"/>
+ </category>
+ <root>
+ <priority value="debug"/>
+ <appender-ref ref="STDOUT"/>
+ </Root>
+</log4j:configuration>
+----
+
+Log4j 2 XML configuration
+
+.log4j2.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration>
+ <Appenders>
+ <Socket name="A1" host="localHost" port="5000">
+ <PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
+ </Socket>
+ <Console name="STDOUT" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Logger name="org.apache.log4j.xml" level="debug">
+ <AppenderRef ref="A1"/>
+ </Logger>
+ <Root level="debug">
+ <AppenderRef ref="STDOUT"/>
+ </Root>
+ </Loggers>
+</Configuration>
+----
+
+=== Sample 4 - AsyncAppender and TTCCLayout
+
+Log4j 1.x XML configuration using the AsyncAppender.
+
+.log4j.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" configDebug="true">
+ <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
+ <appender-ref ref="TEMP"/>
+ </appender>
+ <appender name="TEMP" class="org.apache.log4j.FileAppender">
+ <param name="File" value="temp"/>
+ <layout class="org.apache.log4j.TTCCLayout">
+ <param name="ThreadPrinting" value="true"/>
+ <param name="CategoryPrefixing" value="true"/>
+ <param name="ContextPrinting" value="true"/>
+ </layout>
+ </appender>
+ <root>
+ <priority value="debug"/>
+ <appender-ref ref="ASYNC"/>
+ </Root>
+</log4j:configuration>
+----
+
+Log4j 2 XML configuration.
+
+.log4j2.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="debug">
+ <Appenders>
+ <File name="TEMP" fileName="temp">
+ <PatternLayout pattern="%r [%t] %p %c %notEmpty{%ndc }- %m%n"/>
+ </File>
+ <Async name="ASYNC">
+ <AppenderRef ref="TEMP"/>
+ </Async>
+ </Appenders>
+ <Loggers>
+ <Root level="debug">
+ <AppenderRef ref="ASYNC"/>
+ </Root>
+ </Loggers>
+</Configuration>
+----
+
+=== Sample 5 - AsyncAppender with Console and File
+
+Log4j 1.x XML configuration using the AsyncAppender.
+
+.log4j.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" configDebug="true">
+ <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
+ <appender-ref ref="TEMP"/>
+ <appender-ref ref="CONSOLE"/>
+ </appender>
+ <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </layout>
+ </appender>
+ <appender name="TEMP" class="org.apache.log4j.FileAppender">
+ <param name="File" value="temp"/>
+ <layout class="org.apache.log4j.PatternLayout">
+ <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </layout>
+ </appender>
+ <root>
+ <priority value="debug"/>
+ <appender-ref ref="ASYNC"/>
+ </Root>
+</log4j:configuration>
+----
+
+Log4j 2 XML configuration. Note that the Async Appender should be
+configured after the appenders it references. This will allow it to
+shutdown properly.
+
+.log4j2.xml
+[source,xml]
+----
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="debug">
+ <Appenders>
+ <Console name="CONSOLE" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </Console>
+ <File name="TEMP" fileName="temp">
+ <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
+ </File>
+ <Async name="ASYNC">
+ <AppenderRef ref="TEMP"/>
+ <AppenderRef ref="CONSOLE"/>
+ </Async>
+ </Appenders>
+ <Loggers>
+ <Root level="debug">
+ <AppenderRef ref="ASYNC"/>
+ </Root>
+ </Loggers>
+</Configuration>
+----
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8dea0f4f/src/site/xdoc/manual/migration.xml
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/migration.xml b/src/site/xdoc/manual/migration.xml
deleted file mode 100644
index e503277..0000000
--- a/src/site/xdoc/manual/migration.xml
+++ /dev/null
@@ -1,351 +0,0 @@
-<?xml version="1.0"?>
-<!--
- 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.
--->
-
-<document xmlns="http://maven.apache.org/XDOC/2.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">
- <properties>
- <title>Migrating from Log4j 1.x</title>
- <author email="rgoers@apache.org">Ralph Goers</author>
- </properties>
-
- <body>
- <section name="Migrating from Log4j 1.x">
- <a name="Log4j1.2Bridge"/>
- <subsection name="Using the Log4j 1.x bridge">
- <p>
- Perhaps the simplest way to convert to using Log4j 2 is to replace the log4j 1.x jar file with
- Log4j 2's <code>log4j-1.2-api.jar</code>. However, to use this successfully applications must meet the
- following requirements:
- </p>
- <ol>
- <li>They must not access methods and classes internal to the Log4j 1.x implementation such
- as <code>Appender</code>s, <code>LoggerRepository</code> or <code>Category</code>'s
- <code>callAppenders</code> method.</li>
- <li>They must not programmatically configure Log4j.</li>
- <li>They must not configure by calling the classes <code>DOMConfigurator</code> or
- <code>PropertyConfigurator</code>.</li>
- </ol>
- </subsection>
- <subsection name="Converting to the Log4j 2 API">
- <p>For the most part, converting from the Log4j 1.x API to Log4j 2 should be fairly simple. Many
- of the log statements will require no modification. However, where necessary the following changes must be
- made.</p>
- <ol>
- <li>
- The main package in version 1 is <code>org.apache.log4j</code>, in version 2 it is
- <code>org.apache.logging.log4j</code>
- </li>
- <li>
- Calls to <code>org.apache.log4j.Logger.getLogger()</code> must be modified to
- <code>org.apache.logging.log4j.LogManager.getLogger()</code>.
- </li>
- <li>
- Calls to <code>org.apache.log4j.Logger.getRootLogger()</code> or
- <code>org.apache.log4j.LogManager.getRootLogger()</code> must be replaced with
- <code>org.apache.logging.log4j.LogManager.getRootLogger()</code>.</li>
- <li>
- Calls to <code>org.apache.log4j.Logger.getLogger</code> that accept a <code>LoggerFactory</code> must
- remove the <code>org.apache.log4j.spi.LoggerFactory</code> and use one of Log4j 2's other extension
- mechanisms.
- </li>
- <li>
- Replace calls to <code>org.apache.log4j.Logger.getEffectiveLevel()</code> with
- <code>org.apache.logging.log4j.Logger.getLevel()</code>.
- </li>
- <li>
- Remove calls to <code>org.apache.log4j.LogManager.shutdown()</code>, they are not needed in version 2
- because the Log4j Core now automatically adds a JVM shutdown hook on start up to perform any Core
- clean ups.
- <ol>
- <li>
- Starting in Log4j 2.1, you can specify a custom
- <a class="javadoc" href="../log4j-core/apidocs/org/apache/logging/log4j/core/util/ShutdownCallbackRegistry.html">ShutdownCallbackRegistry</a>
- to override the default JVM shutdown hook strategy.
- </li>
- <li>
- Starting in Log4j 2.6, you can now use <code>org.apache.logging.log4j.LogManager.shutdown()</code>
- to initiate shutdown manually.
- </li>
- </ol>
- </li>
- <li>
- Calls to <code>org.apache.log4j.Logger.setLevel()</code> or similar methods are not supported in the API.
- Applications should remove these. Equivalent functionality is provided in the Log4j 2 implementation
- classes, see <code>org.apache.logging.log4j.core.config.Configurator.setLevel()</code>, but may leave
- the application susceptible to changes in Log4j 2 internals.
- </li>
- <li>
- Where appropriate, applications should convert to use parameterized messages instead of String
- concatenation.
- </li>
- <li>
- <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html"><code>org.apache.log4j.MDC</code></a> and
- <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html"><code>org.apache.log4j.NDC</code></a>
- have been replaced by the <a href="thread-context.html">Thread Context</a>.
- </li>
- </ol>
- </subsection>
- <subsection name="Configuring Log4j 2">
- <p>
- Although the Log4j 2 configuration syntax is different than that of Log4j 1.x, most, if not all, of
- the same functionality is available.
- </p>
- <p>
- Note that system property interpolation via the <code>${foo}</code> syntax has been extended to allow
- property lookups from many different sources. See the <a href="lookups.html">Lookups</a> documentation
- for more details. For example, using a lookup for the system property named <code>catalina.base</code>,
- in Log4j 1.x, the syntax would be <code>${catalina.base}</code>. In Log4j 2, the syntax would be
- <code>${sys:catalina.base}</code>.
- </p>
- <p>
- Log4j 1.x has a XMLLayout which is different from the XmlLayout in Log4j 2, the log4j-1.2-api module
- contains a <code>Log4j1XmlLayout</code> which produce output in the format as in Log4j 1.x.
- The Log4j 1.x <code>SimpleLayout</code> can be emulated with PatternLayout "%level - %m%n".
- The Log4j 1.x <code>TTCCLayout</code> can be emulated with PatternLayout "%r [%t] %p %c %notEmpty{%ndc }- %m%n".
- </p>
- <p>
- Both <code>PatternLayout</code> and <code>EnhancedPatternLayout</code> in Log4j 1.x can be replaced with
- <code>PatternLayout</code> in Log4j 2. The log4j-1.2-api module contains two pattern conversions "%ndc" and "%properties" which
- can be used to emulate "%x" and "%X" in Log4j 1.x PatternLayout ("%x" and %X" in Log4j 2 have a slightly different format).
- </p>
- <p>
- Below are the example configurations for Log4j 1.x and their counterparts in Log4j 2.
- </p>
-
- <h4>Sample 1 - Simple configuration using a Console Appender</h4>
- <p>Log4j 1.x XML configuration</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
-<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
- <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </layout>
- </appender>
- <category name="org.apache.log4j.xml">
- <priority value="info" />
- </category>
- <Root>
- <priority value ="debug" />
- <appender-ref ref="STDOUT" />
- </Root>
-</log4j:configuration>]]></pre>
- <p>Log4j 2 XML configuration</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<Configuration>
- <Appenders>
- <Console name="STDOUT" target="SYSTEM_OUT">
- <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </Console>
- </Appenders>
- <Loggers>
- <Logger name="org.apache.log4j.xml" level="info"/>
- <Root level="debug">
- <AppenderRef ref="STDOUT"/>
- </Root>
- </Loggers>
-</Configuration>]]></pre>
-
- <h4>Sample 2 - Simple configuration using a File Appender, XMLLayout and SimpleLayout</h4>
- <p>Log4j 1.x XML configuration</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
-<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <appender name="A1" class="org.apache.log4j.FileAppender">
- <param name="File" value="A1.log" />
- <param name="Append" value="false" />
- <layout class="org.apache.log4j.xml.XMLLayout" />
- </appender>
- <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.SimpleLayout" />
- </appender>
- <category name="org.apache.log4j.xml">
- <priority value="debug" />
- <appender-ref ref="A1" />
- </category>
- <root>
- <priority value ="debug" />
- <appender-ref ref="STDOUT" />
- </Root>
-</log4j:configuration>]]></pre>
-
- <p>Log4j 2 XML configuration</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<Configuration>
- <Appenders>
- <File name="A1" fileName="A1.log" append="false">
- <Log4j1XmlLayout />
- </File>
- <Console name="STDOUT" target="SYSTEM_OUT">
- <PatternLayout pattern="%level - %m%n"/>
- </Console>
- </Appenders>
- <Loggers>
- <Logger name="org.apache.log4j.xml" level="debug">
- <AppenderRef ref="A1"/>
- </Logger>
- <Root level="debug">
- <AppenderRef ref="STDOUT"/>
- </Root>
- </Loggers>
-</Configuration>]]></pre>
-
- <h4>Sample 3 - SocketAppender</h4>
- <p>Log4j 1.x XML configuration. This example from Log4j 1.x is misleading. The SocketAppender does not
- actually use a Layout. Configuring one will have no effect.</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
-<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
- <appender name="A1" class="org.apache.log4j.net.SocketAppender">
- <param name="RemoteHost" value="localhost"/>
- <param name="Port" value="5000"/>
- <param name="LocationInfo" value="true"/>
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%t %-5p %c{2} - %m%n"/>
- </layout>
- </appender>
- <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </layout>
- </appender>
- <category name="org.apache.log4j.xml">
- <priority value="debug"/>
- <appender-ref ref="A1"/>
- </category>
- <root>
- <priority value="debug"/>
- <appender-ref ref="STDOUT"/>
- </Root>
-</log4j:configuration>]]></pre>
-
- <p>Log4j 2 XML configuration</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<Configuration>
- <Appenders>
- <Socket name="A1" host="localHost" port="5000">
- <PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
- </Socket>
- <Console name="STDOUT" target="SYSTEM_OUT">
- <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </Console>
- </Appenders>
- <Loggers>
- <Logger name="org.apache.log4j.xml" level="debug">
- <AppenderRef ref="A1"/>
- </Logger>
- <Root level="debug">
- <AppenderRef ref="STDOUT"/>
- </Root>
- </Loggers>
-</Configuration>]]></pre>
-
- <h4>Sample 4 - AsyncAppender and TTCCLayout</h4>
- <p>Log4j 1.x XML configuration using the AsyncAppender.</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
-<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" configDebug="true">
- <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
- <appender-ref ref="TEMP"/>
- </appender>
- <appender name="TEMP" class="org.apache.log4j.FileAppender">
- <param name="File" value="temp"/>
- <layout class="org.apache.log4j.TTCCLayout">
- <param name="ThreadPrinting" value="true"/>
- <param name="CategoryPrefixing" value="true"/>
- <param name="ContextPrinting" value="true"/>
- </layout>
- </appender>
- <root>
- <priority value="debug"/>
- <appender-ref ref="ASYNC"/>
- </Root>
-</log4j:configuration>]]></pre>
-
- <p>Log4j 2 XML configuration. </p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<Configuration status="debug">
- <Appenders>
- <File name="TEMP" fileName="temp">
- <PatternLayout pattern="%r [%t] %p %c %notEmpty{%ndc }- %m%n"/>
- </File>
- <Async name="ASYNC">
- <AppenderRef ref="TEMP"/>
- </Async>
- </Appenders>
- <Loggers>
- <Root level="debug">
- <AppenderRef ref="ASYNC"/>
- </Root>
- </Loggers>
-</Configuration>]]></pre>
-
-
- <h4>Sample 5 - AsyncAppender with Console and File</h4>
- <p>Log4j 1.x XML configuration using the AsyncAppender.</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
-<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" configDebug="true">
- <appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
- <appender-ref ref="TEMP"/>
- <appender-ref ref="CONSOLE"/>
- </appender>
- <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </layout>
- </appender>
- <appender name="TEMP" class="org.apache.log4j.FileAppender">
- <param name="File" value="temp"/>
- <layout class="org.apache.log4j.PatternLayout">
- <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </layout>
- </appender>
- <root>
- <priority value="debug"/>
- <appender-ref ref="ASYNC"/>
- </Root>
-</log4j:configuration>]]></pre>
-
- <p>Log4j 2 XML configuration. Note that the Async Appender should be configured after the appenders it
- references. This will allow it to shutdown properly.</p>
- <pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<Configuration status="debug">
- <Appenders>
- <Console name="CONSOLE" target="SYSTEM_OUT">
- <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </Console>
- <File name="TEMP" fileName="temp">
- <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
- </File>
- <Async name="ASYNC">
- <AppenderRef ref="TEMP"/>
- <AppenderRef ref="CONSOLE"/>
- </Async>
- </Appenders>
- <Loggers>
- <Root level="debug">
- <AppenderRef ref="ASYNC"/>
- </Root>
- </Loggers>
-</Configuration>]]></pre>
- </subsection>
- </section>
- </body>
-</document>