我有以下正则表达式用于解析日志文件。

(^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} ((?:WARNING)|(?:ERROR)|(?:DEBUG)|(?:INFO)))(.*(?!\1))

我正在使用findWithinHorizo​​n将文件拆分为条目。

它适用于所有行,除非日志条目多于一行,然后它仅与第一行匹配。有没有办法启用多行匹配?我正在使用

Pattern.compile(myPattern,Pattern.MULTILINE);


现在,它似乎不起作用。

 public static List<LogToken> tokenize(String filename) throws FileNotFoundException {
        Scanner logReader = new Scanner(new File(filename));
        Pattern linePattern = Pattern.compile(_logEntryMarker,Pattern.MULTILINE);

        while (logReader.hasNextLine()) {
            String entry = logReader.findWithinHorizon(linePattern, 0);
            log.debug("Entry:" + entry);
            logReader.nextLine();
        }
....


日志示例

2020-06-03 12:42:49,311 DEBUG - __init__.py:24 - Downloading https://example.com/432fa72661a80e01d68aaafc285c7c65190f4add24b2beee7961df19b47f9c19
2020-06-03 12:42:49,430 DEBUG - __init__.py:26 - Saved https://example.com/432fa72661a80e01d68aaafc285c7c65190f4add24b2beee7961df19b47f9c19 to /tmp/tmpwur9pw14
2020-06-03 12:42:52,653 WARNING - dr.py:974 - Traceback (most recent call last):
File "/opt/app-root/lib/python3.6/site-packages/insights/core/dr.py", line 962, in run
result = DELEGATES[component].process(broker)
File "/opt/app-root/lib/python3.6/site-packages/insights/core/dr.py", line 681, in process
return self.invoke(broker)
File "/opt/app-root/lib/python3.6/site-packages/insights/core/plugins.py", line 64, in invoke
return super(PluginType, self).invoke(broker)
File "/opt/app-root/lib/python3.6/site-packages/insights/core/dr.py", line 661, in invoke
return self.component(*args)
File "/opt/app-root/lib/python3.6/site-packages/ccx_ocp_core/models/nodes.py", line 108, in Nodes
int(node.q.status.capacity.memory.value.split("Ki")[0]) / (1000 * 1000), 2
AttributeError: 'NoneType' object has no attribute 'split'

2020-06-03 12:42:52,757 INFO - payload_tracker_watcher.py:66 - Payload Tracker update successfully sent: {anonymized} processing
2020-06-03 12:42:52,759 DEBUG - kafka_publisher.py:74 - Sending response to the ccx.ocp.results topic.


总共应该有5个条目。第三项应包含python追溯。

最佳答案

@Cary Swoveland正确答案

使用Pattern.DOTALL解决了问题

10-07 15:53