Spaces:
Sleeping
Sleeping
mattoofahad
commited on
Commit
•
e947fcb
1
Parent(s):
26a6dec
added log file
Browse files
logs.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Module doc string"""
|
2 |
+
|
3 |
+
import logging
|
4 |
+
import os
|
5 |
+
import sys
|
6 |
+
|
7 |
+
from colorama import Back, Fore, Style, init
|
8 |
+
|
9 |
+
LOGGER_LEVEL = os.getenv("LOGGER_LEVEL", "INFO")
|
10 |
+
|
11 |
+
# Initialize colorama
|
12 |
+
init(autoreset=True)
|
13 |
+
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
if not logger.hasHandlers():
|
17 |
+
logger.propagate = False
|
18 |
+
logger.setLevel(LOGGER_LEVEL)
|
19 |
+
|
20 |
+
# Define color codes for different log levels
|
21 |
+
log_colors = {
|
22 |
+
logging.DEBUG: Fore.CYAN,
|
23 |
+
logging.INFO: Fore.GREEN,
|
24 |
+
logging.WARNING: Fore.YELLOW,
|
25 |
+
logging.ERROR: Fore.RED,
|
26 |
+
logging.CRITICAL: Fore.RED + Back.WHITE + Style.BRIGHT,
|
27 |
+
}
|
28 |
+
|
29 |
+
class ColoredFormatter(logging.Formatter):
|
30 |
+
"""Module doc string"""
|
31 |
+
|
32 |
+
def format(self, record):
|
33 |
+
"""Module doc string"""
|
34 |
+
|
35 |
+
levelno = record.levelno
|
36 |
+
color = log_colors.get(levelno, "")
|
37 |
+
|
38 |
+
# Format the message
|
39 |
+
message = record.getMessage()
|
40 |
+
|
41 |
+
# Format the rest of the log details
|
42 |
+
details = self._fmt % {
|
43 |
+
"asctime": self.formatTime(record, self.datefmt),
|
44 |
+
"levelname": record.levelname,
|
45 |
+
"module": record.module,
|
46 |
+
"funcName": record.funcName,
|
47 |
+
"lineno": record.lineno,
|
48 |
+
}
|
49 |
+
|
50 |
+
# Combine details and colored message
|
51 |
+
return f"{Fore.WHITE}{details} :: {color}{message}{Style.RESET_ALL}"
|
52 |
+
|
53 |
+
normal_handler = logging.StreamHandler(sys.stdout)
|
54 |
+
normal_handler.setLevel(logging.DEBUG)
|
55 |
+
normal_handler.addFilter(lambda logRecord: logRecord.levelno < logging.WARNING)
|
56 |
+
|
57 |
+
error_handler = logging.StreamHandler(sys.stderr)
|
58 |
+
error_handler.setLevel(logging.WARNING)
|
59 |
+
|
60 |
+
formatter = ColoredFormatter(
|
61 |
+
"%(asctime)s :: %(levelname)s :: %(module)s :: %(funcName)s :: %(lineno)d"
|
62 |
+
)
|
63 |
+
|
64 |
+
normal_handler.setFormatter(formatter)
|
65 |
+
error_handler.setFormatter(formatter)
|
66 |
+
|
67 |
+
logger.addHandler(normal_handler)
|
68 |
+
logger.addHandler(error_handler)
|