# Error Logging System

Sistem logging otomatis untuk monitoring error dan auto-recovery events.

## Lokasi Log Files

Semua log tersimpan di folder `logs/`:

```
logs/
├── error-2026-01-17.log        # Error logs (daily rotation)
├── recovery-2026-01-17.log     # Auto-recovery events
├── combined-2026-01-17.log     # Semua events
```

## Format Log

Logs disimpan dalam format JSON untuk memudahkan parsing:

```json
{
  "timestamp": "2026-01-17 14:05:34",
  "level": "error",
  "message": "Failed to send message",
  "sessionId": "wa1",
  "endpoint": "/message/send-text",
  "error": "Connection Closed",
  "stack": "Error: Connection Closed\n    at ..."
}
```

## Jenis Log

### 1. Error Logs (`error-*.log`)

Mencatat semua error yang terjadi:

- Validation errors
- WhatsApp errors
- Internal server errors
- Failed retry attempts

**Contoh:**

```json
{
  "timestamp": "2026-01-17 14:05:34",
  "level": "error",
  "message": "Validation Error",
  "type": "ValidationError",
  "endpoint": "/message/send-text",
  "method": "POST",
  "sessionId": "wa1",
  "stack": "..."
}
```

### 2. Recovery Logs (`recovery-*.log`)

Mencatat auto-reconnect dan recovery events:

- Session not found attempts
- Auto-reconnect berhasil
- Auto-healing (session reset)
- Retry attempts

**Contoh:**

```json
{
  "timestamp": "2026-01-17 14:05:35",
  "level": "info",
  "message": "Session wa1 encountered error. Attempting to restart session...",
  "type": "recovery",
  "sessionId": "wa1",
  "endpoint": "sendMessage",
  "error": "Connection closed"
}
```

### 3. Combined Logs (`combined-*.log`)

Mencatat semua events (errors + recovery + info)

## Monitoring

### Melihat Error Terbaru

```bash
# Windows PowerShell
Get-Content logs\error-2026-01-17.log -Tail 20

# Atau lihat semua log hari ini
Get-Content logs\combined-2026-01-17.log
```

### Mencari Error Tertentu

```bash
# Cari error untuk session wa1
Select-String "wa1" logs\error-*.log

# Cari semua "Connection Closed" errors
Select-String "Connection Closed" logs\error-*.log
```

### Monitoring Real-Time

```bash
# Follow logs (seperti tail -f di Linux)
Get-Content logs\combined-2026-01-17.log -Wait
```

## Log Rotation

- **Rotasi**: Logs di-rotate setiap hari (midnight)
- **Retention**: Logs disimpan selama **14 hari**
- **Max Size**: File akan di-rotate jika mencapai **20MB**
- **Old Logs**: Otomatis dihapus setelah 14 hari

## Log Levels

- `error`: Error yang memerlukan perhatian
- `warn`: Warning (disconnections, dll)
- `info`: Informasi umum (connections, API requests)
- `debug`: Detail debugging (tidak aktif di production)

## Contoh Use Cases

### 1. Troubleshooting "Connection Closed"

Lihat di `recovery-*.log` untuk melihat berapa kali auto-recovery terjadi:

```bash
Select-String "Connection" logs\recovery-*.log
```

### 2. Monitor Session Tertentu

```bash
Select-String "wa1" logs\combined-*.log
```

### 3. Analisa Error Pattern

```bash
# Hitung berapa kali error terjadi
(Select-String "error" logs\error-*.log).Count
```

## Tips

1. **Check logs secara berkala** untuk melihat pola error
2. **Recovery logs** menunjukkan seberapa sering session disconnect
3. Jika ada **error berulang** untuk session tertentu, mungkin perlu scan QR ulang
4. **Bandwidth monitoring**: Jika banyak auto-recovery, mungkin ada masalah koneksi internet

## Troubleshooting

### Logs Tidak Muncul?

- Pastikan aplikasi berjalan
- Check apakah folder `logs/` sudah ter-create
- Coba trigger error dengan request API

### Log Files Terlalu Besar?

- Log rotation otomatis aktif (max 20MB per file)
- Old logs (>14 hari) otomatis dihapus
- Bisa adjust di `utils/logger.js`: `maxSize` dan `maxFiles`

### Ingin Log Level Lebih Detail?

Edit `.env`:

```env
LOG_LEVEL=debug
```

Restart aplikasi untuk apply perubahan.
