forked from stitionai/devika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
65 lines (49 loc) · 1.87 KB
/
logger.py
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
from functools import wraps
from fastlogging import LogInit
from flask import request
from src.config import Config
class Logger:
def __init__(self):
config = Config()
logs_dir = config.get_logs_dir()
self.logger = LogInit(pathName=logs_dir + "/devika_agent.log", console=True, colors=True)
def read_log_file(self) -> str:
with open(self.logger.pathName, "r") as file:
return file.read()
def info(self, message: str):
self.logger.info(message)
self.logger.flush()
def error(self, message: str):
self.logger.error(message)
self.logger.flush()
def warning(self, message: str):
self.logger.warning(message)
self.logger.flush()
def debug(self, message: str):
self.logger.debug(message)
self.logger.flush()
def exception(self, message: str):
self.logger.exception(message)
self.logger.flush()
def route_logger(logger: Logger):
"""
Decorator factory that creates a decorator to log route entry and exit points.
The decorator uses the provided logger to log the information.
:param logger: The logger instance to use for logging.
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
# Log entry point
logger.info(f"{request.path} {request.method}")
# Call the actual route function
response = func(*args, **kwargs)
# Log exit point, including response summary if possible
try:
response_summary = response.get_data(as_text=True)
logger.debug(f"{request.path} {request.method} - Response: {response_summary}")
except Exception as e:
logger.exception(f"{request.path} {request.method} - {e})")
return response
return wrapper
return decorator