docker / docker/docker-py

Docker logs: Distinction between stdout and stderr in correct order

Open
#3,253 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.2k
Forks
1.7k
Avg merge
13d 8h
Merged PRs (30d)
2

Description

I need to stream the logs from my docker container for logging.

Currently I do this:

stream = container.logs(stdout=True, stderr=True, stream=True)

for line in stream:
    logger.info(line.decode("utf-8"))

Now I need to differentiate between stdout and stderr. The stdout is supposed to be logged to logger.info, the stderr to logger.error.

My approach is to setup two streams, one for stdout and one for stderr and read them in parallel with different threads.

def process_stream(stream, callback):
    for line in stream:
        callback(line.decode("utf-8").strip())

stdout_stream = container.logs(stdout=True, stderr=False, stream=True, timestamps=True)
stderr_stream = container.logs(stdout=False, stderr=True, stream=True, timestamps=True)

stdout_thread = Thread(
    target=process_stream,
    args=(
        stdout_stream,
        logger.info,
    ),
)
stderr_thread = Thread(
    target=process_stream,
    args=(
        stderr_stream,
        logger.error,
    ),
)

stdout_thread.start()
stderr_thread.start()

stdout_thread.join()
stderr_thread.join()

However, messages from stdout and stderr are not always logged in correct order.

I also looked into ways to access the log files from the Docker JSON File logging driver I'm using directly but couldn't find a way.

Is there a way stream the messages in correct order plus the information if the message came from stdout or stderr?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the container.logs entry point and inspect how streamed output is exposed when stdout and stderr are both enabled. Compare this with the Docker JSON File logging driver information mentioned in the issue. Done means determining whether ordered messages can retain their stream source and documenting or exposing a supported approach; verify the result with focused log-stream tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, python
Domain
api, devops
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.