Printing not being logged by Kubernetes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kubernetes does log ordinary application output, but only when the application writes to standard output or standard error in a way the container runtime can capture. If print() statements are not showing up in kubectl logs, the usual causes are buffering, writing to a file instead of stdout, looking at the wrong container, or running the application through a wrapper process that swallows output. The fix is usually in the container or application setup, not in Kubernetes itself.
What kubectl logs Actually Shows
kubectl logs reads the stdout and stderr streams of a container.
That means simple print, printf, and console.log calls should appear if they reach those streams. Kubernetes does not automatically scrape arbitrary files inside the container.
If your application writes to /var/log/app.log, kubectl logs will not show that file unless another process forwards it to stdout.
Buffering Is a Common Cause
Many languages buffer output, especially when they do not detect an interactive terminal. Inside containers, that can make print output appear late or not at all before the process exits.
For Python, unbuffered mode is a common fix.
Or start Python with unbuffered output:
The same idea applies in other languages: make sure logs are flushed promptly.
Confirm You Are Looking at the Right Container
In multi-container Pods, kubectl logs my-pod may default to the first container, not the one you care about. Always check the container name.
This is a frequent source of confusion when a sidecar and the main app run together.
Prefer a Real Logging Stream Over Ad Hoc Prints
Short debugging prints are fine, but production containers should usually log through the process’s normal logging configuration to stdout or stderr.
A simple Python example:
This scales better than scattered print statements and is easier to structure, filter, and ship into a centralized logging system.
Wrapper Scripts Can Hide Output
If the container launches a shell script that starts the real application in the background and then exits or redirects output elsewhere, Kubernetes may never see the expected logs.
A safer pattern is to let the main application run as the container’s foreground process.
That keeps the log stream tied directly to the main container process.
Check Restarts and Previous Logs
If the container crashes quickly, the missing output may actually be in the previous container instance rather than the current one. In that case, inspect restart count and use the previous-log flag.
This is especially useful when startup prints appear briefly and the process exits before you check the normal log stream.
Common Pitfalls
- Writing logs to a file inside the container and expecting
kubectl logsto show them. - Forgetting to flush buffered output.
- Looking at the wrong container in a multi-container Pod.
- Starting the app through a wrapper that redirects or detaches the real process.
- Treating
print()as a long-term logging strategy instead of using the language’s logging facilities.
Summary
- Kubernetes captures container stdout and stderr, not arbitrary log files.
- If prints are missing, check buffering and flushing first.
- Use
kubectl logs -cwhen the Pod has more than one container. - Run the main application in the foreground so Kubernetes can capture its output.
- Prefer structured logging to stdout or stderr for production workloads.
Related reading
- Private Helm repo using CDK EKS
- Problem pulling images when running private docker registry inside of Kubernetes
- Problem with dynamic persistent volume in Helm
- Problem with escaping password with special characters in Kubernetes cloudsql
- Printing the loss during TensorFlow training
- Printing thread id in log file using log4j
- Printing test execution times and pinning down slow tests with py.test
- Private Network with IPFS not working

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.