# Earnify Error Logging System

## Overview
Comprehensive error logging system for debugging and monitoring the Earnify Telegram bot.

## Log Files Location
`/logs/` directory (auto-created)

## Log Types

### 1. Error Logs
**File:** `error_YYYY-MM-DD.log`
- PHP errors, warnings, notices
- Uncaught exceptions
- Database connection failures
- Function errors

### 2. Info Logs
**File:** `info_YYYY-MM-DD.log`
- Database connections
- Successful operations
- General information

### 3. Webhook Logs
**File:** `webhook_YYYY-MM-DD.log`
- All incoming Telegram webhook requests
- Complete JSON payloads
- Useful for debugging bot interactions

### 4. Query Logs
**File:** `query_YYYY-MM-DD.log`
- Database queries
- Query parameters
- Performance monitoring

## Files Added

### 1. `bot/logger.php`
Core logging system with:
- `logError()` - Log errors
- `logInfo()` - Log information
- `logWebhook()` - Log webhook activity
- `logQuery()` - Log database queries
- Automatic error/exception handlers

### 2. `admin/logs.php`
Web-based log viewer:
- View all log files
- Real-time refresh
- Color-coded output
- Easy navigation

## Usage

### In Code
```php
// Log an error
logError('User not found', ['user_id' => 123]);

// Log info
logInfo('User registered successfully', ['telegram_id' => 456]);

// Log webhook (automatic in webhook.php)
logWebhook($update);

// Log query
logQuery('SELECT * FROM users WHERE id = ?', [123]);
```

### Viewing Logs
1. Login to admin panel
2. Navigate to **Logs** menu
3. Select log file from dropdown
4. Click refresh to update

## Features
- ✅ Automatic error/exception handling
- ✅ Daily log rotation
- ✅ JSON context support
- ✅ Timestamp on every entry
- ✅ Web-based viewer
- ✅ No external dependencies

## Security
- Logs stored outside web root (recommended)
- Admin authentication required
- No sensitive data logged (passwords, tokens)

## Troubleshooting

### Logs not appearing?
1. Check `/logs` directory exists and is writable (755)
2. Verify PHP has write permissions
3. Check if error_reporting is enabled

### Log files too large?
- Logs rotate daily automatically
- Delete old log files manually if needed
- Consider implementing log cleanup cron job

## Production Tips
1. Monitor `error_*.log` files regularly
2. Check `webhook_*.log` if bot not responding
3. Use `query_*.log` for performance issues
4. Set up log rotation/cleanup for long-term use
