|
1 | | -import atexit |
2 | 1 | import logging |
3 | | -import os |
4 | | -import threading |
5 | 2 |
|
6 | 3 | from logging.config import dictConfig |
7 | 4 | from pathlib import Path |
8 | 5 | from sys import stdout |
9 | 6 |
|
10 | | -import requests |
11 | | - |
12 | 7 | from dotenv import load_dotenv |
13 | 8 |
|
14 | 9 | from memos import settings |
15 | 10 | from memos.api.context.context import get_current_trace_id |
16 | | -from memos.api.context.context_thread import ContextThreadPoolExecutor |
17 | 11 |
|
18 | 12 |
|
19 | 13 | # Load environment variables |
@@ -45,89 +39,6 @@ def filter(self, record): |
45 | 39 | return True |
46 | 40 |
|
47 | 41 |
|
48 | | -class CustomLoggerRequestHandler(logging.Handler): |
49 | | - _instance = None |
50 | | - _lock = threading.Lock() |
51 | | - |
52 | | - def __new__(cls): |
53 | | - if cls._instance is None: |
54 | | - with cls._lock: |
55 | | - if cls._instance is None: |
56 | | - cls._instance = super().__new__(cls) |
57 | | - cls._instance._initialized = False |
58 | | - cls._instance._executor = None |
59 | | - cls._instance._session = None |
60 | | - cls._instance._is_shutting_down = None |
61 | | - return cls._instance |
62 | | - |
63 | | - def __init__(self): |
64 | | - """Initialize handler with minimal setup""" |
65 | | - if not self._initialized: |
66 | | - super().__init__() |
67 | | - workers = int(os.getenv("CUSTOM_LOGGER_WORKERS", "2")) |
68 | | - self._executor = ContextThreadPoolExecutor( |
69 | | - max_workers=workers, thread_name_prefix="log_sender" |
70 | | - ) |
71 | | - self._is_shutting_down = threading.Event() |
72 | | - self._session = requests.Session() |
73 | | - self._initialized = True |
74 | | - atexit.register(self._cleanup) |
75 | | - |
76 | | - def emit(self, record): |
77 | | - """Process log records of INFO or ERROR level (non-blocking)""" |
78 | | - if os.getenv("CUSTOM_LOGGER_URL") is None or self._is_shutting_down.is_set(): |
79 | | - return |
80 | | - |
81 | | - try: |
82 | | - trace_id = get_current_trace_id() or "no-trace-id" |
83 | | - self._executor.submit(self._send_log_sync, record.getMessage(), trace_id) |
84 | | - except Exception as e: |
85 | | - if not self._is_shutting_down.is_set(): |
86 | | - print(f"Error sending log: {e}") |
87 | | - |
88 | | - def _send_log_sync(self, message, trace_id): |
89 | | - """Send log message synchronously in a separate thread""" |
90 | | - try: |
91 | | - logger_url = os.getenv("CUSTOM_LOGGER_URL") |
92 | | - token = os.getenv("CUSTOM_LOGGER_TOKEN") |
93 | | - |
94 | | - headers = {"Content-Type": "application/json"} |
95 | | - post_content = {"message": message, "trace_id": trace_id} |
96 | | - |
97 | | - # Add auth token if exists |
98 | | - if token: |
99 | | - headers["Authorization"] = f"Bearer {token}" |
100 | | - |
101 | | - # Add traceId to headers for consistency |
102 | | - headers["traceId"] = trace_id |
103 | | - |
104 | | - # Add custom attributes from env |
105 | | - for key, value in os.environ.items(): |
106 | | - if key.startswith("CUSTOM_LOGGER_ATTRIBUTE_"): |
107 | | - attribute_key = key[len("CUSTOM_LOGGER_ATTRIBUTE_") :].lower() |
108 | | - post_content[attribute_key] = value |
109 | | - |
110 | | - self._session.post(logger_url, headers=headers, json=post_content, timeout=5) |
111 | | - except Exception: |
112 | | - # Silently ignore errors to avoid affecting main application |
113 | | - pass |
114 | | - |
115 | | - def _cleanup(self): |
116 | | - """Clean up resources during program exit""" |
117 | | - if not self._initialized: |
118 | | - return |
119 | | - |
120 | | - self._is_shutting_down.set() |
121 | | - try: |
122 | | - self._executor.shutdown(wait=False) |
123 | | - self._session.close() |
124 | | - except Exception as e: |
125 | | - print(f"Error during cleanup: {e}") |
126 | | - |
127 | | - def close(self): |
128 | | - """Override close to prevent premature shutdown""" |
129 | | - |
130 | | - |
131 | 42 | LOGGING_CONFIG = { |
132 | 43 | "version": 1, |
133 | 44 | "disable_existing_loggers": False, |
|
0 commit comments