操作方法:配置 Tomcat 9 以通过 Log4j2 登录
How-To: Configure Tomcat 9 to log via Log4j2
如何将 Tomcat 9 的内部日志记录(catalina 和本地主机)重定向到 Log4j2?
虽然有许多适用于旧版本 Tomcat 和 Log4j 的指南,但我找不到任何 "complete" 关于 Tomcat 9 和 Log4j2 的内容; Apache Tomcat 9 documentation points to "the instructions provided by the alternative logging framework", and the Apache Log4j documentation 状态(2 中的 jar 名称。更正):
Log4j may be used as the logging framework for Apache Tomcat. This
support is implemented automatically by including the log4j-api,
log4j-core, and log4j-appserver jars in the boot classpath. A file
named log4j2-tomcat.xml, log4j2-tomcat.json, log4j2-tomcat.yaml,
log4j2-tomcat.yml, or log4j2-tomcat.properties must also be placed in
the boot classpath. This is most easily done by:
- Creating a set of directories in catalina home named log4j2/lib and log4j2/conf.
- Placing log4j-api-2.12.0.jar, log4j-core-2.12.0.jar, and log4j-appserver-2.12.0.jar in the log4j2/lib directory.
- Creating a file named log4j2-tomcat.xml, log4j2-tomcat.json, log4j2-tomcat.yaml, log4j2-tomcat.yml, or log4j2-tomcat.properties in
the log4j2/conf directory.
- Create or modify setenv.sh in the tomcat bin directory to include
CLASSPATH=$CATALINA_HOME/log4j2/lib/*:$CATALINA_HOME/log4j2/conf
但是要在 log4j2-tomcat.* 配置文件中放入什么?
我在 Apache Tomcat 7 documentation 中找到了一个示例属性文件,但由于这是为与 Log4j 1.x 一起使用而设计的,因此我必须使其适应 Log4j2 属性文件语法。这是结果:
# 'status' refers to log messages from Log4j2 itself
monitorInterval = 30
status = warn
property.loglevel.catalina = info
property.loglevel.localhost = info
property.layoutPattern.catalina = %d %-5p [%t] %-22.22c{1} %m%n
property.layoutPattern.localhost = %d %-5p [%t] %-30.30c{1} %m%n
# Roll-over the logs once per month using CronTriggerPolicy.
property.fileDatePattern.catalina = %d{yyyy-MM}
property.fileDatePattern.localhost = %d{yyyy-MM}
property.cronTriggerSchedule.catalina = 0 0 0 1 * ?
property.cronTriggerSchedule.localhost = 0 0 0 1 * ?
## Appenders
# N.B.: - No need to specify 'appenders = CATALINA, LOCALHOST, CONSOLE'
# since these identifiers do not contain '.' characters.
# - The sub-component identifiers 'policies' and 'cron' are arbitrarily
# chosen; the actual type is specified via the 'type' attribute.
# - 'DirectWriteRolloverStrategy' is used automatically since no 'fileName' specified.
appender.CATALINA.type = RollingFile
appender.CATALINA.name = RollingFile-CATALINA
appender.CATALINA.filePattern = ${sys:catalina.base}/logs/catalina.${fileDatePattern.catalina}.log
appender.CATALINA.layout.type = PatternLayout
appender.CATALINA.layout.pattern = ${layoutPattern.catalina}
appender.CATALINA.policies.type = Policies
appender.CATALINA.policies.cron.type = CronTriggeringPolicy
appender.CATALINA.policies.cron.schedule = ${cronTriggerSchedule.catalina}
appender.CATALINA.policies.cron.evaluateOnStartup = true
appender.CATALINA.filePermissions = rw-r-----
appender.CATALINA.fileOwner = tomcat
appender.CATALINA.fileGroup = adm
appender.LOCALHOST.type = RollingFile
appender.LOCALHOST.name = RollingFile-LOCALHOST
appender.LOCALHOST.filePattern = ${sys:catalina.base}/logs/localhost.${fileDatePattern.localhost}.log
appender.LOCALHOST.layout.type = PatternLayout
appender.LOCALHOST.layout.pattern = ${layoutPattern.localhost}
appender.LOCALHOST.policies.type = Policies
appender.LOCALHOST.policies.cron.type = CronTriggeringPolicy
appender.LOCALHOST.policies.cron.schedule = ${cronTriggerSchedule.localhost}
appender.LOCALHOST.policies.cron.evaluateOnStartup = true
appender.LOCALHOST.filePermissions = rw-r-----
appender.LOCALHOST.fileOwner = tomcat
appender.LOCALHOST.fileGroup = adm
# Uncomment if you want to keep logging to catalina.out after Log4j2 takes over.
#appender.CONSOLE.type = Console
#appender.CONSOLE.name = STDOUT
#appender.CONSOLE.layout.type = PatternLayout
## Configure which loggers log to which appenders
rootLogger.level = ${loglevel.catalina}
rootLogger.appenderRef.CATALINA.ref = RollingFile-CATALINA
#rootLogger.appenderRef.stdout.ref = STDOUT
# Here, the identifier does contain '.' characters, so we must specify the list.
loggers = org.apache.catalina.core.ContainerBase.[Catalina].[localhost]
logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].name = LOCALHOST
logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = ${loglevel.localhost}
logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].appenderRef.LOCALHOST.ref = RollingFile-LOCALHOST
我使用 Log4j2 的主要原因是能够每月进行日志轮换,但您可以轻松地随心所欲地调整一切,甚至无需重新启动 Tomcat。
为了使用 Tomcat 9.0.54+ log4j2 设置,请完成以下操作:
- 激活 JDK JUL 桥 - https://logging.apache.org/log4j/2.x/log4j-jul/
To use the JDK Logging Adapter, you must set the system property
java.util.logging.manager to org.apache.logging.log4j.jul.LogManager
This must be done either through the command line (i.e., using the
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager argument) or by using System.setProperty() before any calls are made
to LogManager or Logger.
- 将所有必需的 log4j2 jar 复制到类路径或 /lib
- 配置-Dlog4j.configurationFile=/your/path/logger.xml
像下面的例子
<?xml version="1.0" encoding="utf-8"?>
<!-- ================================================ -->
<!-- Tomcat log4j2 logger -->
<!-- ================================================ -->
<Configuration status="INFO" monitorInterval="60">
<Properties>
<Property name="logsdir">${sys:catalina.base}/logs</Property>
<Property name="layout">[%d][%p][%c:%L:%M] - %m%n</Property>
</Properties>
<Appenders>
<Console name="DEFAULT" target="SYSTEM_OUT">
<PatternLayout pattern="${layout}"/>
</Console>
<Async name="ASYNC-DEFAULT" includeLocation="true">
<AppenderRef ref="DEFAULT"/>
<AppenderRef ref="INTOFILE"/>
</Async>
<RollingFile name="INTOFILE"
fileName="${logsdir}/worker.log"
filePattern="${logsdir}/catalina.%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${layout}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
</Appenders>
<Loggers>
<!-- OFF -->
<!-- ERROR -->
<!-- WARN -->
<!-- INFO -->
<logger name="org.apache.catalina" level="INFO"/>
<logger name="org.apache.catalina.core" level="INFO"/>
<logger name="org.apache.tomcat" level="INFO"/>
<logger name="org.apache.coyote" level="INFO"/>
<logger name="org.apache.jasper" level="INFO"/>
<!-- DEBUG -->
<logger name="org.apache.catalina.startup" level="DEBUG"/>
<logger name="org.apache.tomcat1" level="DEBUG"/>
<Root level="TRACE">
<AppenderRef ref="ASYNC-DEFAULT"/>
</Root>
</Loggers>
</Configuration>
如何将 Tomcat 9 的内部日志记录(catalina 和本地主机)重定向到 Log4j2?
虽然有许多适用于旧版本 Tomcat 和 Log4j 的指南,但我找不到任何 "complete" 关于 Tomcat 9 和 Log4j2 的内容; Apache Tomcat 9 documentation points to "the instructions provided by the alternative logging framework", and the Apache Log4j documentation 状态(2 中的 jar 名称。更正):
Log4j may be used as the logging framework for Apache Tomcat. This support is implemented automatically by including the log4j-api, log4j-core, and log4j-appserver jars in the boot classpath. A file named log4j2-tomcat.xml, log4j2-tomcat.json, log4j2-tomcat.yaml, log4j2-tomcat.yml, or log4j2-tomcat.properties must also be placed in the boot classpath. This is most easily done by:
- Creating a set of directories in catalina home named log4j2/lib and log4j2/conf.
- Placing log4j-api-2.12.0.jar, log4j-core-2.12.0.jar, and log4j-appserver-2.12.0.jar in the log4j2/lib directory.
- Creating a file named log4j2-tomcat.xml, log4j2-tomcat.json, log4j2-tomcat.yaml, log4j2-tomcat.yml, or log4j2-tomcat.properties in the log4j2/conf directory.
- Create or modify setenv.sh in the tomcat bin directory to include
CLASSPATH=$CATALINA_HOME/log4j2/lib/*:$CATALINA_HOME/log4j2/conf
但是要在 log4j2-tomcat.* 配置文件中放入什么?
我在 Apache Tomcat 7 documentation 中找到了一个示例属性文件,但由于这是为与 Log4j 1.x 一起使用而设计的,因此我必须使其适应 Log4j2 属性文件语法。这是结果:
# 'status' refers to log messages from Log4j2 itself
monitorInterval = 30
status = warn
property.loglevel.catalina = info
property.loglevel.localhost = info
property.layoutPattern.catalina = %d %-5p [%t] %-22.22c{1} %m%n
property.layoutPattern.localhost = %d %-5p [%t] %-30.30c{1} %m%n
# Roll-over the logs once per month using CronTriggerPolicy.
property.fileDatePattern.catalina = %d{yyyy-MM}
property.fileDatePattern.localhost = %d{yyyy-MM}
property.cronTriggerSchedule.catalina = 0 0 0 1 * ?
property.cronTriggerSchedule.localhost = 0 0 0 1 * ?
## Appenders
# N.B.: - No need to specify 'appenders = CATALINA, LOCALHOST, CONSOLE'
# since these identifiers do not contain '.' characters.
# - The sub-component identifiers 'policies' and 'cron' are arbitrarily
# chosen; the actual type is specified via the 'type' attribute.
# - 'DirectWriteRolloverStrategy' is used automatically since no 'fileName' specified.
appender.CATALINA.type = RollingFile
appender.CATALINA.name = RollingFile-CATALINA
appender.CATALINA.filePattern = ${sys:catalina.base}/logs/catalina.${fileDatePattern.catalina}.log
appender.CATALINA.layout.type = PatternLayout
appender.CATALINA.layout.pattern = ${layoutPattern.catalina}
appender.CATALINA.policies.type = Policies
appender.CATALINA.policies.cron.type = CronTriggeringPolicy
appender.CATALINA.policies.cron.schedule = ${cronTriggerSchedule.catalina}
appender.CATALINA.policies.cron.evaluateOnStartup = true
appender.CATALINA.filePermissions = rw-r-----
appender.CATALINA.fileOwner = tomcat
appender.CATALINA.fileGroup = adm
appender.LOCALHOST.type = RollingFile
appender.LOCALHOST.name = RollingFile-LOCALHOST
appender.LOCALHOST.filePattern = ${sys:catalina.base}/logs/localhost.${fileDatePattern.localhost}.log
appender.LOCALHOST.layout.type = PatternLayout
appender.LOCALHOST.layout.pattern = ${layoutPattern.localhost}
appender.LOCALHOST.policies.type = Policies
appender.LOCALHOST.policies.cron.type = CronTriggeringPolicy
appender.LOCALHOST.policies.cron.schedule = ${cronTriggerSchedule.localhost}
appender.LOCALHOST.policies.cron.evaluateOnStartup = true
appender.LOCALHOST.filePermissions = rw-r-----
appender.LOCALHOST.fileOwner = tomcat
appender.LOCALHOST.fileGroup = adm
# Uncomment if you want to keep logging to catalina.out after Log4j2 takes over.
#appender.CONSOLE.type = Console
#appender.CONSOLE.name = STDOUT
#appender.CONSOLE.layout.type = PatternLayout
## Configure which loggers log to which appenders
rootLogger.level = ${loglevel.catalina}
rootLogger.appenderRef.CATALINA.ref = RollingFile-CATALINA
#rootLogger.appenderRef.stdout.ref = STDOUT
# Here, the identifier does contain '.' characters, so we must specify the list.
loggers = org.apache.catalina.core.ContainerBase.[Catalina].[localhost]
logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].name = LOCALHOST
logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = ${loglevel.localhost}
logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].appenderRef.LOCALHOST.ref = RollingFile-LOCALHOST
我使用 Log4j2 的主要原因是能够每月进行日志轮换,但您可以轻松地随心所欲地调整一切,甚至无需重新启动 Tomcat。
为了使用 Tomcat 9.0.54+ log4j2 设置,请完成以下操作:
- 激活 JDK JUL 桥 - https://logging.apache.org/log4j/2.x/log4j-jul/
To use the JDK Logging Adapter, you must set the system property java.util.logging.manager to org.apache.logging.log4j.jul.LogManager This must be done either through the command line (i.e., using the -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager argument) or by using System.setProperty() before any calls are made to LogManager or Logger.
- 将所有必需的 log4j2 jar 复制到类路径或 /lib
- 配置-Dlog4j.configurationFile=/your/path/logger.xml 像下面的例子
<?xml version="1.0" encoding="utf-8"?>
<!-- ================================================ -->
<!-- Tomcat log4j2 logger -->
<!-- ================================================ -->
<Configuration status="INFO" monitorInterval="60">
<Properties>
<Property name="logsdir">${sys:catalina.base}/logs</Property>
<Property name="layout">[%d][%p][%c:%L:%M] - %m%n</Property>
</Properties>
<Appenders>
<Console name="DEFAULT" target="SYSTEM_OUT">
<PatternLayout pattern="${layout}"/>
</Console>
<Async name="ASYNC-DEFAULT" includeLocation="true">
<AppenderRef ref="DEFAULT"/>
<AppenderRef ref="INTOFILE"/>
</Async>
<RollingFile name="INTOFILE"
fileName="${logsdir}/worker.log"
filePattern="${logsdir}/catalina.%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${layout}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
</Appenders>
<Loggers>
<!-- OFF -->
<!-- ERROR -->
<!-- WARN -->
<!-- INFO -->
<logger name="org.apache.catalina" level="INFO"/>
<logger name="org.apache.catalina.core" level="INFO"/>
<logger name="org.apache.tomcat" level="INFO"/>
<logger name="org.apache.coyote" level="INFO"/>
<logger name="org.apache.jasper" level="INFO"/>
<!-- DEBUG -->
<logger name="org.apache.catalina.startup" level="DEBUG"/>
<logger name="org.apache.tomcat1" level="DEBUG"/>
<Root level="TRACE">
<AppenderRef ref="ASYNC-DEFAULT"/>
</Root>
</Loggers>
</Configuration>