A comprehensive logging system has been successfully added to the blue-yellow project with environment variable control.
A centralized logging utility with:
- Dual Output: Console + optional file logging
- Environment Control: Toggle file logging with
SAVE_LOGSenv var - Auto Directory Creation: Creates
logs/folder automatically - Timestamped Files: Format
YYYY-MM-DD_HH-MM-SS.log - Flexible Configuration: Custom log levels and formats
- UTF-8 Support: Handles special characters
Key Functions:
setup_logging(name, level, log_format) # Initialize logging
get_logger(name) # Get logger instanceFiles Created:
.env- Active environment configuration.env.sample- Template for users
Environment Variable:
SAVE_LOGS=true # Enable file logging
SAVE_LOGS=false # Console only (default)Added to pyproject.toml:
python-dotenv = "^1.0.0"
Updated .gitignore:
*.log
logs/
Updated src/cli/menu.py:
- Automatically loads
.envfile - Sets up logging on startup
- Logs all tool executions (Ping, Clone, etc.)
- Logs errors and warnings
- Logs application lifecycle events
Created:
src/lambda/entrypoint_with_logging.py- Lambda with loggingLOGGING.md- Comprehensive user guidesrc/utils/README_LOGGING.md- Detailed documentation
cd /home/kali/labs/blue-yellow
poetry installEdit .env file:
SAVE_LOGS=true# Run the CLI
poetry run python src/main.py
# Or with environment override
SAVE_LOGS=true poetry run python src/main.py# List log files
ls -lh logs/
# View latest log
tail -20 logs/*.log2025-10-01 14:32:19,082 - blue-yellow - INFO - Blue-Yellow CLI started
2025-10-01 14:32:25,123 - blue-yellow - INFO - Tool: Ping URL - Target: https://example.com
2025-10-01 14:32:25,456 - blue-yellow - INFO - Ping successful - Status: 200, Time: 333ms
2025-10-01 14:35:10,479 - blue-yellow - INFO - File logging enabled. Logs will be saved to: logs/2025-10-01_14-35-10.log
2025-10-01 14:35:10,480 - blue-yellow - INFO - Blue-Yellow CLI started
2025-10-01 14:35:15,123 - blue-yellow - INFO - Tool: Ping URL - Target: https://example.com
2025-10-01 14:35:15,456 - blue-yellow - INFO - Ping successful - Status: 200, Time: 333ms
Log file created: logs/2025-10-01_14-35-10.log
blue-yellow/
├── .env # Environment configuration
├── .env.sample # Environment template
├── LOGGING.md # User guide
├── LOGGING_SETUP.md # This file
├── pyproject.toml # Updated with python-dotenv
├── logs/ # Created automatically when SAVE_LOGS=true
│ ├── 2025-10-01_14-35-10.log
│ ├── 2025-10-01_15-22-30.log
│ └── ...
└── src/
├── main.py # CLI entry point
├── cli/
│ └── menu.py # Updated with logging
├── lambda/
│ ├── entrypoint.py # Original
│ └── entrypoint_with_logging.py # With logging
└── utils/
├── logger.py # Core logging module ⭐
└── README_LOGGING.md # Detailed docs
from utils.logger import get_logger
logger = get_logger(__name__)
def my_function():
logger.info("Function started")
try:
# Your code
result = process_data()
logger.info(f"Success: {result}")
except Exception as e:
logger.error(f"Error: {e}")from utils.logger import setup_logging
import logging
logger = setup_logging(__name__, level=logging.DEBUG)
logger.debug("Detailed debug info")
logger.info("General info")
logger.warning("Warning message")
logger.error("Error message")# Enable for single run
SAVE_LOGS=true poetry run python src/main.py
# Enable for session
export SAVE_LOGS=true
poetry run python src/main.py$ SAVE_LOGS=true poetry run python src/main.py
# ✓ logs/ directory created automatically if it doesn't exist$ ls logs/
2025-10-01_14-30-45.log
2025-10-01_15-45-10.log
2025-10-02_09-20-33.log
# ✓ Each run creates a new timestamped file$ SAVE_LOGS=true poetry run python src/main.py
# ✓ Logs appear in BOTH console AND file$ SAVE_LOGS=false poetry run python src/main.py
# ✓ Console only, no file created
$ SAVE_LOGS=true poetry run python src/main.py
# ✓ Console + file logging- CLI Menu (
src/cli/menu.py)- Tool executions logged
- Errors and warnings logged
- Application lifecycle logged
- Ping utility (
src/utils/ping.py) - Clone utility (
src/utils/clone.py) - D2 utility (
src/utils/d2.py) - Login utility (
src/utils/login.py) - Lambda function (
src/lambda/entrypoint.py)
# Before
def ping_url(url: str):
# ... code ...
# After
from utils.logger import get_logger
logger = get_logger(__name__)
def ping_url(url: str):
logger.info(f"Pinging {url}")
# ... code ...
logger.info(f"Ping successful - Status: {status}")# View all logs
cat logs/*.log
# View last 20 lines
tail -20 logs/*.log
# Follow in real-time
tail -f logs/2025-10-01_14-35-10.log
# Search logs
grep "ERROR" logs/*.log# Remove logs older than 7 days
find logs/ -name "*.log" -mtime +7 -delete
# Remove all logs
rm -rf logs/Solution:
poetry installCheck:
- Is
SAVE_LOGS=truein.env? - Run:
cat .env | grep SAVE_LOGS - No spaces around
=: ✓SAVE_LOGS=true✗SAVE_LOGS = true
Solution:
# Ensure directory is writable
chmod u+w .Solution:
# Run from project root
cd /home/kali/labs/blue-yellow
poetry run python src/main.py- LOGGING.md - Comprehensive user guide with examples
- src/utils/README_LOGGING.md - Detailed API documentation
- src/utils/logger.py - Well-documented source code
-
Test the System:
SAVE_LOGS=true poetry run python src/main.py
-
Verify Log File:
ls -lh logs/ cat logs/*.log -
Integrate into Other Modules:
- Add logging to utility functions
- Add logging to Lambda functions
- Add logging to service modules
-
Customize as Needed:
- Adjust log format
- Change log levels
- Add custom handlers
- 🔍 Debugging: Track program execution and errors
- 📊 Auditing: Record all tool usage and results
- 🔐 Security: Log authentication attempts and access
- 📈 Monitoring: Track performance and issues
- 🚀 Production: Enable detailed logging when needed
You now have a fully functional logging system that:
- ✅ Creates timestamped log files when enabled
- ✅ Automatically creates logs/ directory
- ✅ Controls file logging via SAVE_LOGS environment variable
- ✅ Logs all program execution and errors
- ✅ Works with CLI, Lambda, and utility modules
- ✅ Follows Python logging best practices
To enable logging, simply set:
SAVE_LOGS=trueEnjoy your new logging system! 🎊