发布于2026-07-08 阅读(0)
扫一扫,手机访问
跟日志打交道多了,谁还没被海量信息折腾过?一行一行翻?不现实。今天这篇实战,就专门聊聊怎么用正则表达式把日志里那些关键字段精准揪出来。不管是请求方法、时间戳,还是异常堆栈,读完你就能上手,顺便还能搞定几个平时容易卡住的坑。

答案其实很直接:用Python的re模块,写个正则把HTTP请求行里的方法和URL逮出来。
import re
# 示例日志行
log_line = '127.0.0.1 - - [10/Oct/2023:13:55:36 +0000] "GET /index.html HTTP/1.1" 200 4824'
# 正则表达式
pattern = r'"(GET|POST|PUT|DELETE) ([^\s]+)'
# 匹配
match = re.search(pattern, log_line)
if match:
request_method = match.group(1) # 提取请求方法
url = match.group(2) # 提取URL
print(f"请求方法: {request_method}, URL: {url}")
关键点就两个:re.search对付单行匹配,group(1)和group(2)分别拿第一个和第二个括号捕获的内容。
这个场景更常见——比如只想看POST请求或ERROR日志。用re.findall配合过滤,写起来很干净。
import re
# 示例日志列表
log_lines = [
'127.0.0.1 - - [10/Oct/2023:13:55:36 +0000] "GET /index.html HTTP/1.1" 200 4824',
'127.0.0.1 - - [10/Oct/2023:13:55:40 +0000] "POST /login HTTP/1.1" 302 552',
'127.0.0.1 - - [10/Oct/2023:13:55:45 +0000] "GET /about.html HTTP/1.1" 200 2345'
]
# 正则表达式
pattern = r'POST'
# 过滤包含特定关键词的日志行
filtered_lines = [line for line in log_lines if re.search(pattern, line)]
for line in filtered_lines:
print(line)
关键点:re.search判断是否命中,列表推导式一把梭,干净利落。
时间戳格式通常比较固定,像方括号包里那串。用括号捕获一下就出来了。
import re
# 示例日志行
log_line = '127.0.0.1 - - [10/Oct/2023:13:55:36 +0000] "GET /index.html HTTP/1.1" 200 4824'
# 正则表达式
pattern = r'\[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+\-]\d{4})\]'
# 匹配
match = re.search(pattern, log_line)
if match:
timestamp = match.group(1)
print(f"时间戳: {timestamp}")
关键点:时间戳格式有套路,大括号里的部分正好是我们要的。
单行能搞定,多行也不怕。用re.finditer逐行迭代,每个match都能拿到所需字段。
import re
# 示例多行日志
log_data = '''127.0.0.1 - - [10/Oct/2023:13:55:36 +0000] "GET /index.html HTTP/1.1" 200 4824
127.0.0.1 - - [10/Oct/2023:13:55:40 +0000] "POST /login HTTP/1.1" 302 552
127.0.0.1 - - [10/Oct/2023:13:55:45 +0000] "GET /about.html HTTP/1.1" 200 2345'''
# 正则表达式
pattern = r'"(GET|POST|PUT|DELETE) ([^\s]+) \S+" (\d{3})'
# 处理多行日志
for match in re.finditer(pattern, log_data):
request_method = match.group(1)
url = match.group(2)
status_code = match.group(3)
print(f"请求方法: {request_method}, URL: {url}, 状态码: {status_code}")
关键点:re.finditer返回迭代器,不一次性加载全部,适合大日志文件。
遇到那种字段特别多的日志,比如IP、时间、方法、URL、协议、状态码、响应大小、User-Agent全都挤在一行里,那就得把正则写得细一点。一个括号一个字段,清晰可控。
import re
# 示例复杂日志行
log_line = '127.0.0.1 - - [10/Oct/2023:13:55:36 +0000] "GET /api/v1/users/123?query=abc HTTP/1.1" 200 4824 - "Mozilla/5.0"'
# 正则表达式
pattern = r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - - \[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+\-]\d{4})\] "(\w+) (\S+)' \
r'(\S+)" (\d{3}) (\d+) - "(.*)"'
# 匹配
match = re.match(pattern, log_line)
if match:
ip_address = match.group(1)
timestamp = match.group(2)
request_method = match.group(3)
request_url = match.group(4)
request_protocol = match.group(5)
status_code = match.group(6)
response_size = match.group(7)
user_agent = match.group(8)
print(f"IP地址: {ip_address}, 时间戳: {timestamp}, 请求方法: {request_method}, 请求URL: {request_url}, "
f"请求协议: {request_protocol}, 状态码: {status_code}, 响应大小: {response_size}, User-Agent: {user_agent}")
关键点:正则越详细,提取越精准。不过别忘了测试,日志格式一变就得跟着调。
生产日志里最怕看到ERROR,但真要分析时,往往需要把完整异常堆栈捞出来。一般异常日志开头有固定的关键字,比如“ERROR [时间戳] [线程] 异常描述...”,用正则把关键字之后的整段内容捕获就行。
import re
# 示例日志行
log_line = 'ERROR [10/Oct/2023:13:55:36 +0000] [Thread-2] Exception in thread "main" ja va.lang.NullPointerException: Cannot invoke "ja va.util.List.get(int)" because the return value of "ja va.util.List.stream()" is null'
# 正则表达式
pattern = r'ERROR \[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+\-]\d{4})\] \[(\w+)\] (.*)'
# 匹配
match = re.search(pattern, log_line)
if match:
timestamp = match.group(1)
thread = match.group(2)
exception_info = match.group(3)
print(f"时间戳: {timestamp}, 线程: {thread}, 异常信息: {exception_info}")
关键点:异常信息一般很长,正则里用(.*)捕获剩余全部内容,然后按需处理即可。
如果日志每小时生成一次,甚至更频繁,手动跑脚本就不现实了。解决方案是把脚本挂到系统定时任务里,比如Linux的cron,或者Windows的任务计划程序。脚本可以封装成一个函数,用requests从远端拉取日志,再用我们前面写的正则提取字段。下面是一个示例,演示了从URL获取日志并逐行解析的流程。
import re
import requests
# 定义日志处理函数
def extract_log_data():
# 从服务器获取日志数据
log_data = requests.get('http://example.com/log.txt').text
# 正则表达式
pattern = r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) - - \[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+\-]\d{4})\] "(\w+) (\S+)' \
r'(\S+)" (\d{3}) (\d+) - "(.*)"'
# 处理多行日志
for match in re.finditer(pattern, log_data):
ip_address = match.group(1)
timestamp = match.group(2)
request_method = match.group(3)
request_url = match.group(4)
request_protocol = match.group(5)
status_code = match.group(6)
response_size = match.group(7)
user_agent = match.group(8)
print(f"IP地址: {ip_address}, 时间戳: {timestamp}, 请求方法: {request_method}, 请求URL: {request_url}, "
f"请求协议: {request_protocol}, 状态码: {status_code}, 响应大小: {response_size}, User-Agent: {user_agent}")
# 使用系统的定时任务工具(如cron)来周期性执行此脚本
# 例如,每5分钟运行一次:*/5 * * * * python3 /path/to/your/script.py
if __name__ == "__main__":
extract_log_data()
关键点:把正则解析和网络请求封装成函数,然后扔给cron或任务计划程序定期跑,日志处理就完全自动了。
这几个实战案例基本涵盖了从简单到复杂的日志提取场景。只要你熟悉了正则的捕获组、搜索和迭代模式,不管是Apache访问日志还是自定义异常日志,都能快速写出对应的解析脚本。下次再面对堆积如山的日志文件,不用再头疼了——几行正则下去,关键数据直接到手。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8