因為公司開發環境沒有裝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並引用自定義配置文件
.\bin\logstash -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'