Datasets:

ArXiv:
Elron commited on
Commit
a4b3110
1 Parent(s): 4e38750

Upload logging_utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. logging_utils.py +74 -0
logging_utils.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+ import sys
4
+ import threading
5
+ from typing import Optional
6
+
7
+ _lock = threading.Lock()
8
+ _default_handler: Optional[logging.Handler] = None
9
+
10
+ log_levels = {
11
+ "debug": logging.DEBUG,
12
+ "info": logging.INFO,
13
+ "warning": logging.WARNING,
14
+ "error": logging.ERROR,
15
+ "critical": logging.CRITICAL,
16
+ }
17
+
18
+ _default_log_level = logging.DEBUG
19
+
20
+
21
+ def _get_default_logging_level():
22
+ env_level_str = os.getenv("UNITXT_VERBOSITY", None)
23
+ if env_level_str is not None:
24
+ try:
25
+ return log_levels[env_level_str]
26
+ except KeyError as e:
27
+ raise ValueError(
28
+ f"UNITXT_VERBOSITY has to be one of: { ', '.join(log_levels.keys()) }. Got {env_level_str}."
29
+ ) from e
30
+ return _default_log_level
31
+
32
+
33
+ def _get_library_root_logger() -> logging.Logger:
34
+ return logging.getLogger(__name__.split(".")[0])
35
+
36
+
37
+ def _configure_library_root_logger() -> None:
38
+ global _default_handler
39
+
40
+ with _lock:
41
+ if _default_handler is not None:
42
+ return
43
+ _default_handler = logging.StreamHandler(sys.stdout)
44
+ if sys.stderr is None:
45
+ sys.stderr = open(os.devnull, "w")
46
+
47
+ _default_handler.flush = sys.stderr.flush
48
+
49
+ library_root_logger = _get_library_root_logger()
50
+ library_root_logger.addHandler(_default_handler)
51
+ library_root_logger.setLevel(_get_default_logging_level())
52
+
53
+ library_root_logger.propagate = False
54
+
55
+
56
+ def get_logger(name: Optional[str] = None) -> logging.Logger:
57
+ if name is None:
58
+ name = __name__.split(".")[0]
59
+
60
+ _configure_library_root_logger()
61
+ return logging.getLogger(name)
62
+
63
+
64
+ def set_verbosity(level):
65
+ _configure_library_root_logger()
66
+ _get_library_root_logger().setLevel(log_levels.get(level))
67
+
68
+
69
+ def enable_explicit_format() -> None:
70
+ for handler in _get_library_root_logger().handlers:
71
+ formatter = logging.Formatter(
72
+ "[Unitxt|%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s"
73
+ )
74
+ handler.setFormatter(formatter)