Hello and welcome back! In the previous lesson, we successfully deployed our Login App and visualized its logs in Kibana. However, we noticed that the logs generated by our application were not very informative. Today, we will improve our logging mechanism to produce more structured and insightful logs.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Reviewing the Original Logging Implementation
Let’s start by examining how logs are generated in our existing application. In the Python web application’s repository, open the Dockerfile to observe that the entire working directory is being copied. Then, navigate to the application’s entry point by opening the app.py file. In app.py, we import Python’s built-in logging module to send structured logs. Below is the original logging configuration:logger.info is used to send logs, which then appear in the Kubernetes pod logs.
Improving the Logging Structure with a Custom JSON Formatter
To deliver more structured and insightful logs, we updated our application in a file named update_app.py. In this updated version, we continue to use the logging module and introduce a custom JSON formatter. This formatter creates log entries in JSON format with detailed key-value pairs, making it easier for tools like Elasticsearch and Kibana to parse and analyze the logs. Below is the updated code snippet from update_app.py:Structured logging with JSON format streamlines the process of querying logs in Elasticsearch and building detailed dashboards in Kibana.
Benefits of Structured Logging
- Enhanced Debugging: With more detailed log entries, you can pinpoint issues faster.
- Improved Queryability: JSON-formatted logs allow for precise queries and filtering in log management tools.
- Dashboard Readiness: The structured format integrates seamlessly with tools like Kibana, making dashboard creation more efficient.
Next Steps: Deploying the Updated Application
With the improved logging mechanism in place, the next step is to redeploy your application to the Kubernetes cluster and observe the new logs. This updated structure should provide better insights and more actionable data compared to the previous setup.Ensure to update your Kubernetes manifests appropriately and monitor the logs in real-time after redeployment to confirm that the new changes are taking effect.