启动好ES
配置Kibana
配置LogStash
配置Logback(logback-spring.xml)
Logstash独立部署,微服务节点通过网络向Logstash发送日志信息。
一:配置Kibana
1、Kibana配置文件
2、/config/kibana.yml
server.port: 5601 ##服务端口
server.host: "0.0.0.0" ##服务器ip 本机
elasticsearch.url: "http://localhost:9200" ##elasticsearch服务地址 与elasticsearch对应
3、启动kibana
二:配置LogStash
- 自定义配置文件 logstash.conf
input {
tcp {
mode => "server"
#这个需要配置成本机IP,不然logstash无法启动
host => "127.0.0.1"
#端口号 这里其实把logstash作为服务,开启4567端口接收logback发出的消息
port => 4567
codec => json_lines
}
}
filter {
json {
source => "message"
remove_field => ["message"]
}
}
output {
stdout {
codec => rubydebug
}
# Sending properly parsed log events to elasticsearch
elasticsearch {
hosts => ["localhost:9200"]
}
}
- 如下的命令来测试 logstash.conf 是否正确:
- bin/logstash --config.test_and_exit -f logstash.conf
- 启动 bin/logstash -f logstash.conf
二:配置Logback(logback-spring.xml)
- maven项目加入
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.7</version>
</dependency>
- logback-spring.xml增加 (文件路径为 :/resource/logback-spring.xml)
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>169.254.247.253:9250</destination>
<!-- encoder必须配置,有多种可选 -->
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
<root level="${log.level}">
<appender-ref ref="LOGSTASH" />
</root>
my Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<contextName>MovieBooking</contextName>
<timestamp key="TIMESTAMP" datePattern="yyyy-MM-dd" />
<!-- 文件保存路径 -->
<property name="LOGPATH" value="log" />
<springProperty scope="context" name="appName"
source="server.name" defaultValue="localhost.log" />
<!-- 输出到控制台 -->
<appender name="stdout"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n
</pattern>
</layout>
</appender>
<!-- 输出到文件 -->
<appender name="fileLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGPATH}${file.separator}microservername.logs</file>
<append>true</append>
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n
</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGPATH}${file.separator}all${file.separator}%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!--开启tcp格式的logstash传输,通过TCP协议连接Logstash -->
<appender name="stash"
class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!-- 这是是logstash服务器地址 端口 -->
<destination>127.0.0.1:4567</destination>
<!--输出的格式,推荐使用这个 -->
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<providers>
<timestamp />
<version />
<message />
<loggerName />
<threadName />
<logLevel />
<callerData />
</providers>
</encoder>
<keepAliveDuration>5 minutes</keepAliveDuration>
</appender>
<root level="INFO">
<appender-ref ref="fileLog" />
<appender-ref ref="stdout" />
<appender-ref ref="stash" />
</root>
</configuration>