Rootop 服务器运维与web架构

logstash中多个日志监控及日志多行匹配 multiline 插件

问题1:一个服务中可能会有2个以上的日志
问题2:一个日志中,一条完整的日志会包含多行,默认logstash匹配是一行一条,这就需要把多行合并为一条,这样才是一条完整的日志。

日志格式:

日志开头都是以 [日志级别] 开始,如果不是,可以让开发改一下,否则就自己修改正则。

例:

input {

	## 锦衣卫接口日志
	file {
		type => "jinyiweiapi" # 稍后说作用
		path => "/home/dockermount/jinyiwei/logs/catalina.out" # 日志路径
		codec => multiline {
			pattern => "^\["  # 正则表达式,匹配开头为 "[" 的为一条日志的开始
			negate => true	  # true向前匹配,false向后匹配,默认false
			what => previous  # 未匹配的内容是向前合并还是向后合并, previous/next
		}
	}

	# 锦衣卫后台日志
	file {
		type => "jinyiweimgmt"
		path => "/home/dockermount/jinyiwei/logs_manager/catalina.out"
		codec => multiline {
			pattern => "^\["
			negate => true
			what => previous
		}
	}

}

output {

	# 根据上面的type不同,建立不同索引
	if [type] == "jinyiweiapi" {
		elasticsearch {
			action => "index"
			hosts  => "119.x.x.x:9200"
			index => "jinyiwei-tomcat-api-%{+YYYY.MM.dd}" # 为 type 为 jinyiweiapi 的创建索引
			codec => "json"
		}
	}

	# 根据上面的type不同,建立不同索引
	if [type] == "jinyiweimgmt" {
		elasticsearch {
			action => "index"
			hosts  => "119.x.x.x:9200"
			index => "jinyiwei-tomcat-manage-%{+YYYY.MM.dd}" # 为 type 为 jinyiweimgmt 的创建索引
			codec => "json"
		}
	}

	# 调试模式
	stdout {
		codec => rubydebug
	}

}

这样,在elasticsearch中就会出现2个索引,即两个不同的日志,创建独立的索引。便于kibana中查看。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3963.html

作者:Venus

服务器运维与性能优化

评论已关闭。