Skip to content

Commit

Permalink
Add --nocolor and --forcecolor options (#85)
Browse files Browse the repository at this point in the history
Default to nocolor when not running in a tty.
  • Loading branch information
Jon Wayne Parrott committed Feb 25, 2018
1 parent 3614a4c commit f7a91e8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
17 changes: 17 additions & 0 deletions docs/usage.rst
Expand Up @@ -104,6 +104,23 @@ By default nox will continue to run all sessions even if one fails. You can use
nox --stop-on-first-error


Controlling color output
------------------------

By default, Nox will output colorful logs if you're using in an interactive
terminal. However, if you are redirecting ``stderr`` to a file or otherwise
not using an interactive terminal, nox will output in plaintext.

You can manually control Nox's output using the ``--nocolor`` and ``--forcecolor`` flags.

For example, this will always output colorful logs::

nox --forcecolor

However, this will never output colorful logs::

nox --nocolor

Windows
-------

Expand Down
38 changes: 23 additions & 15 deletions nox/logger.py
Expand Up @@ -36,25 +36,33 @@ def success(self, msg, *args, **kwargs):
logger.setLevel(logging.DEBUG)


def setup_logging(): # pragma: no cover
def setup_logging(color): # pragma: no cover
"""Setup logging.
Args:
color (bool): If true, the output will be colored using
colorlog. Otherwise, it will be plaintext.
"""
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()

formatter = ColoredFormatter(
"%(cyan)s%(name)s > %(log_color)s%(message)s",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'blue',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
'SUCCESS': 'green'
}
)

handler.setFormatter(formatter)
if color is True:
formatter = ColoredFormatter(
"%(cyan)s%(name)s > %(log_color)s%(message)s",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'blue',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
'SUCCESS': 'green'
}
)

handler.setFormatter(formatter)

root_logger.addHandler(handler)

# Silence noisy loggers
Expand Down
9 changes: 8 additions & 1 deletion nox/main.py
Expand Up @@ -75,6 +75,13 @@ def main():
parser.add_argument(
'--report', default=None,
help='Output a report of all sessions.')
parser.add_argument(
'--nocolor', default=not sys.stderr.isatty(), action='store_true',
help='Disable all color output.')
parser.add_argument(
'--forcecolor', default=False, action='store_true',
help=('Force color output, even if stdout is not an interactive '
'terminal.'))
parser.add_argument(
'posargs', nargs=argparse.REMAINDER,
help='Arguments that are passed through to the sessions.')
Expand All @@ -90,7 +97,7 @@ def main():
return

global_config = GlobalConfig(args)
setup_logging()
setup_logging(color=not args.nocolor or args.forcecolor)

# Execute the appropriate tasks.
exit_code = workflow.execute(
Expand Down

0 comments on commit f7a91e8

Please sign in to comment.