为什么我无法通过带有FailoverAppender的AsyncLogger / AsyncAppender向Kafka发送消息?

时间:2019-08-08 03:51:38

标签: java spring spring-boot log4j2

我正在尝试编写一个演示来测试 asynclog ,但我意识到它无法向kafka发送消息。我应该怎么做才能使其正常工作?代码将粘贴在后面。

添加:这种情况应该告诉您。当我今天在异步测试主类中添加了Thread.sleep(1000)可行但不会触发故障转移引用附加器时,无论我将kafka syncSend设置为true / false都没有用。
 
这是我想问的其他一些问题:)

  1. 如果我使用异步方式将日志发送到kafka作为审核日志记录框架,是否可以 保证可靠性
    因为在这里log4j2表示
    “建议同步记录这些审核消息。”

  2. 同步情况下,我使用了 failoverappender 来保存丢失的消息 本地磁盘文件。但是我可能有更好的方法将这些消息重新发送到 kafka?它在异步情况下工作吗? 因为现在我无法删除文件,所以我使用filebeat进行扫描 该文件并发送到logstash。

  3. 我对log4j2做了一个Util to Packaging接口,这样就无法在log4j2.xml PatternLayout中获得正确的位置消息,例如“%C%M%F%L”,然后使用
    "Thread.currentThread().getStackTrace()[3];"
    在我的util类中获取这些消息(内部调用了其他方法,因此索引为3),并通过String来发送日志消息,如下所示: "logger.error(MarkerManager.getMarker("MESSAGE"),"Class: {} Method: {} File: {} Line: {} ---{}",X,X,X,X,JSON.toJSONString(X));"
    我可以用更好的方法优化这些代码并获取这些位置信息吗?

我在Windows操作系统上进行测试,我使用的是Java语言,春季启动项目。 这里有一些基本的pom依赖项:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>

log4j2.xml:

<configuration status="debug" monitorInterval="10">
    <properties>
        <property name="LOG_HOME">logs/TESTPROJECT</property>
        <property name="PATTERN_LAYOUT_CONSOLE">%d{DEFAULT} [%t] %-5level --- %msg %xEx%n</property>
        <property name="PATTERN_LAYOUT_CONSOLE_ROOT">%d{DEFAULT} [%t] %-5level --- Class: %C Method: %M File: %F Line:
            %L --- %msg %xEx%n
        </property>
        <property name="PATTERN_LAYOUT">%d{DEFAULT} [%t] %-5level --- %msg %xEx%n</property>
    </properties>
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout charset="utf-8">
                <MarkerPatternSelector defaultPattern="${PATTERN_LAYOUT_CONSOLE_ROOT}">
                    <PatternMatch key="UTIL" pattern="${PATTERN_LAYOUT_CONSOLE}"/>
                    <PatternMatch key="TRACE" pattern="${PATTERN_LAYOUT_CONSOLE}"/>
                    <PatternMatch key="MESSAGE" pattern="${PATTERN_LAYOUT_CONSOLE}"/>
                </MarkerPatternSelector>
            </PatternLayout>
        </Console>
        <RollingRandomAccessFile name="RollingRandomAccessFileInfo" fileName="${LOG_HOME}/info.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/info/info-%d{yyyy-MM-dd}-%i.log.gz"
                                 bufferSize="4096" immediateFlush="false" bufferedIO="true" append="true">
            <Filters>
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="INFO"/>
            </Filters>
            <PatternLayout charset="utf-8">
                <MarkerPatternSelector defaultPattern="${PATTERN_LAYOUT_CONSOLE_ROOT}">
                    <PatternMatch key="UTIL" pattern="${PATTERN_LAYOUT}"/>
                    <PatternMatch key="TRACE" pattern="${PATTERN_LAYOUT}"/>
                    <PatternMatch key="MESSAGE" pattern="${PATTERN_LAYOUT}"/>
                </MarkerPatternSelector>
            </PatternLayout>
            <Policies>
                <CronTriggeringPolicy schedule="0 0 0/12 * * ?"/>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="50">
                <Delete basePath="${LOG_HOME}" testMode="true" maxDepth="5">
                    <IfFileName glob="*/*/info/info-*.log.gz">
                        <IfLastModified age="180d">
                            <IfAny>
                                <IfAccumulatedFileSize exceeds="50GB"/>
                                <IfAccumulatedFileCount exceeds="1000"/>
                            </IfAny>
                        </IfLastModified>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="RollingRandomAccessFileWarn" fileName="${LOG_HOME}/warn.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/warn/warn-%d{yyyy-MM-dd}-%i.log.gz"
                                 bufferSize="4096" immediateFlush="false" bufferedIO="true" append="true">
            <Filters>
                <ThresholdFilter level="WARN"/>
                <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <PatternLayout charset="utf-8">
                <MarkerPatternSelector defaultPattern="${PATTERN_LAYOUT_CONSOLE_ROOT}">
                    <PatternMatch key="UTIL" pattern="${PATTERN_LAYOUT}"/>
                    <PatternMatch key="TRACE" pattern="${PATTERN_LAYOUT}"/>
                    <PatternMatch key="MESSAGE" pattern="${PATTERN_LAYOUT}"/>
                </MarkerPatternSelector>
            </PatternLayout>
            <Policies>
                <CronTriggeringPolicy schedule="0 0 0/12 * * ?"/>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="50">
                <Delete basePath="${LOG_HOME}" testMode="true" maxDepth="5">
                    <IfFileName glob="*/*/warn/warn*.log.gz">
                        <IfLastModified age="180d">
                            <IfAny>
                                <IfAccumulatedFileSize exceeds="50GB"/>
                                <IfAccumulatedFileCount exceeds="1000"/>
                            </IfAny>
                        </IfLastModified>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="RollingRandomAccessFileError" fileName="${LOG_HOME}/error.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/error/error-%d{yyyy-MM-dd}-%i.log.gz"
                                 append="true">
            <ThresholdFilter level="ERROR"/>
            <PatternLayout charset="utf-8">
                <MarkerPatternSelector defaultPattern="${PATTERN_LAYOUT_CONSOLE_ROOT}">
                    <PatternMatch key="UTIL" pattern="${PATTERN_LAYOUT}"/>
                    <PatternMatch key="TRACE" pattern="${PATTERN_LAYOUT}"/>
                    <PatternMatch key="MESSAGE" pattern="${PATTERN_LAYOUT}"/>
                </MarkerPatternSelector>
            </PatternLayout>
            <Policies>
                <CronTriggeringPolicy schedule="0 0 0/12 * * ?"/>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="50">
                <Delete basePath="${LOG_HOME}" testMode="true" maxDepth="5">
                    <IfFileName glob="*/*/error/error*.log.gz">
                        <IfLastModified age="180d">
                            <IfAny>
                                <IfAccumulatedFileSize exceeds="50GB"/>
                                <IfAccumulatedFileCount exceeds="1000"/>
                            </IfAny>
                        </IfLastModified>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
        <Kafka name="KafkaSync" topic="yi-test" syncSend="false" ignoreExceptions="false">
            <MarkerFilter marker="MESSAGE" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout charset="utf-8" pattern="${PATTERN_LAYOUT}"/>
            <ThresholdFilter level="ERROR"/>
            <Property name="bootstrap.servers">192.100.2.44:9092,192.100.2.45:9092</Property>
            <Property name="timeout.ms">3000</Property>
            <Property name="max.block.ms">2000</Property>
        </Kafka>
        <Kafka name="KafkaTrace" topic="yi-test2" syncSend="false" ignoreExceptions="false">
            <MarkerFilter marker="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout charset="utf-8" pattern="${PATTERN_LAYOUT}"/>
            <ThresholdFilter level="ERROR"/>
            <Property name="bootstrap.servers">192.100.2.44:9092,192.100.2.45:9092</Property>
            <Property name="timeout.ms">3000</Property>
            <Property name="max.block.ms">2000</Property>
        </Kafka>
        <File name="failoverKafkaLog" fileName="${LOG_HOME}/failover.log"
              ignoreExceptions="false">
            <MarkerFilter marker="MESSAGE" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout charset="utf-8" pattern="${PATTERN_LAYOUT}"/>
        </File>
        <File name="failoverKafkaTraceLog" fileName="${LOG_HOME}/failoverTrace.log"
              ignoreExceptions="false">
            <MarkerFilter marker="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout charset="utf-8" pattern="${PATTERN_LAYOUT}"/>
        </File>
        <Failover name="Failover" primary="KafkaSync" retryIntervalSeconds="15">
            <Failovers>
                <appender-ref ref="failoverKafkaLog"/>
            </Failovers>
        </Failover>
        <Failover name="FailoverTrace" primary="KafkaTrace" retryIntervalSeconds="15">
            <Failovers>
                <appender-ref ref="failoverKafkaTraceLog"/>
            </Failovers>
        </Failover>
        <Async name="AsyncKafka">
            <appender-ref ref="Failover"/>
            <appender-ref ref="FailoverTrace"/>
        </Async>
    </appenders>
    <loggers>
        <AsyncLogger name="com.logtest.demo" level="debug" includeLocation="true" additivity="false">
            <!--i have tried all these ref situations and no one work -->
            <appender-ref ref="Failover"/>
            <appender-ref ref="FailoverTrace"/>
            <!--<appender-ref ref="KafkaSync"/>-->
            <!--<appender-ref ref="KafkaTrace"/>-->
            <!--<appender-ref ref="AsyncKafka"/>-->
            <appender-ref ref="RollingRandomAccessFileInfo"/>
            <appender-ref ref="RollingRandomAccessFileWarn"/>
            <appender-ref ref="RollingRandomAccessFileError"/>
        </AsyncLogger>
        <root level="info" includeLocation="true">
            <appender-ref ref="Console"/>
        </root>
        <Logger name="org.apache.kafka" level="INFO"/> <!-- avoid recursive logging -->
    </loggers>
</configuration>

这是运行测试类并将log4j2配置级别设置为debug时的日志: 我推送到我的github mygithub的日志文件 因为我限制了身体的限制:(

0 个答案:

没有答案