diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 113da0354..825da51bd 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -171,6 +171,8 @@ class QuteBrowser(QApplication): "(empty for no config storage)") parser.add_argument('-d', '--debug', help="Turn on debugging options.", action='store_true') + parser.add_argument('--nocolor', help="Turn off colored logging.", + action='store_false', dest='color') parser.add_argument('command', nargs='*', help="Commands to execute " "on startup.", metavar=':command') # URLs will actually be in command diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index 899bf832f..700de1d3e 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -17,6 +17,8 @@ """Loggers and utilities related to logging.""" +import os +import sys import logging from logging import getLogger from collections import deque @@ -70,16 +72,15 @@ def init_log(args): datefmt = '%H:%M:%S' if numeric_level <= logging.DEBUG: - console_fmt = (extended_fmt if ColoredFormatter is None - else extended_fmt_colored) + console_fmt = extended_fmt + console_fmt_colored = extended_fmt_colored else: - console_fmt = (simple_fmt if ColoredFormatter is None - else simple_fmt_colored) - if ColoredFormatter is None: - console_formatter = logging.Formatter(console_fmt, datefmt, '{') - else: - console_formatter = ColoredFormatter(console_fmt, datefmt, - log_colors={ + console_fmt = simple_fmt + console_fmt_colored = simple_fmt_colored + if (ColoredFormatter is not None and os.name == 'posix' and + sys.stderr.isatty() and args.color): + console_formatter = ColoredFormatter( + console_fmt_colored, datefmt, log_colors={ 'DEBUG': 'cyan', 'INFO': 'green', 'WARNING': 'yellow', @@ -87,6 +88,8 @@ def init_log(args): 'CRITICAL': 'red', } ) + else: + console_formatter = logging.Formatter(console_fmt, datefmt, '{') console_handler = logging.StreamHandler() console_handler.addFilter(logfilter) console_handler.setLevel(level)