Skip to content

Commit

Permalink
Add logger_name option (#30)
Browse files Browse the repository at this point in the history
* Add logger name and test.

* Run pre-commit.

* Change 'logger_name' default from 'False' to 'None'.

* Add 'logger_name' to the example.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
JoeZiminski and pre-commit-ci[bot] authored Apr 30, 2024
1 parent a3193f7 commit 712ca06
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
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

0 comments on commit 712ca06

Please sign in to comment.