Logstash7.6 @timestamp早8小时,urldecode解决中文URL编码

avatar 2021年12月28日09:07:24 6 1681 views
博主分享免费Java教学视频,B站账号:Java刘哥

本文介绍我最近使用 Logstash 遇到的2个问题

博主 logstash 版本为最新版 7.6

一、解决 @Timestamp 早 8小时

先说下原因,默认编码是 UTC,即世界统一时间

参考:

https://github.com/elastic/logstash/issues/2491

https://github.com/elastic/logstash/pull/1784

网上有很多教程是修改源码,官方是不推荐的,官方建议用 

 

 

方法一、

在 filter 里加

ruby {
	code => "event.set('timestamp', event.get('@timestamp').time.localtime + 8*60*60)"
}
ruby {
	code => "event.set('@timestamp',event.get('timestamp'))"
}

说明:添加 timestamp 属性,然后给该属性在 @timestamp 属性上加8小时,然后把 timestamp 的值赋给 @timestamp

 

方法二、

参考 https://github.com/elastic/logstash/issues/2491

博主没有实验

 

二、中文URL编码解码

因为我打印的日志里中文URL好多是乱码的,即被URL编码了,现在需要解码

解码所有字段

urldecode{
	all_fields => true
}

解码指定字段

urldecode{
	fields => ["repoName"]
	fields => ["message"]
}

 

三、logstash.conf 完整内容

input {  
    file{
        path => 'D:/Apache2.2/logs/svn*.log'
        start_position => "beginning"
    }
} 
filter {  

	grok {
		match =>  {
		  "message" =>  "\[%{GREEDYDATA:ip}\]\[%{GREEDYDATA:user}\]\[%{GREEDYDATA:createTime}\]\[%{GREEDYDATA:action}\]\[%{GREEDYDATA:repoName}\]"
		}
	}
	ruby {
		code => "event.set('timestamp', event.get('@timestamp').time.localtime + 8*60*60)"
	}
	ruby {
		code => "event.set('@timestamp',event.get('timestamp'))"
	}
	mutate {
		remove_field => ["timestamp"]
		remove_field => ["message"]
		remove_field => ["path"]
		remove_field => ["@version"]
		remove_field => ["host"]
	}

	urldecode{
		all_fields => true
	}
}

output {
	elasticsearch {
		hosts => ["127.0.0.1:9200"]
		index => "svn-log"
	}
}

 

日志格式:

[10.1.5.1][zhangsan][2021-12-27 09:55:45][checkout-or-export / r1 depth=infinity][%E6%B5%8B%E8%AF%95%E9%A1%B9%E7%9B%AE]
[10.1.5.1][zhangsan][2021-12-27 09:56:31][checkout-or-export / r1 depth=infinity][%E6%B5%8B%E8%AF%95%E9%A1%B9%E7%9B%AE]
[10.1.5.1][zhangsan][2021-12-27 10:06:49][checkout-or-export / r1 depth=infinity][%E6%B5%8B%E8%AF%95%E9%A1%B9%E7%9B%AE]
[10.1.5.1][zhangsan][2021-12-27 10:07:56][commit r2][%E6%B5%8B%E8%AF%95%E9%A1%B9%E7%9B%AE]
[10.1.5.1][zhangsan][2021-12-27 10:08:09][commit r3][%E6%B5%8B%E8%AF%95%E9%A1%B9%E7%9B%AE]
[10.1.5.1][zhangsan][2021-12-27 10:08:27][commit r4][%E6%B5%8B%E8%AF%95%E9%A1%B9%E7%9B%AE]

 

四、关于时区的问题后续更新

后面整合kibana的时候发现,显示和查询的时间有问题。

最终结论,es 和 logstash 不需要处理时间,统一默认使用 UTC,即 ES 上显示早8小时没有关系,在 kibana 显示的时候会自动根据当前时区转换的。

 

 

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0