且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解析Apache通用日志格式的日志文件

更新时间:2022-06-24 10:09:54

您可以使用正则表达式从日志条目中读取信息.像这样:

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.