Overview
TheLogger class provides a lightweight file-based logging system for the WhatsApp RAG Bot. It supports multiple log levels and automatically organizes logs by date in the logs/ directory.
Logs are stored as plain text files with one entry per line, making them easy to parse and analyze with standard Unix tools like
grep, tail, and awk.Initialization
0755 permissions.
Log Levels
The Logger supports four standard log levels:| Level | Method | Use Case |
|---|---|---|
| INFO | info() | General application events, successful operations |
| WARNING | warning() | Warning conditions, non-critical issues |
| ERROR | error() | Error conditions, failed operations |
| DEBUG | debug() | Detailed debugging information |
Basic Logging
Contextual Logging
Add structured context to log entries for better debugging:Log File Structure
File Organization
Logs are stored in daily files with the formatYYYY-MM-DD.log:
Log Entry Format
Each log entry follows this format:logs/2026-03-06.log):
Real-World Examples
Log Analysis
Use standard Unix tools to analyze logs:API Reference
__construct(string $logPath = null)
Initializes the logger with a log directory path.
Location: src/Core/Logger.php:9
Parameters:
$logPath(string, optional): Path to log directory. Defaults tologs/in project root.
0755 permissions
log(string $message, string $level = 'INFO', array $context = [])
Writes a log entry with the specified level and context.
Location: src/Core/Logger.php:18
Parameters:
$message(string): Log message$level(string): Log level (INFO, WARNING, ERROR, DEBUG)$context(array): Optional context data (JSON-encoded)
info(string $message, array $context = [])
Logs an informational message.
Location: src/Core/Logger.php:35
Parameters:
$message(string): Log message$context(array): Optional context data
error(string $message, array $context = [])
Logs an error message.
Location: src/Core/Logger.php:40
Parameters:
$message(string): Error description$context(array): Optional error details
debug(string $message, array $context = [])
Logs a debug message.
Location: src/Core/Logger.php:45
Parameters:
$message(string): Debug information$context(array): Optional debug data
warning(string $message, array $context = [])
Logs a warning message.
Location: src/Core/Logger.php:50
Parameters:
$message(string): Warning description$context(array): Optional warning details
Best Practices
Use Appropriate Levels
Use INFO for normal operations, WARNING for potential issues, ERROR for failures, and DEBUG for development.
Add Context
Always include relevant context (IDs, timestamps, values) to make debugging easier.
Log Rotation
Implement log rotation to prevent disk space issues. Consider using
logrotate on Linux.Sensitive Data
Never log sensitive data like passwords, API keys, or full credit card numbers.
Log Rotation
On Linux systems, uselogrotate to manage log files:
/etc/logrotate.d/whatsapp-bot
Related Services
Database
Log database operations and errors
Error Handling
Exception handling and error recovery