ELK环境配置+log4j日志记录
elk环境配置+log4j日志记录
1. 背景介绍
在大数据时代,日志记录和管理变得尤为重要。
以往的文件记录日志的形式,既查询起来又不方便,又造成日志在服务器上分散存储,管理起来相当麻烦,
想根据一个关键字查询日志中某个关键信息相当困难。
这个时候,elk诞生了。
什么是elk?
简单来说:它是一套完整的日志记录和分析的解决方案平台。
2. 技术栈介绍
elk = elasticsearch + logstash + kibana
2-1) elasticsearch:
(
elasticsearch is a distributed open source search engine based on apache lucene,
and released under an apache 2.0 license (which means that it can be downloaded, used, and modi ed free of charge).
it provides horizontal scalability, reliability, and multitenant capability for real-time search.
elasticsearch features are available through json over a restful api.
the searching capabilities are backed by a schema-less apache lucene engine,
which allows it to dynamically index data without knowing the structure beforehand.
elasticsearch is able to achieve fast search responses because it uses indexing to search over the texts.
)
elasticsearch是一个分布式的开源的基于apache lucene项目的搜索引擎,它发布在apache 2.0协议下
(这也就意味着它可以被免费地下载,使用并且修改)。
elasticsearch提供了水平的,可扩展的,可靠的,多用户形式的实时搜索。
elasticsearch的功能可以通过json格式的restful api形式访问。
elasticsearch的搜索能力是得到apache lucene引擎的支持,允许给文本数据增加加动态索引。
2-2) logstash
(
logstash is a data pipeline that helps collect, parse, and analyze a large variety of structured and unstructured data and events generated across various systems.
it provides plugins to connect to various types of input sources and platforms, and is designed to ef ciently process logs, events,
and unstructured data sources for distribution into a variety of outputs with the use of its output plugins,
namely le, stdout (as output on console running logstash), or elasticsearch.
)
logstash是一个数据管道,它被用来收集,解析和分析各种结构化的和非结构化的由各种系统产生的数据以及事件。
它提供了插件用来连接到各种输入数据源,可以高效地处理日志,事件以及非结构化的数据,而且可以通过输出插件的形式
把结果输出到各种输出源,比如:标准输出,控制台或者elasticsearch。
2-3) kibana
(kibana is an open source apache 2.0 licensed data visualization platform that helps in
visualizing any kind of structured and unstructured data stored in elasticsearch indexes.
kibana is entirely written in html and javascript. )
kibana是一个基于apache 2.0协议的开源可视化平台,它用来可视化任何结构化的和非结构化的存储在elasticsearch索引
中的数据。kibana完全用html和javascript编写。
3. 下载/安装/配置/启动
前面说了很多废话,接下来是每个程序员感兴趣的动手环节了,let's start!
3-1) 安装elasticsearch
下载地址:
a) 解压安装包elasticsearch-5.2.2.tar.gz
(由于本人是mac系统)我把它解压到了/usr/local目录下,完整路径如下:
/usr/local/elasticsearch-5.2.2
b) 编辑配置文件
cd config
vi elasticsearch.yml
内容如下:
# ---------------------------------- cluster ----------------------------------- # # use a descriptive name for your cluster: # cluster.name: my-application # # ------------------------------------ node ------------------------------------ # # use a descriptive name for the node: # node.name: node-1 # # add custom attributes to the node: # #node.attr.rack: r1 # # ----------------------------------- paths ------------------------------------ # # path to directory where to store the data (separate multiple locations by comma): # path.data: /tmp/elasticsearch/data # # path to log files: # path.logs: /tmp/elasticsearch/logs # ---------------------------------- network ----------------------------------- # # set the bind address to a specific ip (ipv4 or ipv6): # network.host: localhost # # set a custom port for http: # http.port: 9200
c) 启动elasticsearch
./bin/elasticsearch
如果没有错误发生,可以进入到下一步的验证环节
d) 验证
可以使用curl命令:
curl 'http://localhost:9200/?pretty'
或者
直接用浏览器打开
http://localhost:9200
3-2) 安装logstash
a) 解压安装包logstash-5.2.2.tar.gz
完整路径如下:
/usr/local/logstash-5.2.2
b) 编辑配置文件
cd config
新建配置文件log4j_es.conf
vi log4j_es.conf
内容如下:
input { log4j { host => "127.0.0.1" port => 4560 } } output { stdout { codec => rubydebug } elasticsearch{ hosts => ["localhost:9200"] index => "log4j-%{+yyyy.mm.dd}" document_type => "log4j_type" } }
c) 启动logstash
./bin/logstash -f config/log4j-es.conf
3-3) 安装kibana
a) 解压安装包kibana-5.2.2.tar.gz
完整路径如下:
/usr/local/ kibana-5.2.2
b) 编辑配置文件
cd config
vi kibana.yml
内容如下:
# kibana is served by a back end server. this setting specifies the port to use. server.port: 5601 server.host: "localhost" # the url of the elasticsearch instance to use for all your queries. elasticsearch.url: "http://localhost:9200" # kibana uses an index in elasticsearch to store saved searches, visualizations and # dashboards. kibana creates a new index if the index doesn't already exist. kibana.index: ".kibana"
c) 启动kibana
./bin/kibana
d) 验证
打开网址:
http://localhost:5601/
见到如下logo
4. log4j记录日志到logstash
4-1) 新建maven项目
pom中的关键dependency配置如下:
<dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency
4-2) log4j.properties(放在resources文件夹下)
### 设置### log4j.rootlogger = debug,stdout,d,e,logstash ### 输出信息到控制抬 ### log4j.appender.stdout = org.apache.log4j.consoleappender log4j.appender.stdout.target = system.out log4j.appender.stdout.layout = org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern = [%-5p] %d{yyyy-mm-dd hh:mm:ss,sss} method:%l%n%m%n ### 输出debug 级别以上的日志到=/users/bee/documents/elk/log4j/debug.log### log4j.appender.d = org.apache.log4j.dailyrollingfileappender log4j.appender.d.file = /users/kg/documents/logs/elk/debug.log log4j.appender.d.append = true log4j.appender.d.threshold = debug log4j.appender.d.layout = org.apache.log4j.patternlayout log4j.appender.d.layout.conversionpattern = %-d{yyyy-mm-dd hh:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 输出error 级别以上的日志到=/users/bee/documents/elk/log4j/error.log ### log4j.appender.e = org.apache.log4j.dailyrollingfileappender log4j.appender.e.file =/users/kg/documents/logs/elk/error.log log4j.appender.e.append = true log4j.appender.e.threshold = error log4j.appender.e.layout = org.apache.log4j.patternlayout log4j.appender.e.layout.conversionpattern = %-d{yyyy-mm-dd hh:mm:ss} [ %t:%r ] - [ %p ] %m%n #输出日志到logstash log4j.appender.logstash=org.apache.log4j.net.socketappender log4j.appender.logstash.remotehost=127.0.0.1 log4j.appender.logstash.port=4560 log4j.appender.logstash.reconnectiondelay=60000 log4j.appender.logstash.locationinfo=true
4-3) java代码(elklog4jtest.java):
package org.genesis.arena.elk; import org.apache.log4j.logger; /** * created by kg on 17/3/27. */ public class elklog4jtest { private static final logger logger = logger.getlogger(elklog4jtest.class); public static void main(string[] args) throws exception { logger.debug("this is a debug message!"); logger.info("this is info message!"); logger.warn("this is a warn message!"); logger.error("this is error message!"); try{ system.out.println(5/0); }catch(exception e){ logger.error(e); } } }
4-4) 运行结果
[debug] 2017-03-29 12:56:00,454 method:org.genesis.arena.elk.elklog4jtest.main(elklog4jtest.java:11)
this is a debug message!
[info ] 2017-03-29 12:56:00,529 method:org.genesis.arena.elk.elklog4jtest.main(elklog4jtest.java:12)
this is info message!
[warn ] 2017-03-29 12:56:00,531 method:org.genesis.arena.elk.elklog4jtest.main(elklog4jtest.java:13)
this is a warn message!
[error] 2017-03-29 12:56:00,533 method:org.genesis.arena.elk.elklog4jtest.main(elklog4jtest.java:14)
this is error message!
[error] 2017-03-29 12:56:00,538 method:org.genesis.arena.elk.elklog4jtest.main(elklog4jtest.java:19)
java.lang.arithmeticexception: / by zero
然后会在logstash控制台看到输出如下图:
5. 连通kibana
5-1) 打开 http://localhost:5601/
5-2) 创建索引
还记得我们之前在logstash配置文件中配置的索引吗?
log4j-%{+yyyy.mm.dd}
因此,我们应该创建索引为:log4j-*
5-3)验证
从这里可以看到自己刚才在java代码中记录的日志。
推荐阅读
-
详解Spring Boot配置使用Logback进行日志记录的实战
-
nginx关闭favicon.ico、robots.txt日志记录配置
-
nginx访问日志并删除指定天数前的日志记录配置方法
-
实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
-
nginx中用JSON格式记录日志的配置示例
-
ELK日志分析系统(1)-基本环境搭建
-
详解Spring Boot配置使用Logback进行日志记录的实战
-
php开发环境配置记录
-
springboot如何使用logback-spring配置日志格式,并分环境配置
-
SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务