-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_lombok_slf4j.py
More file actions
49 lines (39 loc) · 2.19 KB
/
fix_lombok_slf4j.py
File metadata and controls
49 lines (39 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import re
def fix_slf4j_in_file(filepath):
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
except Exception:
return
# Check if we have @Slf4j but no SLF4J imports, implying we expect Lombok to work.
# However, if Lombok isn't generating the `log` variable for some reason (e.g. IDE config vs Maven),
# or if we want to ensure standard logging, we might explicitly declare the logger.
# Alternatively, ensure the class isn't doing something weird that breaks Lombok.
# Actually, in the error earlier, 'log' couldn't be found.
# Let's revert @Slf4j back to explicit Logger definitions in the Flink engine
# just in case Flink's compiler plugin or something is stripping Lombok annotations before processing.
if 'import lombok.extern.slf4j.Slf4j;' in content and '@Slf4j' in content:
class_match = re.search(r'public\s+class\s+(\w+)', content)
if class_match:
class_name = class_match.group(1)
# Remove Lombok imports/annotations
content = content.replace('import lombok.extern.slf4j.Slf4j;\n', '')
content = content.replace('@Slf4j\n', '')
# Add SLF4J imports
if 'import org.slf4j.Logger;' not in content:
content = re.sub(r'package\s+[\w\.]+;', r'\g<0>\n\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;', content)
# Add logger field
logger_field = f"\n private static final Logger log = LoggerFactory.getLogger({class_name}.class);\n"
content = re.sub(r'(public\s+class\s+\w+.*?\{)', r'\g<1>' + logger_field, content, count=1, flags=re.DOTALL)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
def walk_and_fix(root_dir):
for root, dirs, files in os.walk(root_dir):
if any(x in root for x in ['target', '.git', '.idea']):
continue
for file in files:
if file.endswith('.java'):
fix_slf4j_in_file(os.path.join(root, file))
if __name__ == "__main__":
walk_and_fix('crossbet-server/java/crossbet-flink-engine')