<p>Set the <code>LOGURU_COLORIZE</code> variable in your Docker container.</p>
<pre class="lang-yaml prettyprint-override"><code>run:
    stage: run
    image: $CI_REGISTRY_IMAGE:latest
    variables:
        LOGURU_COLORIZE: &quot;true&quot;
    script:
        - ls -la
        - python3 colored_logging.py
</code></pre>

CC BY-SA 4.0
2021-12-23T23:38:01.857Z
Thank you so much!
ziggyler
CC BY-SA 4.0
2021-12-20T15:56:06.487Z
2021-12-20T15:56:06.487Z
swysocki
<p>I am using a docker container for my GitLab CI and would like to have any output/logging messages in color inside the CI. What I mean: the commands from <code>.gitlab-ci.yml</code> are in color, but the outputs of the scripts running <code>python3 colored_logging.py</code> are not.</p>
<p>It is so cumbersome to dig through bigger logs without any colors :-(</p>
<p>Here is an example project on GitLab:
<a href="https://gitlab.com/ziggyler.ziegler/non-colored-ci/-/jobs/1873125969" rel="noreferrer">https://gitlab.com/ziggyler.ziegler/non-colored-ci/-/jobs/1873125969</a></p>
<p>Does somebody know how to get colored logging messages in lines 113-117?
Any hints?</p>
<p>Thank you!</p>
<p>Actual output
<a href="https://i.sstatic.net/pQFCB.png" rel="noreferrer"><img src="https://i.sstatic.net/pQFCB.png" alt="Actual output" /></a></p>
<p>Desired output
<a href="https://i.sstatic.net/FlDnO.png" rel="noreferrer"><img src="https://i.sstatic.net/FlDnO.png" alt="Desired output" /></a></p>
<p>My <code>Dockerfile</code>:</p>
<pre><code>FROM python:3.6

ENV TERM xterm-256color

RUN apt-get update &amp;&amp; rm -rf /var/lib/apt/lists/*

RUN pip3 install loguru

</code></pre>
<p>My <code>.gitlab-ci.yml</code>:</p>
<pre><code>image: docker:19.03.12

services:
  - docker:19.03.12-dind

stages:
  - build
  - run

build:
    stage: build
    rules:
      - if: $CI_PIPELINE_SOURCE == &quot;push&quot; &amp;&amp; $CI_COMMIT_BRANCH
        changes:
          - Dockerfile
    before_script:
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    script:
        - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
        - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
        - docker push $CI_REGISTRY_IMAGE:latest


run:
    stage: run
    image: $CI_REGISTRY_IMAGE:latest
    script:
        - ls -la
        - python3 colored_logging.py
</code></pre>
<p>My Python code to create the logging messages:</p>
<pre class="lang-py prettyprint-override"><code>from loguru import logger

logger.debug(&quot;This is a debug message!&quot;)
logger.info(&quot;This is an info logging message!&quot;)
logger.warning(&quot;This is a waring and should appear in yellow color!&quot;)
logger.error(&quot;This is an error in red color!&quot;)
logger.critical(&quot;This is a very critical message!&quot;)
</code></pre>

<docker><continuous-integration><gitlab>
Colored logging messages from Python script within GitLab CI
ziggyler
# Colored logging messages from Python script within GitLab CI

(Asked by: username_0 on 2021-12-17)

I am using a docker container for my GitLab CI and would like to have any output/logging messages in color inside the CI. What I mean: the commands from .gitlab-ci.yml are in color, but the outputs of the scripts running python3 colored_logging.py are not.
It is so cumbersome to dig through bigger logs without any colors :-(
Here is an example project on GitLab:
https://gitlab.com/username_0.ziegler/non-colored-ci/-/jobs/1873125969
Does somebody know how to get colored logging messages in lines 113-117?
Any hints?
Thank you!
Actual output

Desired output

My Dockerfile:
FROM python:3.6

ENV TERM xterm-256color

RUN apt-get update && rm -rf /var/lib/apt/lists/*

RUN pip3 install loguru


My .gitlab-ci.yml:
image: docker:19.03.12

services:
  - docker:19.03.12-dind

stages:
  - build
  - run

build:
    stage: build
    rules:
      - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH
        changes:
          - Dockerfile
    before_script:
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    script:
        - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
        - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
        - docker push $CI_REGISTRY_IMAGE:latest


run:
    stage: run
    image: $CI_REGISTRY_IMAGE:latest
    script:
        - ls -la
        - python3 colored_logging.py

My Python code to create the logging messages:
from loguru import logger

logger.debug("This is a debug message!")
logger.info("This is an info logging message!")
logger.warning("This is a waring and should appear in yellow color!")
logger.error("This is an error in red color!")
logger.critical("This is a very critical message!")

**Tags:** docker, continuous-integration, gitlab


---

## 1 answers

### Answer by username_1 on 2021-12-20. Score: 7 | Accepted answer

Set the LOGURU_COLORIZE variable in your Docker container.
run:
    stage: run
    image: $CI_REGISTRY_IMAGE:latest
    variables:
        LOGURU_COLORIZE: "true"
    script:
        - ls -la
        - python3 colored_logging.py

> **Comments:**
> **username_0**: Thank you so much!  

---

