将Spring Boot日志直接发送到Logstash,不带文件 [英] Send spring boot logs directly to logstash with no file
问题描述
因此,我正在使用kubernetes和spring boot构建一个完整的云解决方案.
So, I'm building a full cloud solution using kubernetes and spring boot.
我的spring boot应用程序已部署到容器中,并直接在控制台上登录.由于容器是临时的,因此我也希望将日志也发送到远程logstash服务器,以便可以对其进行处理并将其发送到Elastic.
My spring boot application is deployed to a container and logs directly on the console. As containers are ephemerals I'd like to send logs also to a remote logstash server, so that they can be processed and sent to elastic.
通常我会在托管我的应用程序的服务器上安装文件拍子,虽然可以,但是没有任何内置方法可以让我避免在发送文件之前在文件上写日志吗?
Normally I would install a filebeat on the server hosting my application, and I could, but isn't there any builtin method allowing me to avoid writing my log on a file before sending it?
当前,我正在使用log4j,但只要它具有"logbackappender",我便认为切换到另一个记录器没有问题.
Currently I'm using log4j but I see no problem in switching to another logger as long it has a "logbackappender".
推荐答案
您可以尝试在 resources
文件夹中添加 logback.xml
:
You can try to add logback.xml
in resources
folder :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration scan="true">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<param name="Encoding" value="UTF-8"/>
<remoteHost>localhost</remoteHost>
<port>5000</port>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"app_name":"YourApp", "app_port": "YourPort"}</customFields>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="logstash"/>
</root>
</configuration>
然后添加 logstash编码器
依赖项:
pom.xml
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.11</version>
</dependency>
logstash.conf
input {
udp {
port => "5000"
type => syslog
codec => json
}
tcp {
port => "5000"
type => syslog
codec => json_lines
}
http {
port => "5001"
codec => "json"
}
}
filter {
if [type] == "syslog" {
mutate {
add_field => { "instance_name" => "%{app_name}-%{host}:%{app_port}" }
}
}
}
output {
elasticsearch {
hosts => ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
index => "logs-%{+YYYY.MM.dd}"
}
}
这篇关于将Spring Boot日志直接发送到Logstash,不带文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!