如何使用Log4j2将日志发送到Graylog?

huangapple go评论79阅读模式
英文:

How to send log by Log4j2 to Graylog?

问题

我正试图将一个Java应用程序与Docker上的Graylog服务器集成,但我无法从我的应用程序发送消息到Graylog,有人可以帮助我吗?

这是我的Docker运行命令:

$ docker run --name mongo -d mongo:3
$ docker run --name elasticsearch \
    -e "http.host=0.0.0.0" \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -d docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10
$ docker run --name graylog --link mongo --link elasticsearch \
    -p 9000:9000 -p 12201:12201 -p 1514:1514 \
    -e GRAYLOG_HTTP_EXTERNAL_URI="http://127.0.0.1:9000/" \
    -d graylog/graylog:3.3

这是我在Graylog内的输入配置:

Graylog Input

这是我正在使用的应用程序教程链接:
https://talhature.com/2020/04/25/using-graylog-with-log4j2/

这是我的log4j2.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" monitorInterval="180">
	<Properties>
		<Property name="app-name">graylog-example-fatjar</Property>

		<!-- 根据需要更改log-path -->
		<Property name="log-path">$${sys:application-directory}/${app-name}/log
		</Property>
		<Property name="log-pattern">[%sn] %d{yyyy/MM/dd HH:mm:ss,SSS} [%-6p] [%t]
			%c{3}:%L - %m%n</Property>
	</Properties>

	<!-- 根据需要更改HOST和PORT属性 -->
	<Appenders>
		<Gelf name="gelf" host="udp:127.0.0.1" port="12201"
			version="1.1" extractStackTrace="true" filterStackTrace="true"
			mdcProfiling="true" includeFullMdc="true" maximumMessageSize="8192"
			originHost="%host{fqdn}">

			<!-- 这些字段定义不是强制性的,您可以使用默认值 -->
			<Field name="timestamp" pattern="%d{dd MMM yyyy HH:mm:ss,SSS}" />
			<Field name="level" pattern="%level" />
			<Field name="simpleClassName" pattern="%C{1}" />
			<Field name="className" pattern="%C" />
			<Field name="server" pattern="%host" />
			<Field name="server.fqdn" pattern="%host{fqdn}" />

			<!-- 这些是我的自定义Graylog字段 -->
			<Field name="logStream" literal="MYAWESOMEAPPS" />
			<Field name="projectName" literal="MYAWESOMEPROJECT" />
		</Gelf>
		<RollingFile name="RollingFile"
			fileName="${log-path}/server/${app-name}-server"
			filePattern="${log-path}/server/${app-name}-server-%d{yyyy-MM-dd}-%i.log.gz"
			append="false">
			<PatternLayout>
				<pattern>${log-pattern}</pattern>
			</PatternLayout>
			<Policies>
				<SizeBasedTriggeringPolicy size="100 MB" />
			</Policies>
		</RollingFile>
	</Appenders>

	<Loggers>
		<Root level="INFO" additivity="false" includeLocation="true">
			<AppenderRef ref="RollingFile" />
			<AppenderRef ref="gelf" />
		</Root>
	</Loggers>
</Configuration>

解决方案:

按以下方式启动Docker以打开5555端口:

$ docker run --link mongo --link elasticsearch \
    -p 9000:9000 -p 12201:12201 -p 1514:1514 -p 5555:5555 \
    -e GRAYLOG_HTTP_EXTERNAL_URI="http://127.0.0.1:9000/" \
    -d graylog/graylog:3.3

在我的log4j2.xml中,我更改了第7行:

<Gelf name="Gelf" host="tcp:127.0.0.1" port="5555" version="1.1"

我的Graylog输入(Gelf:TCP):

Graylog Input Gelf TCP

英文:

I'm trying to integrate a java application with a graylog server on the docker. but I'm not able to send messages from my application to graylog, can someone help me?

Here is my Docker Run:

$ docker run --name mongo -d mongo:3
$ docker run --name elasticsearch \
-e &quot;http.host=0.0.0.0&quot; \
-e &quot;ES_JAVA_OPTS=-Xms512m -Xmx512m&quot; \
-d docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10
$ docker run --name graylog --link mongo --link elasticsearch \
-p 9000:9000 -p 12201:12201 -p 1514:1514 \
-e GRAYLOG_HTTP_EXTERNAL_URI=&quot;http://127.0.0.1:9000/&quot; \
-d graylog/graylog:3.3

here is my Input config inside Graylog

Graylog Input

here is the application tutorial i am using:
https://talhature.com/2020/04/25/using-graylog-with-log4j2/

here is my log4j2.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;Configuration status=&quot;TRACE&quot; monitorInterval=&quot;180&quot;&gt;
&lt;Properties&gt;
&lt;Property name=&quot;app-name&quot;&gt;graylog-example-fatjar&lt;/Property&gt;
&lt;!-- CHANGE log-path ACCORDING TO YOUR NEEDS --&gt;
&lt;Property name=&quot;log-path&quot;&gt;$${sys:application-directory}/${app-name}/log
&lt;/Property&gt;
&lt;Property name=&quot;log-pattern&quot;&gt;[%sn] %d{yyyy/MM/dd HH:mm:ss,SSS} [%-6p] [%t]
%c{3}:%L - %m%n&lt;/Property&gt;
&lt;/Properties&gt;
&lt;!-- CHANGE HOST AND PORT PROPERTIES ACCORDING TO YOUR NEEDS --&gt;
&lt;Appenders&gt;
&lt;Gelf name=&quot;gelf&quot; host=&quot;udp:127.0.0.1&quot; port=&quot;12201&quot;
version=&quot;1.1&quot; extractStackTrace=&quot;true&quot; filterStackTrace=&quot;true&quot;
mdcProfiling=&quot;true&quot; includeFullMdc=&quot;true&quot; maximumMessageSize=&quot;8192&quot;
originHost=&quot;%host{fqdn}&quot;&gt;
&lt;!-- THESE FIELD DEFINITIONS ARE NOT MANDATORY, YOU CAN USE DEFAULTS --&gt;
&lt;Field name=&quot;timestamp&quot; pattern=&quot;%d{dd MMM yyyy HH:mm:ss,SSS}&quot; /&gt;
&lt;Field name=&quot;level&quot; pattern=&quot;%level&quot; /&gt;
&lt;Field name=&quot;simpleClassName&quot; pattern=&quot;%C{1}&quot; /&gt;
&lt;Field name=&quot;className&quot; pattern=&quot;%C&quot; /&gt;
&lt;Field name=&quot;server&quot; pattern=&quot;%host&quot; /&gt;
&lt;Field name=&quot;server.fqdn&quot; pattern=&quot;%host{fqdn}&quot; /&gt;
&lt;!-- THESE ARE MY CUSTOM GRAYLOG FIELDS --&gt;
&lt;Field name=&quot;logStream&quot; literal=&quot;MYAWESOMEAPPS&quot; /&gt;
&lt;Field name=&quot;projectName&quot; literal=&quot;MYAWESOMEPROJECT&quot; /&gt;
&lt;/Gelf&gt;
&lt;RollingFile name=&quot;RollingFile&quot;
fileName=&quot;${log-path}/server/${app-name}-server&quot;
filePattern=&quot;${log-path}/server/${app-name}-server-%d{yyyy-MM-dd}-%i.log.gz&quot;
append=&quot;false&quot;&gt;
&lt;PatternLayout&gt;
&lt;pattern&gt;${log-pattern}&lt;/pattern&gt;
&lt;/PatternLayout&gt;
&lt;Policies&gt;
&lt;SizeBasedTriggeringPolicy size=&quot;100 MB&quot; /&gt;
&lt;/Policies&gt;
&lt;/RollingFile&gt;
&lt;/Appenders&gt;
&lt;Loggers&gt;
&lt;Root level=&quot;INFO&quot; additivity=&quot;false&quot; includeLocation=&quot;true&quot;&gt;
&lt;AppenderRef ref=&quot;RollingFile&quot; /&gt;
&lt;AppenderRef ref=&quot;gelf&quot; /&gt;
&lt;/Root&gt;
&lt;/Loggers&gt;
&lt;/Configuration&gt;

*SOLUTION

up my docker this way to open a port 5555:

$ docker run --link mongo --link elasticsearch \
-p 9000:9000 -p 12201:12201 -p 1514:1514 -p 5555:5555 \
-e GRAYLOG_HTTP_EXTERNAL_URI=&quot;http://127.0.0.1:9000/&quot; \
-d graylog/graylog:3.3

in my log4j2.xml i change the line 7:

&lt;Gelf name=&quot;Gelf&quot; host=&quot;tcp:127.0.0.1&quot; port=&quot;5555&quot; version=&quot;1.1&quot;

and my Graylog Input (Gelf:TCP):
Graylog Input Gelf TCP

答案1

得分: 2

Graylog的员工在这里。根据您的说法,看起来您还没有在您的Graylog实例上启用GELF输入。我建议您阅读我们针对Graylog版本3.3的Docker安装文档,可以在这里找到。更新的文档可以在这里找到。

我要注意的是,虽然示例中的输入是用于原始/纯文本,但如果您按照文档中该部分的说明创建一个GELF TCP输入,那么您应该开始在UI中看到您的日志显示。

英文:

Graylog employee here. From what you're saying, it sounds like you've not enabled a GELF input on your Graylog instance. I'd recommend reading through our Docker installation docs for graylog version 3.3 here. Newer documentation can be found here

I'll note that while the input in the example is for Raw/Plaintext, if you create a GELF TCP input using the instructions in that portion of the docs, then you should start seeing your logs show in the UI.

huangapple
  • 本文由 发表于 2020年10月28日 01:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64560041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定