Skip to content

Commit 09518b8

Browse files
rickwierengaCopilotCopilot
authored
easy way to make logger verbose (#768)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
1 parent 86bbc46 commit 09518b8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

pylabrobot/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import logging
3+
import sys
34
from pathlib import Path
45
from typing import Optional, Union
56

@@ -64,3 +65,21 @@ def configure(cfg: Config):
6465

6566

6667
configure(CONFIG)
68+
69+
70+
def verbose(make_verbose: bool, level: int = logging.INFO) -> None:
71+
"""Add a StreamHandler to the pylabrobot logger to make logging output visible to the console.
72+
If set to False, remove the console StreamHandler. This only removes StreamHandlers that output
73+
to sys.stdout or sys.stderr, and will not affect FileHandlers or other subclasses.
74+
"""
75+
logger = logging.getLogger("pylabrobot")
76+
for handler in logger.handlers[:]: # iterate over a copy to allow safe removal
77+
if isinstance(handler, logging.StreamHandler) and not isinstance(handler, logging.FileHandler):
78+
if handler.stream in (sys.stdout, sys.stderr):
79+
logger.removeHandler(handler)
80+
if make_verbose:
81+
logger.setLevel(level)
82+
handler = logging.StreamHandler()
83+
handler.setLevel(level)
84+
handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
85+
logger.addHandler(handler)

0 commit comments

Comments
 (0)