请帮我解决一个问题.Thx! :) 我想通过使用log4j2从tomcat输出日志,然后这些日志可以输出到kafka。 我在tomcat / lib中的log4j2.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<Properties>
<Property name="CATALINA_BASE_LOGPATH">../logs</Property>
<Property name="logLocalIP">10.11.12.13</Property>
</Properties>
<appenders>
<console name="CONSOLE" target="SYSTEM_OUT">
<patternLayout charset="UTF-8"
pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
</console>
<rollingFile name="CATALINA"
fileName="${CATALINA_BASE_LOGPATH}/catalina.log"
filePattern="${CATALINA_BASE_LOGPATH}/catalina.%date{yyyyMMdd}">
<patternLayout charset="UTF-8"
pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
<policies>
<timeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="100 MB"/>
</policies>
</rollingFile>
<rollingFile name="LOCALHOST"
fileName="${CATALINA_BASE_LOGPATH}/localhost.log"
filePattern="${CATALINA_BASE_LOGPATH}/localhost.%date{yyyyMMdd}">
<patternLayout charset="UTF-8"
pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
<policies>
<timeBasedTriggeringPolicy />
</policies>
</rollingFile>
<rollingFile name="MANAGER"
fileName="${CATALINA_BASE_LOGPATH}/manager.log"
filePattern="${CATALINA_BASE_LOGPATH}/manager.%date{yyyyMMdd}">
<patternLayout charset="UTF-8"
pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
<policies>
<timeBasedTriggeringPolicy />
</policies>
</rollingFile>
<rollingFile name="HOST-MANAGER"
fileName="${CATALINA_BASE_LOGPATH}/host-manager.log"
filePattern="${CATALINA_BASE_LOGPATH}/host-manager.%date{yyyyMMdd}">
<patternLayout charset="UTF-8"
pattern="%date{yyyy-MM-dd HH:mm:ss}[%-5level]%logger[%L]-%message%n" />
<policies>
<timeBasedTriggeringPolicy />
</policies>
</rollingFile>
<Kafka name="KAFKA_DEBUG" topic="kafka">
<filters>
<thresholdFilter level="DEBUG" onMatch="ACCEPT"
onMismatch="DENY" />
</filters>
<patternLayout charset="UTF-8"
pattern="%date{yyyy-MM-dd HH:mm:ss}||%-5level||%logger||Tomcat||${logLocalIP}||%mdc{logRemoteIP}||%ex{short.className}.%ex{short.methodName}[%ex{short.lineNumber}]||%message||%ex{1}%n" >
</patternLayout>
<Property name="bootstrap.servers">localhost:9092</Property>
</Kafka>
</appenders>
<loggers>
<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="INFO">
<appender-ref ref="LOCALHOST" />
</logger>
<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]" level="INFO" additivity="false">
<appender-ref ref="MANAGER" />
</logger>
<logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]" level="INFO" additivity="false">
<appender-ref ref="HOST-MANAGER" />
</logger>
<Logger name="org.apache.kafka" level="INFO" /><!-- avoid recursive logging -->
<root level="INFO">
<appender-ref ref="CATALINA" />
<appender-ref ref="KAFKA_DEBUG" />
<appender-ref ref="CONSOLE" />
</root>
</loggers>
</configuration>
当我运行tomcat时,日志不会更改为log4j,如下所示:
十二月 29, 2017 11:18:09 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version: Apache Tomcat/8.0.48
十二月 29, 2017 11:18:09 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built: Nov 30 2017 16:26:50 UTC
十二月 29, 2017 11:18:09 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number: 8.0.48.0
十二月 29, 2017 11:18:09 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name: Linux
十二月 29, 2017 11:18:09 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version: 4.10.0-42-generic
但是如果删除<Kafka>...</Kafka>
appender,日志会变为log4j:
2017-12-29 11:50:29,379 main ERROR Unable to locate appender "KAFKA_DEBUG" for logger config "root"
2017-12-29 11:50:29[INFO ]org.apache.catalina.startup.VersionLoggerListener[89]-Server version: Apache Tomcat/8.0.48
2017-12-29 11:50:29[INFO ]org.apache.catalina.startup.VersionLoggerListener[91]-Server built: Nov 30 2017 16:26:50 UTC
2017-12-29 11:50:29[INFO ]org.apache.catalina.startup.VersionLoggerListener[93]-Server number: 8.0.48.0
2017-12-29 11:50:29[INFO ]org.apache.catalina.startup.VersionLoggerListener[95]-OS Name: Linux
2017-12-29 11:50:29[INFO ]org.apache.catalina.startup.VersionLoggerListener[97]-OS Version: 4.10.0-42-generic
2017-12-29 11:50:29[INFO ]org.apache.catalina.startup.VersionLoggerListener[99]-Architecture: amd64
我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kafkaweb</groupId>
<artifactId>kafkaweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>0.8.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.8.2.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-streams -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>0.10.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-jcl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>
</project>
log deosn&t输出到kafka tomcat版本:apache-tomcat-8.0.48