转载

SpringBoot继承LogStash实现日志收集

在springBoot 项目下pom文件增加logStash 依赖包

<!--logStash-->
    <dependency>
        <groupId>net.logstash.logback</groupId>
        <artifactId>logstash-logback-encoder</artifactId>
        <version>5.3</version>
    </dependency>
复制代码

在springboot资源文件夹下增加log日志数据配置文件 logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <!--配置logStash 服务地址-->
        <destination>192.168.92.50:4560</destination>
        <!-- 日志输出编码 -->
        <encoder charset="UTF-8"
                 class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "logLevel": "%level",
                        "serviceName": "${springAppName:-}",
                        "pid": "${PID:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "rest": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="LOGSTASH" />
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

复制代码

三、配置logstash 日志收集配置

在logstash 配置文件logstash.conf 增加日志收集配置信息

# Sample Logstash configuration for creating a simple
# Beats -> Logstash -> Elasticsearch pipeline.

input {
  tcp {
    mode => "server"
    host => "192.168.92.50"
    port => 4560
    codec => json_lines
  }
}
output {
  elasticsearch {
    hosts => "192.168.92.50:9200"
    index => "springboot-logstash-%{+YYYY.MM.dd}"
  }
}

复制代码

注意上述配置的host地址和es地址信息,利用此次配置文件启动logstash服务。

输入我们logstash日志配置的名称继续下一步,选择带时间戳过滤器

点击左侧Discover菜单,已经发现数据已经展现

原文  https://juejin.im/post/5cdbf2a7e51d453af905216b
正文到此结束
Loading...