- A+
所属分类:未分类
ELK安装我就不多说了,之前说过,elk安装,下来直接说怎么弄吧
这里以logback日志为例,在项目中加入logstash-logback-encoder依赖,完整pom如代码清单所示。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.baiked</groupId> <artifactId>springboot_logstash</artifactId> <version>1.0.0-SNAPSHOT</version> <name>springboot-logstash</name> <description>springboot-logstash</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
接下来新建一个logback-spring.xml文件,配置logback日志信息,注意这里配置的destination属性,输出的要和logstash配置的对应上,不然收集不上,内容如下:
<?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"> <destination>127.0.0.1:8888</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>
修改启动类,加入一个mvc方法,主要用于输出日志,如下所示。
package com.baiked; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class SpringbootLogstashApplication { Logger logger = LoggerFactory.getLogger(SpringbootLogstashApplication.class); @GetMapping("test") public void test(){ logger.info("测试初始一些日志吧!"); } public static void main(String[] args) { SpringApplication.run(SpringbootLogstashApplication.class, args); } }
logstash配置如下,再次提醒一下,输入要与刚刚配置的对应上,输出为本地es:
input { tcp { mode => "server" host => "0.0.0.0" port => 8888 codec => json_lines } } output { elasticsearch { hosts => "localhost:9300" index => "springboot-logstash-%{+YYYY.MM.dd}" } }
- 安卓客户端下载
- 微信扫一扫
- 微信公众号
- 微信公众号扫一扫