|
1 | 1 | # logstyles/formatter.py
|
2 | 2 | import threading
|
3 |
| - |
4 | 3 | from .utils import hex_to_ansi, reset_code
|
5 | 4 |
|
| 5 | +# logstyles/formatter.py |
6 | 6 | def escape_angle_brackets(text):
|
7 |
| - """Escapes '<' and '>' characters in the given text.""" |
8 |
| - return text.replace('<', '\\<').replace('>', '\\>') |
| 7 | + """Escapes '<' and '>' characters in the given text using HTML entities.""" |
| 8 | + return text.replace('<', '<').replace('>', '>') |
| 9 | + |
9 | 10 |
|
| 11 | +# logstyles/formatter.py |
10 | 12 | def create_formatter(theme, base_format, delimiter=None, override_included_parts=None):
|
11 | 13 | timestamp_format = theme.get('timestamp_format', '%Y-%m-%d %H:%M:%S')
|
12 | 14 | styles = theme['styles']
|
13 | 15 | delimiter = delimiter or base_format['delimiter']
|
14 | 16 | parts_order = base_format['parts_order']
|
15 | 17 |
|
16 |
| - included_parts = [ |
17 |
| - part.replace('_part', '') for part in parts_order |
18 |
| - ] |
19 |
| - |
| 18 | + # Determine which parts to include |
20 | 19 | if override_included_parts is not None:
|
21 | 20 | included_parts = override_included_parts
|
| 21 | + else: |
| 22 | + included_parts = [ |
| 23 | + part.replace('_part', '') for part in parts_order |
| 24 | + ] |
| 25 | + |
| 26 | + # Filter parts_order based on included_parts |
| 27 | + parts_order = [p for p in parts_order if p.replace('_part', '') in included_parts] |
| 28 | + remaining_parts = [p for p in included_parts if f"{p}_part" not in parts_order] |
| 29 | + parts_order.extend(f"{p}_part" for p in remaining_parts) |
| 30 | + |
22 | 31 |
|
23 | 32 | # Default and maximum widths for fields
|
24 | 33 | field_widths_config = {
|
@@ -55,31 +64,35 @@ def formatter(record):
|
55 | 64 |
|
56 | 65 | fields = {}
|
57 | 66 |
|
58 |
| - # Prepare field values |
59 |
| - for part in parts_order: |
60 |
| - part_key = part.replace('_part', '') |
61 |
| - if part_key == 'time' and 'time' in included_parts: |
62 |
| - fields['time'] = time_str |
63 |
| - elif part_key == 'level' and 'level' in included_parts: |
64 |
| - fields['level'] = level_name |
65 |
| - elif part_key == 'module' and 'module' in included_parts: |
66 |
| - module_name = escape_angle_brackets(record['module']) |
67 |
| - fields['module'] = module_name |
68 |
| - elif part_key == 'function' and 'function' in included_parts: |
69 |
| - function_name = escape_angle_brackets(record['function']) |
70 |
| - fields['function'] = function_name |
71 |
| - elif part_key == 'line' and 'line' in included_parts: |
72 |
| - line_str = str(record['line']) |
73 |
| - fields['line'] = line_str |
74 |
| - elif part_key == 'thread_name' and 'thread_name' in included_parts: |
75 |
| - thread_name = escape_angle_brackets(record['thread'].name) |
76 |
| - fields['thread_name'] = thread_name |
77 |
| - elif part_key == 'process_name' and 'process_name' in included_parts: |
78 |
| - process_name = escape_angle_brackets(record['process'].name) |
79 |
| - fields['process_name'] = process_name |
80 |
| - elif part_key == 'message' and 'message' in included_parts: |
81 |
| - message = escape_angle_brackets(record['message']) |
82 |
| - fields['message'] = message |
| 67 | + # Retrieve values, prioritizing record['extra'] for custom fields |
| 68 | + # Only set fields if they are included |
| 69 | + if 'time' in included_parts: |
| 70 | + fields['time'] = time_str |
| 71 | + if 'level' in included_parts: |
| 72 | + fields['level'] = level_name |
| 73 | + |
| 74 | + # For fields that can be overridden by extra: |
| 75 | + if 'module' in included_parts: |
| 76 | + module_name = escape_angle_brackets(record['extra'].get('module', record['module'])) |
| 77 | + fields['module'] = module_name |
| 78 | + if 'function' in included_parts: |
| 79 | + function_name = escape_angle_brackets(record['extra'].get('function', record['function'])) |
| 80 | + fields['function'] = function_name |
| 81 | + if 'line' in included_parts: |
| 82 | + line_val = record['extra'].get('line', record['line']) |
| 83 | + line_str = escape_angle_brackets(str(line_val)) |
| 84 | + fields['line'] = line_str |
| 85 | + if 'thread_name' in included_parts: |
| 86 | + thread_val = record['extra'].get('thread_name', record['thread'].name) |
| 87 | + thread_name = escape_angle_brackets(thread_val) |
| 88 | + fields['thread_name'] = thread_name |
| 89 | + if 'process_name' in included_parts: |
| 90 | + process_val = record['extra'].get('process_name', record['process'].name) |
| 91 | + process_name = escape_angle_brackets(process_val) |
| 92 | + fields['process_name'] = process_name |
| 93 | + if 'message' in included_parts: |
| 94 | + message = escape_angle_brackets(record['message']) |
| 95 | + fields['message'] = message |
83 | 96 |
|
84 | 97 | # Update current field widths up to maximums
|
85 | 98 | with field_widths_lock:
|
|
0 commit comments