使用logstash结合logback收集微服务日志
Posted peterpoker
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用logstash结合logback收集微服务日志相关的知识,希望对你有一定的参考价值。
因为公司开发环境没有装elk,所以每次查看各个微服务的日志只能使用如下命令
这样子访问日志是并不方便,于是想为每个微服务的日志都用logstash收集到一个文件out中,那以后只要输出这个文件则可查看所有的日志
结合logback和logstash收集日志
1.为需要收集日志的微服务引入logstash-logback-encode依赖
//用logstash收集logback
compile ‘net.logstash.logback:logstash-logback-encoder:5.2‘
2.编写logback.xml并设置appender指定logstash的ip端口
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>127.0.0.1:9100</destination>
<includeCallerData>true</includeCallerData>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>log/weather.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.wugui" level="info" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="stash" />
<appender-ref ref="FILE"/>
</logger>
<logger name="com.wugui.spring.cloud.weather.Service" level="debug" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="stash" />
</logger>
</configuration>
3.编写logstash启动引用的配置文件test.conf
input {
tcp {
##host:port就是上面appender中的 destination,这里其实把logstash作为服务,开启9250端口接收logback发出的消息
host => "127.0.0.1"
port => 9100
mode => "server"
tags => ["tags"]
codec => json_lines
}
}
output {
stdout { codec => rubydebug }
#输出到es
elasticsearch { hosts => "127.0.0.1:9200" }
#输出到一个文件中
file {
path => "D:/study/springcloud_house/ELK/logstash-6.1.1/out"
codec => line
}
}
4.用powershell启动Logstash并引用自定义配置文件
.inlogstash -f test.conf
5.运行微服务,可观察到logstash控制台在输出收集到的日志
注意:
当使用springBootVersion =2.0.1.RELEASE时,引用依赖compile ‘net.logstash.logback:logstash-logback-encoder:4.7‘会报错,报错信息如下:
ERROR in net.logstash.logback.appender.LogstashTcpSocketAppender[stash] - Unable start disruptor java.lang.NoSuchMethodError: ch.qos.logback.core.encoder.Encoder.init(Ljava/io/OutputStream;)V
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:166)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.reinitialize(LogbackLoggingSystem.java:212)
at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:75)
at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:114)
at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:264)
at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:237)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:200)
at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:173)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:137)
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:197)
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:104)
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:70)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:358)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
at com.wugui.spring.cloud.weather.Application.main(Application.java:14)
解决办法:引入最新的logstash-logback-encoder依赖即可
compile ‘net.logstash.logback:logstash-logback-encoder:5.2‘
以上是关于使用logstash结合logback收集微服务日志的主要内容,如果未能解决你的问题,请参考以下文章
配置Spring项目输出JSON到LogStash或者使用FileBeat收集上传到ELK
ELK之八----Logstash结合kafka收集系统日志和nginx日志
docker安装ELK 收集springboot日志(方法1)