Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support providing custom stream to the logger #65

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/hpotk/util/_log.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging
import typing

DEFAULT_LOG_FMT = '%(asctime)s %(name)-20s %(levelname)-3s : %(message)s'


def setup_logging(level: int = logging.INFO,
log_fmt: str = DEFAULT_LOG_FMT):
def setup_logging(
level: int = logging.INFO,
log_fmt: str = DEFAULT_LOG_FMT,
stream: typing.Optional[typing.TextIO] = None,
):
"""
Create a basic configuration for the logging library. Set up console and file handler using provided `log_fmt`.

Expand All @@ -19,12 +23,13 @@ def setup_logging(level: int = logging.INFO,

:param level: the verbosity to use, `INFO` by default.
:param log_fmt: format string for logging.
:param stream: stream to write to. Will default to `sys.stderr` if `None`.
"""
# create logger
logger = logging.getLogger()
logger.setLevel(level)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch = logging.StreamHandler(stream=stream)
ch.setLevel(level)
# create formatter
formatter = logging.Formatter(log_fmt)
Expand Down
Loading