patterndockerMinor
Colored logging messages from Python script within GitLab CI
Viewed 0 times
scriptlogginggitlabcoloredwithinmessagespythonfrom
Problem
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
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
Desired output
My
My
My Python code to create the logging messages:
.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
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 loguruMy
.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.pyMy 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!")
Solution
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
Context
StackExchange DevOps Q#15132, answer score: 7
Revisions (0)
No revisions yet.