Spaces:
Runtime error
Runtime error
Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
import logging.handlers
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
|
6 |
+
handler = None
|
7 |
+
|
8 |
+
|
9 |
+
def build_logger(logger_name, logger_dir):
|
10 |
+
global handler
|
11 |
+
|
12 |
+
formatter = logging.Formatter(
|
13 |
+
fmt="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
14 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
15 |
+
)
|
16 |
+
|
17 |
+
# Set the format of root handlers
|
18 |
+
if not logging.getLogger().handlers:
|
19 |
+
logging.basicConfig(level=logging.INFO)
|
20 |
+
logging.getLogger().handlers[0].setFormatter(formatter)
|
21 |
+
|
22 |
+
# Redirect stdout and stderr to loggers
|
23 |
+
stdout_logger = logging.getLogger("stdout")
|
24 |
+
stdout_logger.setLevel(logging.INFO)
|
25 |
+
sl = StreamToLogger(stdout_logger, logging.INFO)
|
26 |
+
sys.stdout = sl
|
27 |
+
|
28 |
+
stderr_logger = logging.getLogger("stderr")
|
29 |
+
stderr_logger.setLevel(logging.ERROR)
|
30 |
+
sl = StreamToLogger(stderr_logger, logging.ERROR)
|
31 |
+
sys.stderr = sl
|
32 |
+
|
33 |
+
# Get logger
|
34 |
+
logger = logging.getLogger(logger_name)
|
35 |
+
logger.setLevel(logging.INFO)
|
36 |
+
|
37 |
+
# Add a file handler for all loggers
|
38 |
+
if handler is None:
|
39 |
+
os.makedirs(logger_dir, exist_ok=True)
|
40 |
+
filename = os.path.join(logger_dir, logger_name + ".log")
|
41 |
+
handler = logging.handlers.TimedRotatingFileHandler(filename, when="D", utc=True)
|
42 |
+
handler.setFormatter(formatter)
|
43 |
+
|
44 |
+
for name, item in logging.root.manager.loggerDict.items():
|
45 |
+
if isinstance(item, logging.Logger):
|
46 |
+
item.addHandler(handler)
|
47 |
+
|
48 |
+
return logger
|
49 |
+
|
50 |
+
|
51 |
+
class StreamToLogger(object):
|
52 |
+
"""
|
53 |
+
Fake file-like stream object that redirects writes to a logger instance.
|
54 |
+
"""
|
55 |
+
|
56 |
+
def __init__(self, logger, log_level=logging.INFO):
|
57 |
+
self.terminal = sys.stdout
|
58 |
+
self.logger = logger
|
59 |
+
self.log_level = log_level
|
60 |
+
self.linebuf = ""
|
61 |
+
|
62 |
+
def __getattr__(self, attr):
|
63 |
+
return getattr(self.terminal, attr)
|
64 |
+
|
65 |
+
def write(self, buf):
|
66 |
+
temp_linebuf = self.linebuf + buf
|
67 |
+
self.linebuf = ""
|
68 |
+
for line in temp_linebuf.splitlines(True):
|
69 |
+
# From the io.TextIOWrapper docs:
|
70 |
+
# On output, if newline is None, any '\n' characters written
|
71 |
+
# are translated to the system default line separator.
|
72 |
+
# By default sys.stdout.write() expects '\n' newlines and then
|
73 |
+
# translates them so this is still cross platform.
|
74 |
+
if line[-1] == "\n":
|
75 |
+
self.logger.log(self.log_level, line.rstrip())
|
76 |
+
else:
|
77 |
+
self.linebuf += line
|
78 |
+
|
79 |
+
def flush(self):
|
80 |
+
if self.linebuf != "":
|
81 |
+
self.logger.log(self.log_level, self.linebuf.rstrip())
|
82 |
+
self.linebuf = ""
|