如何通过 gelf 将 MDC 传输到 graylog?
How to transfer MDC via gelf to graylog?
我们有一个日志记录流程,其中我们的 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 似乎是为此推荐的传输协议。我找到了多个库来执行此操作,并从 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 中得到了一堆字段,但这些值没有展开。
我找到了 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>
我使用具有以下 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>
我们有一个日志记录流程,其中我们的 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 似乎是为此推荐的传输协议。我找到了多个库来执行此操作,并从 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 中得到了一堆字段,但这些值没有展开。
我找到了 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>
我使用具有以下 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>