如何通过gelf将MDC转移到graylog?

时间:2016-12-02 11:15:39

标签: log4j2 graylog2 mdc gelf

我们有一个日志记录流,我们的java应用程序填充了一个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是推荐的传输协议。我发现有多个库可以执行此操作,并从log4j2的内置GelfLayout开始。但这不支持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中获得了一堆字段,但这些值没有扩展。

2 个答案:

答案 0 :(得分:1)

我找到了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>

答案 1 :(得分:-2)

我将logstash-gelf与以下logback配置一起使用;

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>