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

Commit

Permalink
custom loglevel for logfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hager committed Jul 7, 2017
1 parent 7f41746 commit c5cb7fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ Future Features & Ideas
TODO
----

* Tests:
* Tests

* Custom handlers and reconfiguration
* Weird: py.test with default logger - capturing err does not work if the logger is setup initially in logzero. Only works when setup from the py script.
* Strange behaviour: py.test with default logger - capturing err does not work if the logger is setup initially in logzero. Only works when setup from the py script.


Related Projects
Expand Down
5 changes: 3 additions & 2 deletions logzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def formatter(formatter, update_custom_handlers=False):
_formatter = formatter


def logfile(filename, formatter=None, mode='a', maxBytes=0, backupCount=0, encoding=None):
def logfile(filename, formatter=None, mode='a', maxBytes=0, backupCount=0, encoding=None, loglevel=None):
"""
Setup logging to file with a RotatingFileHandler.
Expand All @@ -385,6 +385,7 @@ def logfile(filename, formatter=None, mode='a', maxBytes=0, backupCount=0, encod
:arg int maxBytes: Size of the logfile when rollover should occur. Defaults to 0, rollover never occurs.
:arg int backupCount: Number of backups to keep. Defaults to 0, rollover never occurs.
:arg string encoding: Used to open the file with that encoding.
:arg int loglevel: Set a custom loglevel for the file logger, else uses the currently set. This will be overwritten if you call `logzero.loglevel(..)`.
"""
# Step 1: If an internal RotatingFileHandler already exists, remove it
for handler in list(logger.handlers):
Expand All @@ -396,7 +397,7 @@ def logfile(filename, formatter=None, mode='a', maxBytes=0, backupCount=0, encod
if filename:
rotating_filehandler = RotatingFileHandler(filename, mode=mode, maxBytes=maxBytes, backupCount=backupCount, encoding=encoding)
setattr(rotating_filehandler, LOGZERO_INTERNAL_LOGGER_ATTR, True)
rotating_filehandler.setLevel(_loglevel)
rotating_filehandler.setLevel(loglevel or _loglevel)
rotating_filehandler.setFormatter(formatter or _formatter or LogFormatter(color=False))
logger.addHandler(rotating_filehandler)

Expand Down
29 changes: 29 additions & 0 deletions tests/test_new_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,32 @@ def test_api_rotating_logfile(capsys):

finally:
temp.close()


def test_api_logfile_custom_loglevel():
"""
logzero.logfile(..) should be able to use a custom loglevel
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
# Set logfile with custom loglevel
logzero.logfile(temp.name, loglevel=logging.WARN)
logzero.logger.info("info1")
logzero.logger.warn("warn1")

# If setting a loglevel it will currently overrite the custom loglevel
# of the file handler
logzero.loglevel(logging.INFO)
logzero.logger.info("info2")
logzero.logger.warn("warn2")

with open(temp.name) as f:
content = f.read()
assert "] info1" not in content
assert "] warn1" in content
assert "] info2" in content
assert "] warn2" in content

finally:
temp.close()

0 comments on commit c5cb7fb

Please sign in to comment.