使用Gelf的Java GrayLog集成

时间:2019-04-11 13:21:15

标签: java log4j log4net-appender graylog

我想通过使用“ gelf”库在Java中进行Graylog集成。我添加了配置参数,以便将graylog服务器连接到“ log4j_Appender.xml”

 <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="info" packages="org.graylog2.log4j2">
     <Appenders>
      <GELF name="gelfAppender" server="graylog.llm.com" port="12212" hostname="llmtest.llm.com" protocol="UDP" includeThreadContext="true">
       <KeyValuePair key="grayloggroup" value="LLMFIGHTTEST" />
       <KeyValuePair key="application" value="baggagebustest" />
       <KeyValuePair key="environment" value="baggagebustest" />

    <!--    <Filters> -->
    <!--     <Filter type="MakerFilter" marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"></Filter> -->
    <!--     <Filter type="MakerFilter" marker="EXCEPTION" onMatch="DENY" onMismatch="ACCEPT"></Filter> -->
    <!--    </Filters> -->
       <PatternLayout pattern="%m%n"/>
      </GELF>
     </Appenders>
     <Loggers>
      <Root level="info">
       <AppenderRef ref="gelfAppender" />
      </Root>
     </Loggers>
    </Configuration>

但是我无法连接到“ graylog.llm.com”,也没有在“ LLMFIGHTTEST”流中添加日志。

我正在github中使用this项目。

1 个答案:

答案 0 :(得分:0)

我们应该在log4j.xml文件中使用添加附加器,并将log4j2-gelf依赖项添加为maven依赖项。

Dependency,

<dependency> <groupId>org.graylog2.log4j2</groupId> <artifactId>log4j2-gelf</artifactId> <version>1.3.2-SNAPSHOT</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" packages="org.graylog2.log4j2">
 <Appenders>
  <GELF name="gelfAppender" server="graylog.test.com" port="12212" hostname="test.th.com" protocol="UDP" includeThreadContext="true">

    <KeyValuePair key="ORDERID" value="$${ctx:ORDERID}"/>

   <Filters>
    <Filter type="MakerFilter" marker="PARENT" onMatch="DENY" onMismatch="NEUTRAL"></Filter>
    <Filter type="MakerFilter" marker="TEST" onMatch="DENY" onMismatch="ACCEPT"></Filter>
   </Filters>
  <PatternLayout pattern="ORDERID : $${ctx:ORDERID} %msg%n"  />

  </GELF>
  <Console name="STDOUT" target="SYSTEM_OUT">
   <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%c{1.}] - %m%n" />
  </Console>
 </Appenders>
 <Loggers>
  <Root level="info">
   <AppenderRef ref="gelfAppender" />
   <AppenderRef ref="STDOUT" />
    <AppenderRef ref="Error-Appender" level="error"/>
  </Root>
 </Loggers>
</Configuration>