🚀Logging with Golang, Docker, Cloud, and Loki Overview This project demonstrates a complete logging pipeline, from generating logs in a Golang service to storing and visualizing them using Loki. It includes containerization with Docker, log rotation with LogRotate, cloud storage using S3, and monitoring with Loki.
🔹 Key Features Golang Logging – Generates logs with different levels (INFO to ERROR). Docker Integration – Ensures the service runs smoothly in any environment. Log Rotation – Uses LogRotate to manage and archive logs. Cloud Storage – Uploads logs to Amazon S3 for backup. Loki Monitoring – Visualizes logs for easy analysis. 🛠️ Requirements Golang – For log generation. Docker – For containerization. AWS CLI – For uploading logs to S3 (or Azure CLI for Azure Blob). Loki – For log visualization. LogRotate – For log management. 📌 Steps to Set Up 1️⃣ Generate Logs with Golang Navigate to the project folder: cd cmd Run the Golang service: go run main.go View logs in the terminal. 2️⃣ Run the Service in Docker Go to the project root: cd .. Build the Docker image: docker build -t golang-logging-service . Start the container: docker run -d golang-logging-service 3️⃣ Enable Log Rotation Modify logrotate.conf as needed. Mount a volume when running Docker. Run log rotation manually: logrotate -f logrotate.conf 4️⃣ Store Logs in S3 Configure AWS CLI for S3 access.
Create a script (upload_logs.sh) to upload logs:
bash Copy Edit #!/bin/bash AWS_BUCKET="your-s3-bucket" LOG_FILE="path/to/rotated/logfile.log" aws s3 cp "$LOG_FILE" "s3://$AWS_BUCKET/logs/$(basename "$LOG_FILE")" Automate uploads using cron.
5️⃣ Monitor Logs with Loki Configure Loki to read logs from S3.
Use Grafana or Loki CLI to query logs:
text Copy Edit {job="golang-logging-service"} ⚙️ Customization Edit logrotate.conf for specific log rotation settings. Modify upload_logs.sh to change storage location. Adjust Loki settings for better visualization. 🔮 Future Enhancements Integrate Fluentd or Logstash for better log management. Add advanced log filtering in Loki. Automate log upload monitoring. Expand to Azure Blob storage. Create Grafana dashboards for visualization. Implement CI/CD for automated logging pipeline. This setup ensures a smooth and efficient logging system, making log management and monitoring easier! 🚀