File size: 1,642 Bytes
5889992
 
 
 
 
 
 
ac6202c
 
5889992
 
ac6202c
 
 
 
5889992
 
ac6202c
 
 
 
 
 
5889992
 
 
 
ac6202c
5889992
 
ac6202c
 
 
5889992
ac6202c
 
 
 
 
 
 
5889992
 
 
 
ac6202c
5889992
ac6202c
5889992
 
 
ac6202c
5889992
 
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
import logging
import json
from datetime import datetime
from typing import Any, Dict
from pathlib import Path

class TheryBotLogger:
    _initialized = False

    def __init__(self, log_dir: Path = Path("logs")):
        self.log_dir = log_dir
        if not TheryBotLogger._initialized:
            self._setup_logging()
            TheryBotLogger._initialized = True

    def _setup_logging(self) -> None:
        self.log_dir.mkdir(parents=True, exist_ok=True)

        # Only add handlers if the root logger has none yet
        root = logging.getLogger()
        if root.handlers:
            return

        file_handler = logging.FileHandler(
            self.log_dir / f"thery_bot_{datetime.now():%Y%m%d}.log"
        )
        file_handler.setLevel(logging.INFO)

        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.WARNING)

        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        )
        file_handler.setFormatter(formatter)
        console_handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))

        root.setLevel(logging.INFO)
        root.addHandler(file_handler)
        root.addHandler(console_handler)

    def log_interaction(
        self,
        interaction_type: str,
        data: Dict[str, Any],
        level: int = logging.INFO,
    ) -> None:
        """Log an interaction with structured data."""
        log_entry = {
            "timestamp": datetime.now().isoformat(),
            "type": interaction_type,
            "data": data,
        }
        logging.log(level, json.dumps(log_entry))