Commit 71c87176 authored by Cyril Roelandt's avatar Cyril Roelandt
Browse files

Make "colorlog" optional.

We also bump the version requirements to >5.0.1 and fix our tests after
a colorlog commit[1] broke them.

[1] https://github.com/borntyping/python-colorlog/commit/7d1709770bb723aa2e7a056463870df31ebee6bf
parent 4d89efa9
......@@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.
Based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Changed
- colorlog is no longer a required dependency. It can be installed by using the
"color" extra.
## [0.11.1] - 2021-05-02
### Fixed
- Add missing dependency on setuptools
......
......@@ -23,7 +23,6 @@ packages = find:
install_requires =
packaging
spdx_lookup
colorlog
setuptools
[options.entry_points]
......@@ -43,6 +42,9 @@ backends =
upt-nix
upt-openbsd
color =
colorlog>5.0.1
[options.data_files]
share/man/man1 =
upt.1
......@@ -5,6 +5,7 @@ envlist=flake8,py36
deps =
coverage
.[test]
.[color]
commands =
coverage erase
coverage run --source=upt -m unittest {posargs}
......
......@@ -4,7 +4,11 @@
import logging
import sys
import colorlog
try:
import colorlog
color_available = True
except ModuleNotFoundError:
color_available = False
class MaxLevelFilter(logging.Filter):
......@@ -28,7 +32,14 @@ def create_logger(log_level, colored=False):
stderr_handler.setLevel(logging.ERROR)
logger.addHandler(stderr_handler)
logger.colored = colored
if colored and not color_available:
logger.colored = False
logger_set_formatter(logger, 'upt')
logger.warning('You requested colored logs, but the colorlog module '
'could not be found. Switching to black and white '
'logs.')
else:
logger.colored = colored
return logger
......
......@@ -52,6 +52,8 @@ class TestLog(unittest.TestCase):
'[ERROR ] [foo] error\n'
'[CRITICAL] [foo] critical\n')
@unittest.skipIf(upt.log.color_available is False,
reason='Colorlog not available')
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=StringIO)
def test_set_formatter_colored(self, m_stdout, m_stderr):
......@@ -68,7 +70,40 @@ class TestLog(unittest.TestCase):
'\x1b[33m [WARNING ] [foo] warning\x1b[0m\n')
self.assertEqual(m_stderr.getvalue(),
'\x1b[31m [ERROR ] [foo] error\x1b[0m\n'
'\x1b[01;31m [CRITICAL] [foo] critical\x1b[0m\n')
'\x1b[1;31m [CRITICAL] [foo] critical\x1b[0m\n')
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=StringIO)
def test_set_formatter_color_unavailable(self, m_stdout, m_stderr):
try:
# We want to pretend that colorlog is not installed.
# Ideally we would not mess with upt.log.color_available and would
# instead override the relevant methods from importlib, but I'm too
# lazy to figure out how to do this.
# This test should pass whether or not colorlog is actually
# installed.
color_available = upt.log.color_available
upt.log.color_available = False
logger = upt.log.create_logger(logging.DEBUG, colored=True)
upt.log.logger_set_formatter(logger, 'foo')
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
finally:
upt.log.color_available = color_available
self.assertEqual(m_stdout.getvalue(),
'[WARNING ] [upt] You requested colored logs, but '
'the colorlog module could not be found. Switching '
'to black and white logs.\n'
'[DEBUG ] [foo] debug\n'
'[INFO ] [foo] info\n'
'[WARNING ] [foo] warning\n')
self.assertEqual(m_stderr.getvalue(),
'[ERROR ] [foo] error\n'
'[CRITICAL] [foo] critical\n')
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment