卡夫卡在log4j2 appender。xml不工作

时间:2021-12-03 21:50:02

Please help me to solve a problem.Thx! :) I want to output the log from tomcat by using log4j2, and then these logs could be outputed to kafka. My log4j2.xml in tomcat/lib is:

请帮我解决一个问题。:)我想用log4j2将日志从tomcat输出,然后这些日志可以输出到kafka。我的log4j2。xml在tomcat / lib是:

<?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>

When I run the tomcat, log doesn't change to log4j, looks like:

当我运行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

But if I delete the <Kafka>...</Kafka> appender, the logs change to log4j:

但是如果我删除 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

my pom.xml:

我的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 output to kafka tomcat version:apache-tomcat-8.0.48

log deosn不输出到kafka tomcat版本:apache-tomcat-8.0.48。

1 个解决方案

#1


0  

You can use Kafka logstash plugin to send anything to Kafka and rum your consumers to consume the messages. For reference Link

您可以使用Kafka日志隐藏插件将任何东西发送给Kafka,并向您的消费者发送数据来消费这些消息。链接供参考

#1


0  

You can use Kafka logstash plugin to send anything to Kafka and rum your consumers to consume the messages. For reference Link

您可以使用Kafka日志隐藏插件将任何东西发送给Kafka,并向您的消费者发送数据来消费这些消息。链接供参考