Skip to content

Commit

Permalink
Add a force_color option
Browse files Browse the repository at this point in the history
This should provide a workaround for #112, where calling basicConfig
with stream=sys.stdout forced color detection though istty, with no
way to disable it.
  • Loading branch information
borntyping committed Nov 8, 2021
1 parent fa5514d commit 7a90afa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions colorlog/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(
validate: bool = True,
stream: typing.Optional[typing.IO] = None,
no_color: bool = False,
force_color: bool = False,
) -> None:
"""
Set the format and colors the ColoredFormatter will use.
Expand Down Expand Up @@ -95,6 +96,10 @@ def __init__(
- stream (typing.IO)
The stream formatted messages will be printed to. Used to toggle colour
on non-TTY outputs. Optional.
- no_color (bool):
Disable color output.
- force_color (bool):
Enable color output. Takes precedence over `no_color`.
"""

# Select a default format if `fmt` is not provided.
Expand All @@ -106,12 +111,11 @@ def __init__(
super().__init__(fmt, datefmt, style)

self.log_colors = log_colors if log_colors is not None else default_log_colors
self.secondary_log_colors = (
secondary_log_colors if secondary_log_colors is not None else {}
)
self.secondary_log_colors = secondary_log_colors if secondary_log_colors is not None else {}
self.reset = reset
self.stream = stream
self.no_color = no_color
self.force_color = force_color

def formatMessage(self, record: logging.LogRecord) -> str:
"""Format a message from a record object."""
Expand All @@ -137,10 +141,10 @@ def _escape_code_map(self, item: str) -> EscapeCodes:

def _blank_escape_codes(self):
"""Return True if we should be prevented from printing escape codes."""
if self.no_color:
return True
if self.force_color or "FORCE_COLOR" in os.environ:
return False

if "NO_COLOR" in os.environ:
if self.no_color or "NO_COLOR" in os.environ:
return True

if self.stream is not None and not self.stream.isatty():
Expand Down Expand Up @@ -191,9 +195,7 @@ def __init__(self, fmt: typing.Mapping[str, str], **kwargs: typing.Any) -> None:
}
)
"""
self.formatters = {
level: ColoredFormatter(fmt=f, **kwargs) for level, f in fmt.items()
}
self.formatters = {level: ColoredFormatter(fmt=f, **kwargs) for level, f in fmt.items()}

def format(self, record: logging.LogRecord) -> str:
return self.formatters[record.levelname].format(record)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="colorlog",
version="6.5.0",
version="6.6.0",
description="Add colours to the output of Python's logging module.",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 7a90afa

Please sign in to comment.