6

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/ziggyler.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 Actual output

Desired 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!")

1 Answer 1

7

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
1
  • 1
    Thank you so much!
    – ziggyler
    Commented Dec 23, 2021 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.