Controlling colour¶
Simple colours its output with ANSI escape codes.
gouge follows the NO_COLOR convention and the
FORCE_COLOR variable used by many tools, and strips colour when the
output is not a terminal.
Precedence¶
The decision is taken when the formatter is created, in this order. The first rule that applies wins:
Setting |
Created directly |
With |
|---|---|---|
|
colour |
colour |
|
colour |
colour |
|
no colour |
no colour |
none of these |
colour |
colour on a terminal |
An environment variable counts as set when it is not empty. FORCE_COLOR
wins over NO_COLOR.
basicConfig() lets
colorama remove the codes when the
stream is not a terminal, for example when the output is piped into a file.
A formatter created directly cannot know where its output goes and emits
colour codes unless NO_COLOR is set.
Switch colour off¶
Set NO_COLOR in the environment. The output then holds no escape codes
at all, including those added by pre-formatters:
>>> import logging
>>> import os
>>> from gouge.colourcli import Simple
>>> record = logging.makeLogRecord({
... "name": "app",
... "levelno": logging.INFO,
... "levelname": "INFO",
... "msg": "Ready",
... })
>>> os.environ["NO_COLOR"] = "1"
>>> Simple().format(record)
'2026-10-27 13:34:48,899 INFO [app] Ready'
$ NO_COLOR=1 python app.py
Switch colour on¶
Set FORCE_COLOR, or pass force_styling=True, to keep the colour
codes when the output is not a terminal, for example in a CI log viewer
that renders them:
>>> formatter = Simple(force_styling=True)
>>> formatter.format(record)
'\x1b[32m2026-10-27 13:34:48,899\x1b[0m \x1b[36mINFO \x1b[0m \x1b[1m[app]\x1b[0m Ready'
force_styling also works in basicConfig()
and in configuration files.
Note
The environment is read when the formatter is created. Changing
NO_COLOR or FORCE_COLOR later has no effect on existing
formatters.
Other formatters¶
The formatters in gouge.parseable never emit colour. A
pre-formatter which adds colour codes, such as
uvicorn_access(), is covered by the rules above:
its codes are removed with all the others when colour is off.