File: print-container-logs.rst

package info (click to toggle)
python-pykube-ng 22.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: python: 2,336; makefile: 44
file content (27 lines) | stat: -rw-r--r-- 752 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
===========================
How to print container logs
===========================

To print the most recent logs for multiple pods and containers, you can use the ``timestamps`` parameter and sort log lines afterwards.
Note that this will not work correctly if your container logs contain newlines!

.. code-block:: python

    tail_lines = 100

    logs = []

    for pod in pods:
        for container in pod.obj["spec"]["containers"]:
            container_log = pod.logs(
                container=container["name"],
                timestamps=True,
                tail_lines=tail_lines,
            )
            for line in container_log.split("\n"):
                logs.append(line)

    logs.sort()

    for log in logs:
        print(log)