fix log
Browse files- app/core/logger.py +9 -19
app/core/logger.py
CHANGED
@@ -28,8 +28,8 @@ class Logger:
|
|
28 |
self.log_level = log_level.upper()
|
29 |
self.environment = os.getenv("ENVIRONMENT", "PROD").upper()
|
30 |
|
31 |
-
# Skip file logging in Hugging Face environment
|
32 |
-
if self.environment
|
33 |
self.log_file_path = None
|
34 |
else:
|
35 |
self.log_file_path = os.getenv("LOG_FILE_PATH", self._get_default_log_file_path())
|
@@ -39,14 +39,9 @@ class Logger:
|
|
39 |
if self.environment == "DEV":
|
40 |
# For development, use local logs directory
|
41 |
log_dir = Path("logs")
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
# Create logs directory if it doesn't exist
|
47 |
-
log_dir.mkdir(parents=True, exist_ok=True)
|
48 |
-
|
49 |
-
return str(log_dir / "app.log")
|
50 |
|
51 |
def _rename_event_key(self, _, __, event_dict: EventDict) -> EventDict:
|
52 |
"""
|
@@ -112,12 +107,7 @@ class Logger:
|
|
112 |
)
|
113 |
|
114 |
def _configure_logging(self, processors: list[Processor]) -> logging.Logger:
|
115 |
-
"""
|
116 |
-
Configures logging with the specified processors based on the environment.
|
117 |
-
|
118 |
-
Returns:
|
119 |
-
logging.Logger: The configured root logger.
|
120 |
-
"""
|
121 |
formatter = structlog.stdlib.ProcessorFormatter(
|
122 |
foreign_pre_chain=processors,
|
123 |
processors=[
|
@@ -136,8 +126,8 @@ class Logger:
|
|
136 |
stream_handler.setFormatter(formatter)
|
137 |
root_logger.addHandler(stream_handler)
|
138 |
|
139 |
-
# Only add file logging if
|
140 |
-
if self.environment
|
141 |
try:
|
142 |
file_handler = TimedRotatingFileHandler(
|
143 |
filename=self.log_file_path,
|
@@ -149,7 +139,7 @@ class Logger:
|
|
149 |
file_handler.setFormatter(formatter)
|
150 |
root_logger.addHandler(file_handler)
|
151 |
except PermissionError:
|
152 |
-
# If file logging fails,
|
153 |
pass
|
154 |
|
155 |
root_logger.setLevel(self.log_level)
|
|
|
28 |
self.log_level = log_level.upper()
|
29 |
self.environment = os.getenv("ENVIRONMENT", "PROD").upper()
|
30 |
|
31 |
+
# Skip file logging in production/Hugging Face environment
|
32 |
+
if self.environment in ["PROD", "HUGGINGFACE"]:
|
33 |
self.log_file_path = None
|
34 |
else:
|
35 |
self.log_file_path = os.getenv("LOG_FILE_PATH", self._get_default_log_file_path())
|
|
|
39 |
if self.environment == "DEV":
|
40 |
# For development, use local logs directory
|
41 |
log_dir = Path("logs")
|
42 |
+
log_dir.mkdir(parents=True, exist_ok=True)
|
43 |
+
return str(log_dir / "app.log")
|
44 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
def _rename_event_key(self, _, __, event_dict: EventDict) -> EventDict:
|
47 |
"""
|
|
|
107 |
)
|
108 |
|
109 |
def _configure_logging(self, processors: list[Processor]) -> logging.Logger:
|
110 |
+
"""Configures logging with the specified processors."""
|
|
|
|
|
|
|
|
|
|
|
111 |
formatter = structlog.stdlib.ProcessorFormatter(
|
112 |
foreign_pre_chain=processors,
|
113 |
processors=[
|
|
|
126 |
stream_handler.setFormatter(formatter)
|
127 |
root_logger.addHandler(stream_handler)
|
128 |
|
129 |
+
# Only add file logging if in development and log_file_path is set
|
130 |
+
if self.environment == "DEV" and self.log_file_path:
|
131 |
try:
|
132 |
file_handler = TimedRotatingFileHandler(
|
133 |
filename=self.log_file_path,
|
|
|
139 |
file_handler.setFormatter(formatter)
|
140 |
root_logger.addHandler(file_handler)
|
141 |
except PermissionError:
|
142 |
+
# If file logging fails, continue with console logging only
|
143 |
pass
|
144 |
|
145 |
root_logger.setLevel(self.log_level)
|