配置索引
- 打开kibana,选择Stack Management
- 查看索引管理, 可以看到索引已经建立了
- 创建索引模式
- 创建索引模式
点击下一步,选择时间字段 - 在discover界面查看效果
Java项目实践
配置日志模版
- application.properties
logging.pattern.console=[%X{logId}] %clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%c){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx} logging.pattern.file=[%X{logId}] %d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %c : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
logId为自定义日志id
编写日志拦截器
LogInterceptor
@Component public class LogInterceptor extends HandlerInterceptorAdapter { private static final String LOG_ID = "logId"; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String logId = UUID.randomUUID().toString(); MDC.put(LOG_ID, logId); return super.preHandle(request, response, handler); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { MDC.remove(LOG_ID); super.postHandle(request, response, handler, modelAndView); } }
将拦截器加入到Web配置中
@Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Autowired private LogInterceptor logInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(logInterceptor).addPathPatterns("/**"); } }
这样就能通个日志ID找到一个请求的所有日志了
配置filebeat
参照以上配置中的ih-front-center-ljey-staging
配置
查看kibana
使用日志id查询日志
日志定时清理
随时时间的流逝,ES中的日志数据将不可避免的往上增长,但我们一般所需要的日志不过是一个月之内或者更少,那我们应当如何解决这个问题呢?
elatis官方推荐工具curator, 使用该工具可以方便简单的对es中的索引进行管理。
安装
官方提供了多种安装方式,但推荐使用pip方式安装
pip install elasticsearch-curator
配置
curator一共需要两个配置文件,一个是config_file
,一个是action_file
- config_file: 描述了基本配置信息,如何与es连接,curator的日志文件存储路径
- action_file: 描述了应当如何操作索引
由于我们使用pip方式安装,没有样板文件,需要自己去官方拷贝一份
config_file
--- client: #es地址,默认端口9200,其他端口可使用 192.168.1.13:9201 格式 hosts: - 192.168.1.13 - 192.168.1.14 - 192.168.1.15 port: 9200 url_prefix: # 如果es开启了ssl访问,这里要改成True,但由于Python版本问题,官方也没给出明确的使用方式, 各种兼容问题... use_ssl: False # 证书 certificate: # 客户端签名 client_cert: client_key: ssl_no_validate: False # es账号 username: elastic # es密码 password: 123456 timeout: 30 master_only: False logging: loglevel: INFO # curator的日志地址 logfile: '/opt/server/curator/config/curator.log' # 日志格式 logformat: default # 不输出elasticsearch和urllib3依赖的日志,这里的elasticsearch不是指es服务端,仅仅是个依赖 blacklist: ['elasticsearch', 'urllib3']
action_file
--- actions: # 1表示第一个action,可以配很多个,我这里只配了一个 1: # action delete_indices 表示删除索引,官方提供了许多的action action: delete_indices # 描述, 自己随便写 description: "delete 3 days ago index" # 操作,每个action都有自己对应的操作,可见官方文档 options: # 出现异常是否继续 continue_if_exception: False # 覆盖默认的超时时间180, 单位秒 timeout_override: 300 # 是否禁止该action, 可以不配 disable_action: False # 忽略空的索引列表,如果以下filters配置过滤出来的索引列表是空的 ignore_empty_list: True # 过滤配置,每一个过滤类型都有自己的配置 filters: # 过滤类型,这里是 模板方式 - filtertype: pattern # 前缀匹配,还有后缀匹配以及正则,时间方式 kind: prefix # 值, 该过滤器表示过滤以ih为前缀的索引 value: ih # 排除,默认False,可不配,改为True则表示匹配除了以ih开头的索引 exclude: False # 过滤类型 年龄方式 - filtertype: age # 过滤方式, 以索引的creation_date(创建时间)进行过滤 source: creation_date # 方向,older表示以前的,也有以后的 direction: older # 单位 天 unit: days # 3天 该过滤器表示过滤3天之前创建的索引 unit_count: 3
启动
curator --config /opt/server/curator/config/config.yml /opt/server/curator/config/action.yml
注意config.yml 和 action.yml一定要写绝对路径
查看日志
可去kibana中查看是否删除成功
配置定时执行
由于curator并不像后台程序一样会一直在后台运行,它只执行一次,所以我们需要借助linux的定时功能实现定时执行curator
#编辑定时任务 crontab -e #编写配置 每天0点0分执行 0 0 * * * /usr/local/bin/curator --config /opt/server/curator/config/config.yml /opt/server/curator/config/action.yml #重启 service cron restart
第一次会让你选择编译器,如果选错编译器了要更改编译器 输入 select-editor 重新选择