Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --nocolor and --forcecolor options #85

Merged
merged 3 commits into from Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 with output in plaintext.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/with/will

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


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 ouput will be colored using
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/ouput/output

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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