Original / Zhuji Qian
logstash is an open source data collection engine, supports a variety of input selection, it is possible to collect data from multiple sources simultaneously, want to forward the data storage "library", for example, it can be forwarded to store elasticsearch, and the like can also be forwarded to kafka in messaging middleware. logstash has a powerful plug-in ecology, including basic http, tcp, udp, file and kafa, redis and so on.
Spring background running project, you can see the log log print messages on the console, we can ERROR log log information will be forwarded to listen and storage.
How to achieve logstash to monitor and log data ERROR Spring project forwarding store it?
Deployment architecture diagram:
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173004355-1825333039.png)
You can be implemented in accordance with the following process:
1. Download logstash.
Network disk to get logstash-5.5.2 versions according to the following address:
Link: https: //pan.baidu.com/s/1h7xo65P7_O76Azt0-I-2-A
Extraction code: 95vg
You can also download the official website: https://www.elastic.co/cn/downloads/logstash
2. Install logstash
Directly to the compressed archive to a local disk in it, do not require additional operations, 5.x or later logstash is no need to install additional plug-ins, already comes with most plug-ins.
3. Verify that the installation was successful
Run cmd, logstash entering the bin directory, run the command: logstash -e 'stdin INPUT {{Output} {} {}} stdout'
Successful run shot below, that is successfully installed and started:
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173200192-1112583945.png)
After startup, the input cursor: hello wrold
Under normal circumstances, the following information is displayed to prove logstash can be properly used.
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173229804-1346673996.png)
在这个过程里,涉及到几个概念,logstash是一个管道,里面有两个input和output的必选元素,即输入与输出,之间还可以有一个可选的过滤器filter过滤器。input插件从源头获取到数据,过滤器会根据条件来进行修改,最后通过ouput插件将数据传输,可输出给Elasticsearch、kafka、file等。这几个东西都是插件,在logstash5.x版本以上的,是已经自带了,不需要额外安装。
处理过程模型图如下:
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173405377-828575209.png)
Logstash 提供了一个 shell 脚本叫 logstash,
支持以下运行参数:
执行命令: -e 执行-e后面的参数
:logstash -e 'input{stdin{}} output{stdout{}}'
执行文件: --config 或 -f 执行-f后面的conf文件:
logstash -f logstash.conf
输入插件:input{ … }
过滤插件:filter{ … }
输出插件:output{ … }
测试配置文件是否正确,然后退出:-t
在这篇文章里,主要用到以上这些命令,其余读者若感兴趣可以自行去研究探索。
4.配置一个文件**.conf
可以在bin目录或者config目录等地方,我选择的是bin目录下,新建一个logstash.conf文件,如下:
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173445867-1603535622.png)
logstash.conf文件夹里进行信息的配置信息:
1 input { stdin { } }#该行可有可无 2 input { 3 #开启tcp插件的监听 4 tcp { 5 #这个需要配置成本机IP,不然logstash无法启动 6 host => "127.0.0.1" 7 #端口号 8 port => 9600 9 #将日志以json格式输入 10 codec => json_lines 11 } 12 } 13 14 output { 15 #输出打印 16 stdout { codec => rubydebug } 17 }
配置好,就可以先启动进行监听了,启动命令:先进到存放logstash.conf的目录下,我的目录在bin里,所以进入到了的是bin目录,执行:logstash -f logstash.conf。启动成功的截图如下:
5.在spring进行logstash配置的maven依赖引入
我在项目里用到的开源日志组件是logback,它是log4j的改良,主要分为以下三个模块:
logback-classic:log4j的一个改良版本,完整实现了slf4j API,可以方便更换成其它日志系统如log4j或JDK14 Logging。
logback-access:访问模块与Servlet容器集成提供通过Http来访问日志的功能。
logback-core:是其它两个模块的基础模块。
logback需要在maven里引用到的依赖:
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>net.logstash.log4j</groupId> <artifactId>jsonevent-layout</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.0</version> </dependency>
如果引用到的ch.qos.logback依赖版本太低的话,可能会出现以下错误:java.lang.NoSuchMethodError: ch.qos.logback.core.util.Loader.getResourceOccurrenceCount(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/util/Set;
可以根据项目需求来选择合适的版本,经过测试,以上的1.2.3版本是可以符合要求的。
配置完成依赖后,就可以开始进行下一步配置。
6.在spring的logback.xml进行logstash配置
<!--开启tcp格式的logstash传输,通过TCP协议连接Logstash--> <appender name="STASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>127.0.0.1:9600</destination> <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"> <jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator"> <escape> <targetCharacterCode>10</targetCharacterCode> <escapeSequence>\u2028</escapeSequence> </escape> </jsonFactoryDecorator> <providers> <pattern> <pattern> { "timestamp":"%date{ISO8601}", "user":"test", "message":"[%d{yyyy-MM-dd HH:mm:ss.SSS}][%p][%t][%l{80}|%L]%m"}%n } </pattern> </pattern> </providers> </encoder> <keepAliveDuration>5 minutes</keepAliveDuration> </appender> <root level="INFO"> <appender-ref ref="STASH"/> </root>
配置说明:
encoder:配置的规范;
LoggingEventCompositeJsonEncoder:json格式的编码器,即将日志数据转换成json格式;
jsonFactoryDecorator:解决中文转码的问题;
providers:json格式提供者,对json进行一个定制化设置,比如,timestamp,message,thread_name等,其他的自定义的字段的值可以通过MDC设置进来,格式就是%date{xx},
注意:按照上面的设置,logstash才可以正常接收到日志数据,否则是无法接收到的。
destination定义的ip与端口与logstash里的logstash.conf需一直,logstash.conf里的tcp会一直监听这个ip的端口:
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173819919-1004166943.png)
配置完成后,启动spring项目,这时原来监听tcp的logstash就可以实时监听接收到了数据,logstash的控制台显示打印如下:
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173838955-1563430117.png)
若要监听到的是ERROR级别的日志,在logback.xml里的logstash配置里的appender里添加一行以下代码即可:
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter>
在日志级别修改为以下即可:
<root level="INFO"> <appender-ref ref="STASH"/> <appender-ref ref="ERROR"/> </root>
到这一步,就完成了通过logstash收集spring的logback日志的功能,在这个基础上,可以再进一步扩展,扩展将logstash采集到的数据输出到
Elasticsearch。
input { stdin { } }#该行可有可无
input {
#开启tcp模式的监听
tcp {
#这个需要配置成本机IP,不然logstash无法启动
host => "127.0.0.1"
#端口号
port => 9600
#将日志以json格式输入
codec => json_lines
}
}
output {
#输出打印
stdout { codec => rubydebug }
elasticsearch { hosts => ["127.0.0.1:9200"] }
}
打开已经本地安装的
Elasticsearch:http://127.0.0.1:9100/
可以看到,ES可以接受到logstash接收到的数据了:
![](https://img2018.cnblogs.com/blog/1545382/201909/1545382-20190926173957392-247906967.png)
作者:朱季谦
出处:https://www.cnblogs.com/zhujiqian/
本文版权归作者和博客园共有。欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接!
出处:https://www.cnblogs.com/zhujiqian/
本文版权归作者和博客园共有。欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接!