今天得闲就自己搭了个ELK示例,过程挺简单的。
Elastic Search,
logstash, kibana再这个地址https://www.elastic.co/cn/products都可以下载。话不多说。
首先安装ES。解压下载包到置顶目录启动
脚本再bin目录下的elasticsearch,参数配置在config目录下。主要配置文件为elasticsearch.yml。主要可配置端口和discovery.zen.minimum_master_nodes这个选举的最少节点(防止脑裂问题"split brain")。
之后安装logstash。解压压缩包后再解压目录的同级下创建一个logstash-simple.conf
下面是我的配置
class="javascript" name="code">
input {
tcp {
port => 8333
}
}
filter {
}
output {
elasticsearch { hosts => "127.0.0.1" }
stdout {
codec => rubydebug
}
}
如何配置这个地址可以参考.https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
logstash流程管道是按照input,filter,output执行的,可用的插件可参考一下的地址。
https://www.elastic.co/guide/en/logstash/current/input-plugins.html
https://www.elastic.co/guide/en/logstash/current/output-plugins.html
之后是kibana安装。解压完成后config目录下有kibana.yml。主要配置ES的http端口地址elasticsearch.url: "http://localhost:9200/",这样才能获得通过Logstash导入ES的日志数据.
下面是我的logback的配置文件,通过logback的appender直接导入logstash
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<appender name="logstash-out" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!-- <destination>localhost:8333</destination> -->
<param name="Encoding" value="UTF-8"/>
<remoteHost>localhost</remoteHost>
<port>8333</port>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="logstash-out" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
配置完成后,随便写个日志输出代码就可验证了。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogPrint {
private static final Logger LOGGER = LoggerFactory.getLogger(LogPrint.class);
public static void main(String[] args) {
LOGGER.info("test log info begin");
for (int i = 0; i < 100; i++) {
LOGGER.debug("[ResponseEntity]:{}", "i am debug" + i);
LOGGER.info("[ResponseEntity] i am info" + i);
LOGGER.error("[ResponseEntity]i am error" + i);
LOGGER.warn("[ResponseEntity]i am warn" + i);
LOGGER.debug("[ResponseEntity]:{}", "i am debug" + i);
LOGGER.info("[ResponseEntity] i am info" + i);
LOGGER.error("[ResponseEntity]i am error" + i);
LOGGER.warn("[ResponseEntity]i am warn" + i);
}
}
}