5

我们有一个日志记录流程,我们的 java 应用程序填充一个 MDC,该 MDC 通过 log4j2 传输到 syslog 到中央 rsyslog 安装。在这里,我们广泛使用 MDC。因此,我们的设置是:

   <Syslog name="syslog" format="RFC5424" host="localhost" port="514" protocol="UDP"
        appName="messaging_platform.${application}" mdcId="mdc" includeMDC="true" facility="LOCAL5" connectTimeoutMillis="100" ignoreExceptions="false">
    <LoggerFields>
        <KeyValuePair key="class" value="%c"/>
        <KeyValuePair key="classname" value="%c{1}"/>
        <KeyValuePair key="exception" value="%ex{full}"/>
        <KeyValuePair key="method" value="%method"/>
        <KeyValuePair key="line" value="%line"/>
        <KeyValuePair key="application_name" value="${application}"/>
        <KeyValuePair key="sequenceNumber" value="%sequenceNumber"/>
        <KeyValuePair key="application_version" value="${application.version}"/>
        <KeyValuePair key="marker" value="%marker"/>
        <KeyValuePair key="thread" value="%thread"/>
        <KeyValuePair key="system_nano_time" value="%nano"/>
        <KeyValuePair key="app_uptime" value="%relative"/>
    </LoggerFields>
</Syslog>

我正在尝试将其转换为使用 graylog,似乎 GELF 是推荐的传输协议。我找到了多个库来执行此操作,并从GelfLayoutlog4j2 的内置开始。但这不支持LoggerFields

那么将这些字段放入 Graylog 的推荐方法是什么?如果我做

   <Socket name="Graylog" protocol="udp" host="localhost" port="12201">
        <GelfLayout host="localhost" compressionType="GZIP" compressionThreshold="1024">
            <KeyValuePair key="class" value="%c"/>
            <KeyValuePair key="classname" value="%c{1}"/>
            <KeyValuePair key="exception" value="%ex{full}"/>
            <KeyValuePair key="method" value="%method"/>
            <KeyValuePair key="line" value="%line"/>
            <KeyValuePair key="application_name" value="${application}"/>
            <KeyValuePair key="sequenceNumber" value="%sequenceNumber"/>
            <KeyValuePair key="application_version" value="${application.version}"/>
            <KeyValuePair key="marker" value="%marker"/>
            <KeyValuePair key="thread" value="%thread"/>
            <KeyValuePair key="system_nano_time" value="%nano"/>
            <KeyValuePair key="app_uptime" value="%relative"/>
        </GelfLayout>
    </Socket>

我在 Graylog 中得到了一堆字段,但值没有扩展。

4

2 回答 2

2

我找到了一个解决方案logstash-gelf

   <Gelf name="gelf" host="udp:localhost" port="12201" version="1.1" extractStackTrace="true"
          filterStackTrace="true" mdcProfiling="true" includeFullMdc="true" maximumMessageSize="8192"
          originHost="%host{fqdn}">
        <Field name="class" pattern="%c"/>
        <Field name="classname" pattern="%c{1}"/>
        <Field name="exception" pattern="%ex{full}"/>
        <Field name="method" pattern="%method"/>
        <Field name="line" pattern="%line"/>
        <Field name="application_name" pattern="${application}"/>
        <Field name="sequenceNumber" pattern="%sequenceNumber"/>
        <Field name="application_version" pattern="${application.version}"/>
        <Field name="marker" pattern="%marker"/>
        <Field name="thread" pattern="%thread"/>
        <Field name="system_nano_time" pattern="%nano"/>
        <Field name="app_uptime" pattern="%relative"/>
        <Field name="severity" pattern="%level{WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace, INFO=Info}"/>
    </Gelf>
于 2016-12-02T12:04:59.173 回答
-1

我使用具有以下 logback 配置的 logstash-gelf;

logback.xml 配置:

<appender name="NameOfAppender" class="biz.paluch.logging.gelf.logback.GelfLogbackAppender">
    <host>udp:10.123.33.41</host> <!-- graylog endpoint ip here -->
    <port>12201</port> <!-- graylog endpoint port here -->
    <version>1.1</version>
    <facility>java-prod</facility>
    <extractStackTrace>true</extractStackTrace>
    <filterStackTrace>true</filterStackTrace>
    <mdcProfiling>true</mdcProfiling>
    <timestampPattern>yyyy-MM-dd HH:mm:ss,SSSS</timestampPattern>
    <maximumMessageSize>8192</maximumMessageSize>

    <additionalFields>Env=prod,Operation=INTG_PERF</additionalFields>

    <!-- This are fields using MDC -->
    <mdcFields>RequestTime,ResponseTime,ElapsedTime,ServiceType,ServiceIdentifier,Endpoint,Method,Status</mdcFields>

    <dynamicMdcFields>mdc.*,(mdc|MDC)fields</dynamicMdcFields>

    <includeFullMdc>false</includeFullMdc>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
</appender>

<logger name="NameOfLogger" level="INFO">
    <appender-ref ref="NameOfAppender" />
</logger> 

java日志记录示例:

    org.slf4j.Logger logger = LoggerFactory.getLogger("NameOfLogger");

    MDC.put("RequestTime", new Date().toString());
    MDC.put("ResponseTime", new Date().toString());
    MDC.put("ElapsedTime", "232");
    MDC.put("ServiceType", "BANK");
    MDC.put("ServiceIdentifier", "XBANK");
    MDC.put("Endpoint", "https://www..");
    MDC.put("Method", "doSale");
    MDC.put("Status", "SUCCEED");

    logger.info("WS INTEGRATION PERF");

maven依赖:

    <dependency>
        <groupId>biz.paluch.logging</groupId>
        <artifactId>logstash-gelf</artifactId>
        <version>1.11.1</version>
    </dependency>
于 2019-10-04T12:42:24.543 回答