Skip to content

Commit

Permalink
Add logger name and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeZiminski committed Apr 29, 2024
1 parent a3193f7 commit a7aaa31
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
15 changes: 13 additions & 2 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=False,
):
"""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 False, 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=False,
):
"""Sets up (possibly multiprocessing aware) logging.
:param filename: Where to save the logs to
Expand All @@ -234,7 +239,11 @@ def initialise_logger(
Default: 'DEBUG'
:param log_to_console: Print logs to the console or not
"""
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 +274,7 @@ def setup_logging(
file_level="DEBUG",
multiprocessing_aware=True,
log_to_console=True,
logger_name=False,
):
"""Sets up (possibly multiprocessing aware) logging.
:param filename: Where to save the logs to
Expand All @@ -282,6 +292,7 @@ def setup_logging(
print_level=print_level,
file_level=file_level,
log_to_console=log_to_console,
logger_name=logger_name,
)
if multiprocessing_aware:
try:
Expand Down
19 changes: 19 additions & 0 deletions tests/tests/test_general.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fancylog
import logging

lateral_separator = "**************"

Expand All @@ -20,3 +21,21 @@ 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.keys()

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

assert logger_name in logging.root.manager.loggerDict.keys()


0 comments on commit a7aaa31

Please sign in to comment.