This repository was archived by the owner on Jan 1, 2025. It is now read-only.
forked from ASUS-AICS/LibMultiLabel-Old-Archive
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.py
More file actions
96 lines (73 loc) · 3.11 KB
/
Copy pathlogging.py
File metadata and controls
96 lines (73 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import logging
LOG_FORMAT = '%(asctime)s %(levelname)s:%(message)s'
class ListHandler(logging.Handler):
"""Collect logged messages to a list of strings that can be obtained later.
The `logging` module does not provide this function, so we implement one.
"""
def __init__(self, level=logging.NOTSET):
super().__init__(level)
self.logs = []
def emit(self, record):
string = self.format(record)
self.logs.append(string)
def get_logs(self):
"""Return and clear all collected logs.
Returns:
list[str]: A list of formatted logs.
"""
logs = self.logs
self.logs = []
return logs
stream_handler = None
def add_stream_handler(level=logging.INFO):
"""Create and return a stream handler so that logging messages are
sent to the terminal. The stream handler is attached to the root logger.
The logging messages from the `transformer` and `pytorch_lighting`
modules are propagated to the root logger so they can be managed by
us (e.g., silence them in silent mode).
If the handler had been created, this function returns the handler
created earlier instead.
Returns:
logging.StreamHandler: The created stream handler.
"""
global stream_handler
if stream_handler:
return stream_handler
else:
logging.getLogger().setLevel(logging.NOTSET) # use handlers to control levels
try:
import transformers.utils.logging as transformer_logging
transformer_logging.disable_default_handler()
transformer_logging.enable_propagation()
except ImportError:
pass
lightning_logger = logging.getLogger('pytorch_lightning')
lightning_logger.handlers.clear()
lightning_logger.propagate = True
stream_handler = logging.StreamHandler()
stream_handler.setLevel(level)
stream_handler.setFormatter(logging.Formatter(LOG_FORMAT))
logging.getLogger().addHandler(stream_handler)
return stream_handler
collect_handler = None
def add_collect_handler(level=logging.NOTSET):
"""Create and return a ListHandler so that logging records with the attribute
`collect=True` is collected. The ListHandler is attached to the root logger.
If the handler had been created, this function returns the handler created
earlier instead.
To collect a log, set the key 'collect' with the value `True` in the `extra`
argument when logging. An example (similarly for logs of other levels):
logging.info('important message', extra={'collect': True})
Returns:
ListHandler: The created ListHandler.
"""
global collect_handler
if collect_handler:
return collect_handler
else:
logging.getLogger().setLevel(logging.NOTSET) # use handlers to control levels
collect_handler = ListHandler(level=level)
collect_handler.setFormatter(logging.Formatter(LOG_FORMAT))
collect_handler.addFilter(lambda record: record.__dict__.get('collect', False))
logging.getLogger().addHandler(collect_handler)
return collect_handler