-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_stream.py
More file actions
74 lines (63 loc) · 2.58 KB
/
Copy pathlog_stream.py
File metadata and controls
74 lines (63 loc) · 2.58 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Thread-safe log queue + custom logging handler.
Log records are pushed into a queue; the SSE endpoint drains it to the browser.
"""
import logging
import queue
import time
from datetime import datetime
_log_queue: queue.Queue = queue.Queue(maxsize=500)
LEVEL_EMOJI = {
"DEBUG": "🔍",
"INFO": "ℹ️",
"WARNING": "⚠️",
"ERROR": "❌",
"CRITICAL": "🚨",
}
STEP_MARKERS = {
"Starting daily recommendation pipeline": ("🚀", "Pipeline started"),
"Fetching Scholar profile": ("👤", "Loading your Scholar profile"),
"Using cached Scholar profile": ("👤", "Scholar profile loaded from cache"),
"Fetching arxiv papers": ("📡", "Fetching latest arXiv papers"),
"Got first page": ("📄", "arXiv papers received"),
"Topic signals": ("🗳️", "Loading vote signals"),
"ML ranker": ("🤖", "Training ML preference model"),
"TF-IDF model": ("🤖", "TF-IDF model ready"),
"Computing reputation": ("🏛️", "Scoring institution reputation"),
"HTTP Request: POST https://api.anthropic": ("✨", "Claude is ranking papers…"),
"Summarized:": ("📝", "Generating summary"),
"Saved": ("💾", "Recommendations saved"),
"Pipeline complete": ("✅", "Pipeline complete!"),
}
class QueueHandler(logging.Handler):
def emit(self, record: logging.LogRecord):
msg = self.format(record)
level = record.levelname
emoji = LEVEL_EMOJI.get(level, "•")
# Detect named step
step_label = None
for marker, (step_emoji, label) in STEP_MARKERS.items():
if marker in msg:
emoji = step_emoji
step_label = label
break
entry = {
"ts": datetime.now().strftime("%H:%M:%S"),
"level": level,
"emoji": emoji,
"step": step_label,
"msg": record.getMessage(),
}
try:
_log_queue.put_nowait(entry)
except queue.Full:
pass # drop oldest would be nicer but this is fine
def get_queue() -> queue.Queue:
return _log_queue
def install_handler():
"""Attach QueueHandler to the root logger."""
handler = QueueHandler()
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(name)s: %(message)s")
handler.setFormatter(formatter)
logging.getLogger().addHandler(handler)