Skip to content

Commit 712ca06

Browse files
Add logger_name option (#30)
* 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>
1 parent a3193f7 commit 712ca06

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

example.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ def main(directory):
3030
variables=[args, args.paths],
3131
verbose=verbose,
3232
timestamp=True,
33+
logger_name="my_logger",
3334
)
3435

35-
logging.info("This is an info message")
36-
logging.debug("This is a debug message")
37-
logging.warning("This fun logging experience is about to end :(")
36+
logger = logging.getLogger("my_logger")
37+
38+
logger.info("This is an info message")
39+
logger.debug("This is a debug message")
40+
logger.warning("This fun logging experience is about to end :(")
3841

3942

4043
if __name__ == "__main__":

fancylog/fancylog.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def start_logging(
3737
log_to_file=True,
3838
log_to_console=True,
3939
timestamp=True,
40+
logger_name=None,
4041
):
4142
"""Prepares the log file, and then begins logging.
4243
@@ -59,8 +60,10 @@ def start_logging(
5960
Default: True
6061
:param log_to_file: If True, write a log file, otherwise just print to
6162
terminal.
62-
:param timestamp: If True, add a timestamp to the filename
6363
:param log_to_console: Print logs to the console or not: Default: True
64+
:param timestamp: If True, add a timestamp to the filename
65+
:param logger_name: If None, logger uses default logger. Otherwise,
66+
logger name is set to `logger_name`.
6467
:return: Path to the logging file#
6568
"""
6669
output_dir = str(output_dir)
@@ -98,6 +101,7 @@ def start_logging(
98101
file_level=file_log_level,
99102
multiprocessing_aware=multiprocessing_aware,
100103
log_to_console=log_to_console,
104+
logger_name=logger_name,
101105
)
102106
return logging_file
103107

@@ -225,6 +229,7 @@ def initialise_logger(
225229
print_level="INFO",
226230
file_level="DEBUG",
227231
log_to_console=True,
232+
logger_name=None,
228233
):
229234
"""Sets up (possibly multiprocessing aware) logging.
230235
:param filename: Where to save the logs to
@@ -233,8 +238,14 @@ def initialise_logger(
233238
:param file_level: What level of logging to print to file.
234239
Default: 'DEBUG'
235240
:param log_to_console: Print logs to the console or not
241+
:param logger_name: If None, logger uses default logger. Otherwise,
242+
logger name is set to `logger_name`.
236243
"""
237-
logger = logging.getLogger()
244+
if logger_name:
245+
logger = logging.getLogger(logger_name)
246+
else:
247+
logger = logging.getLogger()
248+
238249
logger.setLevel(getattr(logging, file_level))
239250

240251
formatter = logging.Formatter(
@@ -265,6 +276,7 @@ def setup_logging(
265276
file_level="DEBUG",
266277
multiprocessing_aware=True,
267278
log_to_console=True,
279+
logger_name=None,
268280
):
269281
"""Sets up (possibly multiprocessing aware) logging.
270282
:param filename: Where to save the logs to
@@ -275,13 +287,15 @@ def setup_logging(
275287
:param multiprocessing_aware: Default: True
276288
:param log_to_console: Print logs to the console or no.
277289
Default: True
278-
290+
:param logger_name: If None, logger uses default logger. Otherwise,
291+
logger name is set to `logger_name`.
279292
"""
280293
initialise_logger(
281294
filename,
282295
print_level=print_level,
283296
file_level=file_level,
284297
log_to_console=log_to_console,
298+
logger_name=logger_name,
285299
)
286300
if multiprocessing_aware:
287301
try:

tests/tests/test_general.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
import fancylog
24

35
lateral_separator = "**************"
@@ -20,3 +22,19 @@ def test_start_logging(tmp_path):
2022
assert lines[3].startswith("Output directory: ")
2123
assert lines[4].startswith("Current directory: ")
2224
assert lines[5].startswith("Version: ")
25+
26+
27+
def test_logger_name(tmp_path):
28+
"""
29+
Quick check that expecter logger name is created
30+
when specified.
31+
"""
32+
logger_name = "hello_world"
33+
34+
# Logger name should not already exist
35+
assert logger_name not in logging.root.manager.loggerDict
36+
37+
# Logger name should be created
38+
fancylog.start_logging(tmp_path, fancylog, logger_name=logger_name)
39+
40+
assert logger_name in logging.root.manager.loggerDict

0 commit comments

Comments
 (0)