logstash采集日志

logstash主要用来采集分布式及微服务系统日志,从而对日志进行统一管理分析检索。
下载https://www.elastic.co/cn/downloads/logstash,根据自己系统对应下载(linux建议使用TAR.GZ,win系使用zip)笔者使用windows
在这里插入图片描述
下载后,解压。
1.采集控制台接收输入,并输出控制台。
进入config目录新建console2console.conf 文件

input {
  stdin{
  }
}

output{
  stdout{
  }
}

input 是输入,stdin是标准输入(即控制台),output是输出,stdout是标准输出(即控制台)
进入bin目录执行logstash.bat -f …/config/console2console.conf
在这里插入图片描述
但是当输入中文时不能正常显示,由于编码问题。同时也可以让输出json格式

input {
  stdin{
	codec => plain{ charset => "GBK" }
  }
}

output{
  stdout{
	codec => json
  }
}

亲测可以正常了。
2.采集日志文件到es(这是目前最常被使用的方法)
在config目录下新建file2es.conf文件

input {
  file{
	path => ["E:/devetool/apache-tomcat-7.0.77/logs/catalina.2019-07-18.log"]
	#采集的编码格式
	codec => plain{ charset => "GBK" }
  }
}

output{
  elasticsearch{ 
	hosts => "localhost:9200"
	index => "tomcat-catalina-%{+YYYY.MM.dd}" #索引名称
	document_type => "tomcat-catalina" #type
  }  
  stdout{
  
  }
}

此处stdout输出是为了观察采集,生产不需要, 采集端的编码格式要根据自己的环境来设置哦。
进入bin目录执行logstash.bat -f …/config/file2es.conf,在启动tomcat。等下观察es中的数据。
在这里插入图片描述
还可以采集多个文件,还有对日志过滤预处理等复杂处理。贴一份比较全的供参考哦

input {
  file{
	path => "E:/devetool/apache-tomcat-7.0.77/logs/catalina.2019-07-18.log"
	codec => plain{ charset => "GBK" }
	type => "tomcat-catalina"
  }
  
  file{
	path => "E:/devetool/apache-tomcat-7.0.77/log/disconf-log4j.log"
	type => "disconf-log4j"
	#多行日志合并
	codec => multiline{
      negate => true  #是否匹配到
      pattern => "(?<datetime>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{3})" #匹配的正则
      what => "previous" #将没匹配到的合并到上一条,可选previous或next, previous是合并到匹配的上一行末尾
	}
  }
}

#从采集的数据处理
filter{
	grok{
		match => {"message" => "(?<logdatetime>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d{3})"}
		remove_field => "logdatetime"
		add_field =>{
            "from" => "lifeng"
        }
	}
	date{
        match => ["logdatetime", "yyyy-MM-dd HH:mm:ss,SSS"]   #这里是如果datetime跟后面的格式匹配上了就会去替换,替换什么呢?target默认指的就是@timestamp,所以就是以datetime的时间更新@timestamp的时间
		target => "@timestamp"
	}
}


output{
	if[type] == "tomcat-catalina"{
		elasticsearch{ 
			hosts => "localhost:9200"
			index => "tomcat-catalina-%{+YYYY.MM.dd}" #索引名称
			document_type => "tomcat-catalina" #type
		} 
	}
	
	if[type] == "disconf-log4j"{
		elasticsearch{ 
			hosts => "localhost:9200"
			index => "disconf-log4j-%{+YYYY.MM.dd}" #索引名称
			document_type => "disconf-log4j" #type
		} 
	}
	
	stdout{

	}
}

实际生产使用可能还需要定制更多参数。可以参考官网,也可以私信。如果觉得还可以点个赞哦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值