Spaces:
Sleeping
Sleeping
Falcao Zane Vijay
commited on
Commit
·
09c9920
1
Parent(s):
e6dd42c
deploy #12
Browse files- src/utils/logger.py +19 -12
src/utils/logger.py
CHANGED
@@ -4,21 +4,28 @@ import logging
|
|
4 |
import os
|
5 |
from datetime import datetime
|
6 |
|
7 |
-
def setup_logger(log_dir="logs", log_level=logging.INFO):
|
8 |
"""
|
9 |
-
Sets up a logger that writes to
|
10 |
"""
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
logging.basicConfig(
|
16 |
level=log_level,
|
17 |
format="%(asctime)s [%(levelname)s] - %(message)s",
|
18 |
-
handlers=
|
19 |
-
|
20 |
-
logging.StreamHandler()
|
21 |
-
]
|
22 |
)
|
23 |
-
logging.info("Logger initialized.")
|
24 |
-
|
|
|
4 |
import os
|
5 |
from datetime import datetime
|
6 |
|
7 |
+
def setup_logger(log_dir="logs", log_level=logging.INFO, enable_file_logging=True):
|
8 |
"""
|
9 |
+
Sets up a logger that writes to console and optionally to a timestamped log file.
|
10 |
"""
|
11 |
+
handlers = [logging.StreamHandler()] # Always log to console
|
12 |
+
|
13 |
+
if enable_file_logging:
|
14 |
+
try:
|
15 |
+
os.makedirs(log_dir, exist_ok=True)
|
16 |
+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
17 |
+
log_file = os.path.join(log_dir, f"log_{timestamp}.log")
|
18 |
+
handlers.append(logging.FileHandler(log_file))
|
19 |
+
print(f"File logging enabled: {log_file}")
|
20 |
+
except PermissionError:
|
21 |
+
print("File logging disabled due to permission issues - logging to console only")
|
22 |
+
except Exception as e:
|
23 |
+
print(f"File logging disabled due to error: {e}")
|
24 |
+
|
25 |
logging.basicConfig(
|
26 |
level=log_level,
|
27 |
format="%(asctime)s [%(levelname)s] - %(message)s",
|
28 |
+
handlers=handlers,
|
29 |
+
force=True # This ensures reconfiguration if already configured
|
|
|
|
|
30 |
)
|
31 |
+
logging.info("Logger initialized.")
|
|