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 support for NO_COLOR #504

Merged
merged 3 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ You can find out backwards-compatibility policy [here](https://github.com/hynek/
- `structlog.stdlib.BoundLogger` now has, analogously to our native logger, a full set of async log methods prefixed with an `a`: `await log.ainfo("event!")`
[#502](https://github.com/hynek/structlog/issues/502)

- The default configuration now respects the presence of `FORCE_COLOR` (regardless of its value except empty string).
- The default configuration now respects the presence of `FORCE_COLOR` (regardless of its value, unless an empty string).
This disables all heuristics whether it makes sense to use colors.
[#338](https://github.com/hynek/structlog/issues/338)
[#503](https://github.com/hynek/structlog/issues/503)

- The default configuration now respects the presence of [`NO_COLOR`](https://no-color.org) (regardless of its value, unless an empty string).
This disables all heuristics whether it makes sense to use colors and overrides `FORCE_COLOR`.
[#504](https://github.com/hynek/structlog/issues/504)


### Fixed
Expand Down
11 changes: 11 additions & 0 deletions docs/console-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ structlog.configure(
```


## Standard Environment Variables

*structlog*'s default configuration uses colors if standard out is a TTY (i.e. an interactive session).

It's possible to override this behavior by setting two standard environment variables to any value except an empty string:

- `FORCE_COLOR` *activates* colors, regardless of where output is going.
- [`NO_COLOR`](https://no-color.org) *disables* colors, regardless of where the output is going and regardless the value of `FORCE_COLOR`.
Please note that `NO_COLOR` disables _all_ styling, including bold and italics.


## Disabling Exception Pretty-Printing

If you prefer the default terse Exception rendering, but still want *Rich* installed, you can disable the pretty-printing by instantiating {class}`structlog.dev.ConsoleRenderer()` yourself and passing `exception_formatter=structlog.dev.plain_traceback`.
15 changes: 9 additions & 6 deletions src/structlog/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@
set_exc_info,
TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=False),
ConsoleRenderer(
colors=os.environ.get("FORCE_COLOR") is not None
or (
_has_colors
and sys.stdout is not None
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty()
colors=os.environ.get("NO_COLOR", "") == ""
and (
os.environ.get("FORCE_COLOR", "") != ""
or (
_has_colors
and sys.stdout is not None
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty()
)
)
),
]
Expand Down
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ set_env = FORCE_COLOR=1
commands = python -c "import structlog; structlog.get_logger().warning('should be colorful')"


[testenv:color-no]
help = A visual check that NO_COLOR is working.
set_env = NO_COLOR=1
commands = python -c "import structlog; structlog.get_logger().warning('should be plain')"


[testenv:docset]
deps = doc2dash
extras = docs
Expand Down