本文介绍了解析Apache通用日志格式的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从一个常见的日志格式日志文件中获取三条信息.日志文件的条目为:
I'm trying to take three pieces of information from a common log format log file. An entry of the log file would be:
65.54.188.137 - - [03/Oct/2007:02:20:22 -0400] "GET /~longa/statistics/code/xlispstat/smoothers/spline/ HTTP/2.0" 301 2633
,然后,我想将IP,URL和状态代码的出现次数存储在哈希中.我认为他们每个人都必须独立存在.即使您能指出正确的方向,也可以提供任何帮助.
and from that, I want to store the number of occurrences of the IP, the URLs, and the status codes in a hash. I figured they each have to be in their own. Any help would be appreciated, even if you can just point me in the right direction.
推荐答案
您可以使用正则表达式从日志条目中读取信息.像这样:
You can read the information from the log entries with a regular expression. Something like this:
lines.each do |line|
matches = /^(\S+).*GET\s(.*)\sHTTP\S*\s(\d+)/.match(line)
ip = matches[1]
url = matches[2]
status = matches[3]
do
然后,您可以将该信息放入哈希中,并按自己的意愿进行处理.
Then you can put this information into a hash and process it how you like.
这篇关于解析Apache通用日志格式的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!