From c0c9f2561f03584321f1ea0f2800505a2ff80eaa Mon Sep 17 00:00:00 2001 From: RS Nikhil Krishna Date: Thu, 30 Dec 2021 00:29:57 +0530 Subject: [PATCH] Create directory structure, if it doesn't exist, update docs --- CONTRIBUTING.rst | 2 +- docs/index.rst | 1 + logzero/__init__.py | 7 +++++++ tests/test_logzero.py | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index a703107..dfb24b1 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -101,7 +101,7 @@ Before you submit a pull request, check that it meets these guidelines: 2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst. -3. The pull request should work for Python 2.6, 2.7, 3.4, 3.5, 3.6 and for PyPy. Check +3. The pull request should work for Python 3.6, 3.7, 3.8, 3.9 and for PyPy. Check https://travis-ci.org/metachris/logzero/pull_requests and make sure that the tests pass for all supported Python versions. diff --git a/docs/index.rst b/docs/index.rst index dd8e118..adb166c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -19,6 +19,7 @@ Robust and effective logging for Python 2 and 3. * Windows color output supported by `colorama`_ * Robust against str/bytes encoding problems, works with all kinds of character encodings and special characters. * Multiple loggers can write to the same logfile (also works across multiple Python files). +* Any directory structured mentioned is automatically created, if it doesn't exist. * Global default logger with `logzero.logger <#i-logzero-logger>`_ and custom loggers with `logzero.setup_logger(..) <#i-logzero-setup-logger>`_. * Compatible with Python 2 and 3. * All contained in a `single file`_. diff --git a/logzero/__init__.py b/logzero/__init__.py index edc141f..ba40cf9 100644 --- a/logzero/__init__.py +++ b/logzero/__init__.py @@ -40,6 +40,7 @@ import logging from logzero.colors import Fore as ForegroundColors from logzero.jsonlogger import JsonFormatter +from pathlib import Path from logging.handlers import RotatingFileHandler, SysLogHandler from logging import CRITICAL, ERROR, WARNING, WARN, INFO, DEBUG, NOTSET # noqa: F401 @@ -166,6 +167,9 @@ def setup_logger(name=__name__, logfile=None, level=DEBUG, formatter=None, maxBy _logger.addHandler(stderr_stream_handler) if logfile: + # Create the folder for holding the logfile, if it doesn't already exist + Path(logfile).parent.mkdir(parents=True, exist_ok=True) + rotating_filehandler = RotatingFileHandler(filename=logfile, maxBytes=maxBytes, backupCount=backupCount) setattr(rotating_filehandler, LOGZERO_INTERNAL_LOGGER_ATTR, True) rotating_filehandler.setLevel(fileLoglevel or level) @@ -436,6 +440,9 @@ def logfile(filename, formatter=None, mode='a', maxBytes=0, backupCount=0, encod if not filename: return + # Create the folder for holding the logfile, if it doesn't already exist + Path(filename).parent.mkdir(parents=True, exist_ok=True) + # Now add rotating_filehandler = RotatingFileHandler(filename, mode=mode, maxBytes=maxBytes, backupCount=backupCount, encoding=encoding) diff --git a/tests/test_logzero.py b/tests/test_logzero.py index 3e3bf61..c055881 100644 --- a/tests/test_logzero.py +++ b/tests/test_logzero.py @@ -9,6 +9,7 @@ import os import tempfile import logging +from pathlib import Path import logzero @@ -20,6 +21,7 @@ def test_write_to_logfile_and_stderr(capsys): logzero.reset_default_logger() temp = tempfile.NamedTemporaryFile() + # Try creating a logfile try: logger = logzero.setup_logger('test_write_to_logfile_and_stderr', logfile=temp.name) logger.info("test log output") @@ -34,6 +36,22 @@ def test_write_to_logfile_and_stderr(capsys): assert content.endswith("test log output\n") finally: temp.close() + + # Try creating a logfile along with the required parent folder + try: + logger = logzero.setup_logger('test_write_to_logfile_and_stderr', logfile=Path(f"{temp.name}_folder")/Path(temp.name).name) + logger.info("test log output") + + _out, err = capsys.readouterr() + assert " test_logzero:" in err + assert err.endswith("test log output\n") + + with open(temp.name) as f: + content = f.read() + assert " test_logzero:" in content + assert content.endswith("test log output\n") + finally: + temp.close() def test_custom_formatter():