这是与@Oli 非常相似的答案,但是正则表达式更具可读性,我使用groupdict()
因此无需形成新字典,因为它是由正则表达式创建的。日志字符串从左到右解析,消耗每个匹配项。
fmt = re.compile(
r'\[(?P<timestamp>.+?)\]\s+' # Save everything within [] to group timestamp
r'\((?P<thread_id>.+?)\)\s+' # Save everything within () to group thread_id
r'\<(?P<module_name>.+?)\>\s+' # Save everything within <> to group module_name
r'\[(?P<log_level>.+?)\]\s+' # Save everything within [] to group to log_level
r'(?P<message_desc>.+?)(\s:\s|$)' # Save everything before \s:\s or end of line to group message_desc,
r'(?P<message>.+$)?' # if there was a \s:\s, save everything after it to group message. This last group is optional
)
log = '[2013-Mar-05 18:21:45.415053] (4139) <ModuleA> [DEBUG] Message Desciption : An example message!'
match = fmt.search(log)
print match.groupdict()
例子:
log = '[2013-Mar-05 18:21:45.415053] (4139) <ModuleA> [DEBUG] Message Desciption : An example message!'
match = fmt.search(log)
print match.groupdict()
{'log_level': 'DEBUG',
'message': 'An example message!',
'module_name': 'ModuleA',
'thread_id': '4139',
'timestamp': '2013-Mar-05 18:21:45.415053'}
此答案评论中的第一个测试字符串示例
log = '[2013-Mar-05 18:21:45.415053] (0x7aa5e3a0) <Logger> [Info] Opened settings file : /usr/local/ABC/ABC/var/loggingSettings.ini'
match = fmt.search(log)
print match.groupdict()
{'log_level': 'Info',
'message': '/usr/local/ABC/ABC/var/loggingSettings.ini',
'message_desc': 'Opened settings file',
'module_name': 'Logger',
'thread_id': '0x7aa5e3a0',
'timestamp': '2013-Mar-05 18:21:45.415053'}
此答案评论中的第二个测试字符串示例:
log = '[2013-Mar-05 18:21:45.415053] (0x7aa5e3a0) <Logger> [Info] Creating a new settings file'
match = fmt.search(log)
print match.groupdict()
{'log_level': 'Info',
'message': None,
'message_desc': 'Creating a new settings file',
'module_name': 'Logger',
'thread_id': '0x7aa5e3a0',
'timestamp': '2013-Mar-05 18:21:45.415053'}
编辑:已修复以使用 OP 的示例。