44# Copyright 2021 Datadog, Inc.
55
66
7- import logging
8- import re
97import gzip
10- import os
118import json
12- from logs .exceptions import ScrubbingException
9+ import logging
10+ import os
11+ import re
1312
1413from settings import DD_CUSTOM_TAGS , DD_RETRY_KEYWORD
1514
15+ from logs .exceptions import ScrubbingException
16+
1617logger = logging .getLogger ()
1718logger .setLevel (logging .getLevelName (os .environ .get ("DD_LOG_LEVEL" , "INFO" ).upper ()))
1819
@@ -24,28 +25,28 @@ def filter_logs(logs, include_pattern=None, exclude_pattern=None):
2425 """
2526 if include_pattern is None and exclude_pattern is None :
2627 return logs
28+
29+ logger .debug (f"Applying exclude pattern: { exclude_pattern } " )
30+ exclude_regex = compileRegex ("EXCLUDE_AT_MATCH" , exclude_pattern )
31+
32+ logger .debug (f"Applying include pattern: { include_pattern } " )
33+ include_regex = compileRegex ("INCLUDE_AT_MATCH" , include_pattern )
34+
2735 # Add logs that should be sent to logs_to_send
2836 logs_to_send = []
37+
2938 for log in logs :
30- if exclude_pattern is not None or include_pattern is not None :
31- logger .debug ("Filtering log event:" )
32- logger .debug (log )
3339 try :
34- if exclude_pattern is not None :
35- # if an exclude match is found, do not add log to logs_to_send
36- logger .debug (f"Applying exclude pattern: { exclude_pattern } " )
37- exclude_regex = compileRegex ("EXCLUDE_AT_MATCH" , exclude_pattern )
38- if re .search (exclude_regex , log ):
39- logger .debug ("Exclude pattern matched, excluding log event" )
40- continue
41- if include_pattern is not None :
42- # if no include match is found, do not add log to logs_to_send
43- logger .debug (f"Applying include pattern: { include_pattern } " )
44- include_regex = compileRegex ("INCLUDE_AT_MATCH" , include_pattern )
45- if not re .search (include_regex , log ):
46- logger .debug ("Include pattern did not match, excluding log event" )
47- continue
40+ if exclude_regex is not None and re .search (exclude_regex , log ):
41+ logger .debug ("Exclude pattern matched, excluding log event" )
42+ continue
43+
44+ if include_regex is not None and not re .search (include_regex , log ):
45+ logger .debug ("Include pattern did not match, excluding log event" )
46+ continue
47+
4848 logs_to_send .append (log )
49+
4950 except ScrubbingException :
5051 raise Exception ("could not filter the payload" )
5152
@@ -64,20 +65,22 @@ def compress_logs(batch, level):
6465
6566
6667def compileRegex (rule , pattern ):
67- if pattern is not None :
68- if pattern == "" :
69- # If pattern is an empty string, raise exception
70- raise Exception (
71- "No pattern provided:\n Add pattern or remove {} environment variable" .format (
72- rule
73- )
74- )
75- try :
76- return re .compile (pattern )
77- except Exception :
78- raise Exception (
79- "could not compile {} regex with pattern: {}" .format (rule , pattern )
68+ if pattern is None :
69+ return
70+
71+ if pattern == "" :
72+ # If pattern is an empty string, raise exception
73+ raise Exception (
74+ "No pattern provided:\n Add pattern or remove {} environment variable" .format (
75+ rule
8076 )
77+ )
78+ try :
79+ return re .compile (pattern )
80+ except Exception :
81+ raise Exception (
82+ "could not compile {} regex with pattern: {}" .format (rule , pattern )
83+ )
8184
8285
8386def add_retry_tag (log ):
0 commit comments