Commit 97c4730d authored by Korusuke's avatar Korusuke Committed by Cyril Roelandt

Add colored output

parent 7d6898a5
......@@ -3,6 +3,10 @@ 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]
### Added
- New option: --color, to show colored logs
## [0.10.3] - 2019-07-31
### Fixed
- The "wheel" package is no longer required when not building a wheel
......
......@@ -23,6 +23,7 @@ packages = find:
install_requires =
packaging
spdx_lookup
colorlog
[options.entry_points]
console_scripts =
......
......@@ -4,6 +4,8 @@
import logging
import sys
import colorlog
class MaxLevelFilter(logging.Filter):
def __init__(self, max_level):
......@@ -14,7 +16,7 @@ class MaxLevelFilter(logging.Filter):
return record.levelno < self.max_level
def create_logger(log_level):
def create_logger(log_level, colored=False):
logger = logging.getLogger('upt')
logger.setLevel(log_level or logging.INFO)
......@@ -26,10 +28,16 @@ def create_logger(log_level):
stderr_handler.setLevel(logging.ERROR)
logger.addHandler(stderr_handler)
logger.colored = colored
return logger
def logger_set_formatter(logger, name):
formatter = logging.Formatter(f'[%(levelname)-8s] [{name}] %(message)s')
log_format = f'[%(levelname)-8s] [{name}] %(message)s'
if logger.colored:
formatter = colorlog.ColoredFormatter(f'%(log_color)s {log_format}')
else:
formatter = logging.Formatter(log_format)
for handler in logger.handlers:
handler.setFormatter(formatter)
......@@ -40,9 +40,35 @@ class TestLog(unittest.TestCase):
logger = upt.log.create_logger(logging.DEBUG)
upt.log.logger_set_formatter(logger, 'foo')
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
self.assertEqual(m_stdout.getvalue(),
'[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')
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=StringIO)
def test_set_formatter_colored(self, m_stdout, m_stderr):
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')
self.assertEqual(m_stdout.getvalue(), '[DEBUG ] [foo] debug\n')
self.assertEqual(m_stderr.getvalue(), '[ERROR ] [foo] error\n')
logger.critical('critical')
self.assertEqual(m_stdout.getvalue(),
'\x1b[37m [DEBUG ] [foo] debug\x1b[0m\n'
'\x1b[32m [INFO ] [foo] info\x1b[0m\n'
'\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')
if __name__ == '__main__':
......
......@@ -164,6 +164,12 @@ class TestParser(unittest.TestCase):
with self.assertRaises(SystemExit):
parser.parse_args(args)
def test_package_colored_output(self):
parser = upt.upt.create_parser(['pypi'], ['guix'])
args = 'package --color requests'.split()
parsed_args = parser.parse_args(args)
self.assertTrue(parsed_args.color)
class TestCommandLine(unittest.TestCase):
def setUp(self):
......
......@@ -279,6 +279,8 @@ def create_parser(frontends, backends):
help='Suppress all logging output')
parser_package.add_argument('-r', '--recursive', action='store_true',
help='Recursively package requirements'),
parser_package.add_argument('-c', '--color', action='store_true',
help='Show colored logging output'),
parser_package.add_argument('package', help='Name of the package')
return parser
......@@ -373,5 +375,5 @@ def main():
# values for us.
frontend = frontends[args.frontend]()
backend = backends[args.backend]()
upt.log.create_logger(args.log_level)
upt.log.create_logger(args.log_level, args.color)
package(args.package, frontend, backend, args.output, args.recursive)
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