ishanjmukherjee commited on
Commit
6bb0baf
·
1 Parent(s): 77f656a

Rich logging from vortex

Browse files
Files changed (1) hide show
  1. rich_logging.py +59 -0
rich_logging.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copied verbatim from vortex
2
+ import logging
3
+ from rich.logging import RichHandler
4
+
5
+ LOGGING_FORMAT = "%(name)s - %(levelname)s - %(message)s"
6
+
7
+
8
+ def maybe_initialize_root_logger():
9
+ """
10
+ Note: this is no-op if someone already called basicConfig() before.
11
+ """
12
+ logging.basicConfig(
13
+ level=logging.INFO,
14
+ handlers=[RichHandler(rich_tracebacks=True)],
15
+ format=LOGGING_FORMAT,
16
+ )
17
+
18
+
19
+ maybe_initialize_root_logger()
20
+
21
+ activations_file_handler = logging.FileHandler("activations_debug.log")
22
+ activations_file_handler.setFormatter(logging.Formatter(LOGGING_FORMAT))
23
+
24
+
25
+ def initialize_activations_logger():
26
+ """
27
+ Activations logger is available to call any time, but by default
28
+ actual activation prints are disabled (i.e. only errors are printed.)
29
+
30
+ To enable activations, call `enable_activations_logging()`.
31
+ """
32
+
33
+ # Essentially "turned off" by default as activations use info/debug levels.
34
+ # We still allow errors via activation logger, though.
35
+ level = logging.ERROR
36
+
37
+ logger = logging.getLogger("activations_logger")
38
+ logger.setLevel(level)
39
+
40
+ if activations_file_handler not in logger.handlers:
41
+ # Attach to root logger to make sure file captures all logs, not only
42
+ # activations. Easier to correlate and debug.
43
+ logging.getLogger().addHandler(activations_file_handler)
44
+ # By default, make the file only save errors. This reduces file size
45
+ # growth when activations debugging is disabled.
46
+ activations_file_handler.setLevel(level)
47
+ return logger
48
+
49
+
50
+ activations_logger = initialize_activations_logger()
51
+
52
+
53
+ def enable_activations_logging():
54
+ # This enables [up to] debug-level printouts to the console.
55
+ activations_logger.setLevel(logging.DEBUG)
56
+ # And to the file, too. This is needed since the file is attached to the
57
+ # root logger, not to activations_logger. (And attaching to root logger was
58
+ # needed to capture all logs into the activations file.)
59
+ activations_file_handler.setLevel(logging.DEBUG)