MCPcopy Create free account
hub / github.com/Topdu/OpenOCR / get_logger

Function get_logger

tools/utils/logging.py:10–54  ·  view source on GitHub ↗

Initialize and get a logger by name. If the logger has not been initialized, this method will initialize the logger by adding one or two handlers, otherwise the initialized logger will be directly returned. During initialization, a StreamHandler will always be added. If `log_file` is

(name="openrec", log_file=None, log_level=logging.DEBUG)

Source from the content-addressed store, hash-verified

8
9@functools.lru_cache()
10def get_logger(name="openrec", log_file=None, log_level=logging.DEBUG):
11 """Initialize and get a logger by name.
12 If the logger has not been initialized, this method will initialize the
13 logger by adding one or two handlers, otherwise the initialized logger will
14 be directly returned. During initialization, a StreamHandler will always be
15 added. If `log_file` is specified a FileHandler will also be added.
16 Args:
17 name (str): Logger name.
18 log_file (str | None): The log filename. If specified, a FileHandler
19 will be added to the logger.
20 log_level (int): The logger level. Note that only the process of
21 rank 0 is affected, and other processes will set the level to
22 "Error" thus be silent most of the time.
23 Returns:
24 logging.Logger: The expected logger.
25 """
26 logger = logging.getLogger(name)
27 if name in logger_initialized:
28 return logger
29 for logger_name in logger_initialized:
30 if name.startswith(logger_name):
31 return logger
32
33 formatter = logging.Formatter(
34 "[%(asctime)s] %(name)s %(levelname)s: %(message)s",
35 datefmt="%Y/%m/%d %H:%M:%S")
36
37 stream_handler = logging.StreamHandler(stream=sys.stdout)
38 stream_handler.setFormatter(formatter)
39 logger.addHandler(stream_handler)
40
41 rank = int(os.environ["LOCAL_RANK"]) if "LOCAL_RANK" in os.environ else 0
42 if log_file is not None and rank == 0:
43 log_file_folder = os.path.split(log_file)[0]
44 os.makedirs(log_file_folder, exist_ok=True)
45 file_handler = logging.FileHandler(log_file, "a")
46 file_handler.setFormatter(formatter)
47 logger.addHandler(file_handler)
48 if rank == 0:
49 logger.setLevel(log_level)
50 else:
51 logger.setLevel(logging.ERROR)
52 logger_initialized[name] = True
53 logger.propagate = False
54 return logger

Callers 15

demo_opendoc.pyFile · 0.90
openocr.pyFile · 0.90
test_openocr.pyFile · 0.90
infer_det.pyFile · 0.90
infer_doc_onnx.pyFile · 0.90
infer_doc.pyFile · 0.90
mainFunction · 0.90
mainFunction · 0.90
infer_e2e.pyFile · 0.90
infer_rec.pyFile · 0.90
save_ckptFunction · 0.90
load_ckptFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected