Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Create directory structure, if it doesn't exist #432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`_.
Expand Down
7 changes: 7 additions & 0 deletions logzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_logzero.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import tempfile
import logging
from pathlib import Path

import logzero

Expand All @@ -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")
Expand All @@ -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():
Expand Down