Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_multiple_loggers_one_logfile():
"""
Should properly log bytes
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logger1 = logzero.setup_logger(name="logger1", logfile=temp.name)
logger2 = logzero.setup_logger(name="logger2", logfile=temp.name)
logger3 = logzero.setup_logger(name="logger3", logfile=temp.name)
logger1.info("logger1")
logger2.info("logger2")
logger3.info("logger3")
with open(temp.name) as f:
content = f.read().strip()
assert "logger1" in content
assert "logger2" in content
assert "logger3" in content
assert len(content.split("\n")) == 3
def test_default_logger(disableStdErrorLogger=False):
"""
Default logger should work and be able to be reconfigured.
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logzero.setup_default_logger(logfile=temp.name, disableStderrLogger=disableStdErrorLogger)
logzero.logger.debug("debug1") # will be logged
# Reconfigure with loglevel INFO
logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, disableStderrLogger=disableStdErrorLogger)
logzero.logger.debug("debug2") # will not be logged
logzero.logger.info("info1") # will be logged
# Reconfigure with a different formatter
log_format = '%(color)s[xxx]%(end_color)s %(message)s'
formatter = logzero.LogFormatter(fmt=log_format)
logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, formatter=formatter, disableStderrLogger=disableStdErrorLogger)
logzero.logger.info("info2") # will be logged with new formatter
def test_setup_logger_logfile_custom_loglevel(capsys):
"""
setup_logger(..) with filelogger and custom loglevel
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logger = logzero.setup_logger(logfile=temp.name, fileLoglevel=logging.WARN)
logger.info("info1")
logger.warn("warn1")
with open(temp.name) as f:
content = f.read()
assert "] info1" not in content
assert "] warn1" in content
finally:
temp.close()
def test_api_loglevel(capsys):
"""
Should reconfigure the internal logger loglevel
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logzero.logfile(temp.name)
logzero.logger.info("info1")
logzero.loglevel(logging.WARN)
logzero.logger.info("info2")
logzero.logger.warn("warn1")
with open(temp.name) as f:
content = f.read()
assert "] info1" in content
assert "] info2" not in content
assert "] warn1" in content
finally:
temp.close()
def test_custom_formatter():
"""
Should work with a custom formatter.
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
log_format = '%(color)s[%(levelname)1.1s %(asctime)s customnametest:%(lineno)d]%(end_color)s %(message)s'
formatter = logzero.LogFormatter(fmt=log_format)
logger = logzero.setup_logger(logfile=temp.name, formatter=formatter)
logger.info("test log output")
with open(temp.name) as f:
content = f.read()
assert " customnametest:" in content
assert content.endswith("test log output\n")
finally:
temp.close()
def test_write_to_logfile_and_stderr(capsys):
"""
Should log to a file.
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logger = logzero.setup_logger(logfile=temp.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_loglevel():
"""
Should not log any debug messages if minimum level is set to INFO
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logger = logzero.setup_logger(logfile=temp.name, level=logging.INFO)
logger.debug("test log output")
with open(temp.name) as f:
content = f.read()
assert len(content.strip()) == 0
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 with logzero.loglevel(..) it will not overwrite
# 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
def test_bytes():
"""
Should properly log bytes
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logger = logzero.setup_logger(logfile=temp.name)
testbytes = os.urandom(20)
logger.debug(testbytes)
logger.debug(None)
# with open(temp.name) as f:
# content = f.read()
# # assert str(testbytes) in content
finally:
temp.close()