使用正则表达式解析日志

4
我正在尝试使用Python的正则表达式解析Apache日志,并将其分配给单独的变量。
ACCESS_LOG_PATTERN = '^(\S+) (\S+) (\S+) \[([\w:/]+\s[+\-]\d{4})\] "(\S+) (\S+)\s*(\S+)\s*" (\d{3}) (\S+)'

logLine='127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] "GET /images/launch-logo.gif HTTP/1.0" 200 1839'

我将把它解析并分组到以下变量中:

match = re.search(APACHE_ACCESS_LOG_PATTERN, logLine)



    host          = match.group(1)

    client_identd = match.group(2)

    user_id       = match.group(3)

    date_time     = match.group(4)

    method        = match.group(5)

    endpoint      = match.group(6)

    protocol      = match.group(7)

    response_code = int(match.group(8))

    content_size  = match.group(9)

正则表达式模式对于日志行的工作正常,但是对于以下情况,解析/正则表达式匹配失败:
'127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] "GET /" 200 1839'

'127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] "GET / " 200 1839'

我该如何解决这个问题?


可能是解析Apache日志文件的重复问题。 - Wolph
我需要的要求是针对日志行请求部分的特定内容,而不是通用的日志解析。 - Rocky Ray
2个回答

7
您需要通过添加“?”使您的group 7可选。使用以下正则表达式:
"^(\S+) (\S+) (\S+) \[([\w:/]+\s[+\-]\d{4})\] (\S+) (\S+)\s*(\S+)?\s* (\d{3}) (\S+)"
                                                                 ↑

请查看演示

输出:

[
  [
    {
      "content": "127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] \"GET /images/launch-logo.gif HTTP/1.0\" 200 1839",
      "isParticipating": true,
      "groupNum": 0,
      "startPos": 0,
      "endPos": 90
    },
    {
      "content": "127.0.0.1",
      "isParticipating": true,
      "groupNum": 1,
      "startPos": 0,
      "endPos": 9
    },
    {
      "content": "-",
      "isParticipating": true,
      "groupNum": 2,
      "startPos": 10,
      "endPos": 11
    },
    {
      "content": "-",
      "isParticipating": true,
      "groupNum": 3,
      "startPos": 12,
      "endPos": 13
    },
    {
      "content": "01/Jul/1995:00:00:01 -0400",
      "isParticipating": true,
      "groupNum": 4,
      "startPos": 15,
      "endPos": 41
    },
    {
      "content": "\"GET",
      "isParticipating": true,
      "groupNum": 5,
      "startPos": 43,
      "endPos": 47
    },
    {
      "content": "/images/launch-logo.gif",
      "isParticipating": true,
      "groupNum": 6,
      "startPos": 48,
      "endPos": 71
    },
    {
      "content": "HTTP/1.0\"",
      "isParticipating": true,
      "groupNum": 7,
      "startPos": 72,
      "endPos": 81
    },
    {
      "content": "200",
      "isParticipating": true,
      "groupNum": 8,
      "startPos": 82,
      "endPos": 85
    },
    {
      "content": "1839",
      "isParticipating": true,
      "groupNum": 9,
      "startPos": 86,
      "endPos": 90
    }
  ],
  [
    {
      "content": "127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] \"GET /\" 200 1839",
      "isParticipating": true,
      "groupNum": 0,
      "startPos": 91,
      "endPos": 150
    },
    {
      "content": "127.0.0.1",
      "isParticipating": true,
      "groupNum": 1,
      "startPos": 91,
      "endPos": 100
    },
    {
      "content": "-",
      "isParticipating": true,
      "groupNum": 2,
      "startPos": 101,
      "endPos": 102
    },
    {
      "content": "-",
      "isParticipating": true,
      "groupNum": 3,
      "startPos": 103,
      "endPos": 104
    },
    {
      "content": "01/Jul/1995:00:00:01 -0400",
      "isParticipating": true,
      "groupNum": 4,
      "startPos": 106,
      "endPos": 132
    },
    {
      "content": "\"GET",
      "isParticipating": true,
      "groupNum": 5,
      "startPos": 134,
      "endPos": 138
    },
    {
      "content": "/\"",
      "isParticipating": true,
      "groupNum": 6,
      "startPos": 139,
      "endPos": 141
    },
    {
      "content": "",
      "isParticipating": false,
      "groupNum": 7,
      "startPos": -1,
      "endPos": -1
    },
    {
      "content": "200",
      "isParticipating": true,
      "groupNum": 8,
      "startPos": 142,
      "endPos": 145
    },
    {
      "content": "1839",
      "isParticipating": true,
      "groupNum": 9,
      "startPos": 146,
      "endPos": 150
    }
  ],
  [
    {
      "content": "127.0.0.1 - - [01/Jul/1995:00:00:01 -0400] \"GET / \" 200 1839",
      "isParticipating": true,
      "groupNum": 0,
      "startPos": 152,
      "endPos": 212
    },
    {
      "content": "127.0.0.1",
      "isParticipating": true,
      "groupNum": 1,
      "startPos": 152,
      "endPos": 161
    },
    {
      "content": "-",
      "isParticipating": true,
      "groupNum": 2,
      "startPos": 162,
      "endPos": 163
    },
    {
      "content": "-",
      "isParticipating": true,
      "groupNum": 3,
      "startPos": 164,
      "endPos": 165
    },
    {
      "content": "01/Jul/1995:00:00:01 -0400",
      "isParticipating": true,
      "groupNum": 4,
      "startPos": 167,
      "endPos": 193
    },
    {
      "content": "\"GET",
      "isParticipating": true,
      "groupNum": 5,
      "startPos": 195,
      "endPos": 199
    },
    {
      "content": "/",
      "isParticipating": true,
      "groupNum": 6,
      "startPos": 200,
      "endPos": 201
    },
    {
      "content": "\"",
      "isParticipating": true,
      "groupNum": 7,
      "startPos": 202,
      "endPos": 203
    },
    {
      "content": "200",
      "isParticipating": true,
      "groupNum": 8,
      "startPos": 204,
      "endPos": 207
    },
    {
      "content": "1839",
      "isParticipating": true,
      "groupNum": 9,
      "startPos": 208,
      "endPos": 212
    }
  ]
]

3
import re


HOST = r'^(?P<host>.*?)'
SPACE = r'\s'
IDENTITY = r'\S+'
USER = r'\S+'
TIME = r'(?P<time>\[.*?\])'
REQUEST = r'\"(?P<request>.*?)\"'
STATUS = r'(?P<status>\d{3})'
SIZE = r'(?P<size>\S+)'

REGEX = HOST+SPACE+IDENTITY+SPACE+USER+SPACE+TIME+SPACE+REQUEST+SPACE+STATUS+SPACE+SIZE+SPACE

def parser(log_line):
    match = re.search(REGEX,log_line)
    return ( (match.group('host'),
            match.group('time'), 
                      match.group('request') , 
                      match.group('status') ,
                      match.group('size')
                     )
                   )


logLine = """180.76.15.30 - - [24/Mar/2017:19:37:57 +0000] "GET /shop/page/32/?count=15&orderby=title&add_to_wishlist=4846 HTTP/1.1" 404 10202 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"""
result = parser(logLine)
print(result)

结果

('180.76.15.30', '[24/Mar/2017:19:37:57 +0000]', 'GET /shop/page/32/?count=15&orderby=title&add_to_wishlist=4846 HTTP/1.1', '404', '10202')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接