Skip to content

Commit

Permalink
Add support for FORCE_COLOR (#503)
Browse files Browse the repository at this point in the history
fixes #495
  • Loading branch information
hynek authored Apr 6, 2023
1 parent db7a294 commit e968ce1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,22 @@ jobs:
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}


colors:
name: Visual check for color settings using env variables
needs: build-sdist
runs-on: ubuntu-latest

steps:
- name: Get source code from pre-built sdist
uses: re-actors/checkout-python-sdist@release/v1
with:
source-tarball-name: ${{ env.sdist-name }}
workflow-artifact-name: ${{ env.sdist-artifact }}
- uses: actions/setup-python@v4
with:
python-version: ${{env.PYTHON_LATEST}}
- run: python -Im pip install tox wheel

- run: tox -f color
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ You can find out backwards-compatibility policy [here](https://github.com/hynek/
### Added

- `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).
This disables all heuristics whether it makes sense to use colors.
[#338](https://github.com/hynek/structlog/issues/338)


### Fixed
Expand Down
12 changes: 8 additions & 4 deletions src/structlog/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from __future__ import annotations

import os
import sys
import warnings

Expand All @@ -35,10 +36,13 @@
set_exc_info,
TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=False),
ConsoleRenderer(
colors=_has_colors
and sys.stdout is not None
and hasattr(sys.stdout, "isatty")
and sys.stdout.isatty()
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()
)
),
]
_BUILTIN_DEFAULT_CONTEXT_CLASS = cast(Type[Context], dict)
Expand Down
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ commands =
coverage report


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


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

0 comments on commit e968ce1

Please sign in to comment.