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

Add logger_name option #30

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ def main(directory):
variables=[args, args.paths],
verbose=verbose,
timestamp=True,
logger_name="my_logger",
)

logging.info("This is an info message")
logging.debug("This is a debug message")
logging.warning("This fun logging experience is about to end :(")
logger = logging.getLogger("my_logger")

logger.info("This is an info message")
logger.debug("This is a debug message")
logger.warning("This fun logging experience is about to end :(")


if __name__ == "__main__":
Expand Down
20 changes: 17 additions & 3 deletions fancylog/fancylog.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def start_logging(
log_to_file=True,
log_to_console=True,
timestamp=True,
logger_name=None,
):
"""Prepares the log file, and then begins logging.

Expand All @@ -59,8 +60,10 @@ def start_logging(
Default: True
:param log_to_file: If True, write a log file, otherwise just print to
terminal.
:param timestamp: If True, add a timestamp to the filename
:param log_to_console: Print logs to the console or not: Default: True
:param timestamp: If True, add a timestamp to the filename
:param logger_name: If None, logger uses default logger. Otherwise,
logger name is set to `logger_name`.
:return: Path to the logging file#
"""
output_dir = str(output_dir)
Expand Down Expand Up @@ -98,6 +101,7 @@ def start_logging(
file_level=file_log_level,
multiprocessing_aware=multiprocessing_aware,
log_to_console=log_to_console,
logger_name=logger_name,
)
return logging_file

Expand Down Expand Up @@ -225,6 +229,7 @@ def initialise_logger(
print_level="INFO",
file_level="DEBUG",
log_to_console=True,
logger_name=None,
):
"""Sets up (possibly multiprocessing aware) logging.
:param filename: Where to save the logs to
Expand All @@ -233,8 +238,14 @@ def initialise_logger(
:param file_level: What level of logging to print to file.
Default: 'DEBUG'
:param log_to_console: Print logs to the console or not
:param logger_name: If None, logger uses default logger. Otherwise,
logger name is set to `logger_name`.
"""
logger = logging.getLogger()
if logger_name:
logger = logging.getLogger(logger_name)
else:
logger = logging.getLogger()

logger.setLevel(getattr(logging, file_level))

formatter = logging.Formatter(
Expand Down Expand Up @@ -265,6 +276,7 @@ def setup_logging(
file_level="DEBUG",
multiprocessing_aware=True,
log_to_console=True,
logger_name=None,
):
"""Sets up (possibly multiprocessing aware) logging.
:param filename: Where to save the logs to
Expand All @@ -275,13 +287,15 @@ def setup_logging(
:param multiprocessing_aware: Default: True
:param log_to_console: Print logs to the console or no.
Default: True

:param logger_name: If None, logger uses default logger. Otherwise,
logger name is set to `logger_name`.
"""
initialise_logger(
filename,
print_level=print_level,
file_level=file_level,
log_to_console=log_to_console,
logger_name=logger_name,
)
if multiprocessing_aware:
try:
Expand Down
18 changes: 18 additions & 0 deletions tests/tests/test_general.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import fancylog

lateral_separator = "**************"
Expand All @@ -20,3 +22,19 @@ def test_start_logging(tmp_path):
assert lines[3].startswith("Output directory: ")
assert lines[4].startswith("Current directory: ")
assert lines[5].startswith("Version: ")


def test_logger_name(tmp_path):
"""
Quick check that expecter logger name is created
when specified.
"""
logger_name = "hello_world"

# Logger name should not already exist
assert logger_name not in logging.root.manager.loggerDict

# Logger name should be created
fancylog.start_logging(tmp_path, fancylog, logger_name=logger_name)

assert logger_name in logging.root.manager.loggerDict
Loading