Skip to content

Commit 7a90afa

Browse files
committed
Add a force_color option
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.
1 parent fa5514d commit 7a90afa

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

colorlog/formatter.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(
6767
validate: bool = True,
6868
stream: typing.Optional[typing.IO] = None,
6969
no_color: bool = False,
70+
force_color: bool = False,
7071
) -> None:
7172
"""
7273
Set the format and colors the ColoredFormatter will use.
@@ -95,6 +96,10 @@ def __init__(
9596
- stream (typing.IO)
9697
The stream formatted messages will be printed to. Used to toggle colour
9798
on non-TTY outputs. Optional.
99+
- no_color (bool):
100+
Disable color output.
101+
- force_color (bool):
102+
Enable color output. Takes precedence over `no_color`.
98103
"""
99104

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

108113
self.log_colors = log_colors if log_colors is not None else default_log_colors
109-
self.secondary_log_colors = (
110-
secondary_log_colors if secondary_log_colors is not None else {}
111-
)
114+
self.secondary_log_colors = secondary_log_colors if secondary_log_colors is not None else {}
112115
self.reset = reset
113116
self.stream = stream
114117
self.no_color = no_color
118+
self.force_color = force_color
115119

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

138142
def _blank_escape_codes(self):
139143
"""Return True if we should be prevented from printing escape codes."""
140-
if self.no_color:
141-
return True
144+
if self.force_color or "FORCE_COLOR" in os.environ:
145+
return False
142146

143-
if "NO_COLOR" in os.environ:
147+
if self.no_color or "NO_COLOR" in os.environ:
144148
return True
145149

146150
if self.stream is not None and not self.stream.isatty():
@@ -191,9 +195,7 @@ def __init__(self, fmt: typing.Mapping[str, str], **kwargs: typing.Any) -> None:
191195
}
192196
)
193197
"""
194-
self.formatters = {
195-
level: ColoredFormatter(fmt=f, **kwargs) for level, f in fmt.items()
196-
}
198+
self.formatters = {level: ColoredFormatter(fmt=f, **kwargs) for level, f in fmt.items()}
197199

198200
def format(self, record: logging.LogRecord) -> str:
199201
return self.formatters[record.levelname].format(record)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="colorlog",
5-
version="6.5.0",
5+
version="6.6.0",
66
description="Add colours to the output of Python's logging module.",
77
long_description=open("README.md").read(),
88
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)