由於公司開發環境沒有裝elk,因此每次查看各個微服務的日誌只能使用以下命令
java
這樣子訪問日誌是並不方便,因而想爲每一個微服務的日誌都用logstash收集到一個文件out中,那之後只要輸出這個文件則可查看全部的日誌
spring
1.爲須要收集日誌的微服務引入logstash-logback-encode依賴shell
//用logstash收集logback compile 'net.logstash.logback:logstash-logback-encoder:5.2'
2.編寫logback.xml並設置appender指定logstash的ip端口json
<?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.confbootstrap
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並引用自定義配置文件ruby
.\bin\logstash -f test.conf
5.運行微服務,可觀察到logstash控制檯在輸出收集到的日誌
app
當使用springBootVersion =2.0.1.RELEASE時,引用依賴compile 'net.logstash.logback:logstash-logback-encoder:4.7'會報錯,報錯信息以下:elasticsearch
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依賴便可tcp
compile 'net.logstash.logback:logstash-logback-encoder:5.2'