# How to Read Import Error Logs

## 📋 Overview

All import logs are now written **ONLY** to the performance log file. No import logs will appear in `laravel.log`.

**Log File Location:** `storage/logs/import-performance.log`

---

## 🔍 Finding Product Errors

### **1. Open the Performance Log**

**Location:** `storage/logs/import-performance.log`

**How to access:**
- **Localhost:** Open the file directly
- **Production (cPanel):** File Manager → `public_html/Code/html2/storage/logs/import-performance.log`

---

### **2. Look for Product Errors**

Search for this pattern in the log:
```
[2025-11-18] local.ERROR: === PRODUCT IMPORT FAILED ===
```

---

## 📊 Understanding Error Log Format

### **Example Error Entry:**

```json
[2025-11-18 16:45:23] local.ERROR: === PRODUCT IMPORT FAILED === {
    "product_number": 145,
    "row_in_excel": 146,
    "sku": "PRODUCT-145",
    "product_name": "Test Product Name",
    "error_message": "Call to a member function getId() on null",
    "error_file": "/path/to/ProductRepository.php",
    "error_line": 234,
    "time_spent": "2345.67ms",
    "data": {
        "sku": "PRODUCT-145",
        "name": "Test Product Name",
        "description": "Product description here",
        "price": "99.99",
        "categories": "Category1 > Subcategory1, Category2",
        "quantity": "50",
        "images": "https://example.com/image1.jpg, https://example.com/image2.jpg"
    }
}
```

---

## 📖 What Each Field Means

| Field | Description | Example |
|-------|-------------|---------|
| **product_number** | Product position in import (0-based) | 145 |
| **row_in_excel** | Actual row number in Excel file (includes header) | 146 |
| **sku** | Product SKU from Excel | "PRODUCT-145" |
| **product_name** | Product name from Excel | "Test Product Name" |
| **error_message** | What went wrong | "Call to a member function..." |
| **error_file** | Which PHP file had the error | "ProductRepository.php" |
| **error_line** | Line number in that file | 234 |
| **time_spent** | How long before it failed | "2345.67ms" |
| **data** | All product data from Excel | Full product details |

---

## 🎯 Common Errors and Solutions

### **Error 1: "Call to a member function getId() on null"**

**Meaning:** A required object (category, attribute family, etc.) was not found

**Possible Causes:**
- Invalid category name
- Category doesn't exist
- Attribute family missing

**Solution:**
1. Check the `categories` field in the error log
2. Verify category exists in your system
3. Fix category name in Excel file
4. Re-import

---

### **Error 2: "Duplicate entry for key 'products_sku_unique'"**

**Meaning:** Product with this SKU already exists

**Solution:**
1. Check if SKU exists: `SELECT * FROM products WHERE sku = 'PRODUCT-145'`
2. Either:
   - Delete existing product
   - Change SKU in Excel file
   - Skip this product (it will be updated instead)

---

### **Error 3: "Allowed memory size exhausted"**

**Meaning:** PHP ran out of memory

**Solution:**
1. Reduce batch size in `SimplifiedImportController.php` line 178:
   ```php
   $batchSize = 20; // Reduced from 30
   ```
2. Or increase PHP memory limit in `.htaccess`:
   ```apache
   php_value memory_limit 256M
   ```

---

### **Error 4: "Maximum execution time exceeded"**

**Meaning:** Script took too long

**Solution:**
1. Increase timeout in `.htaccess`:
   ```apache
   php_value max_execution_time 300
   ```
2. Or reduce batch size

---

### **Error 5: "cURL error 28: Operation timed out"**

**Meaning:** Image download failed

**Solution:**
1. Check the `images` field in error log
2. Verify image URLs are accessible
3. Remove or fix problematic image URLs
4. Re-import

---

## 📋 Step-by-Step Error Resolution

### **Step 1: Find All Errors**

Search the log file for:
```
=== PRODUCT IMPORT FAILED ===
```

Count how many errors you have.

---

### **Step 2: Identify Failed Products**

For each error, note:
- **Row in Excel:** Which row to fix
- **SKU:** Which product
- **Error message:** What went wrong

**Example:**
```
Row 146: SKU "PRODUCT-145" - Error: "Invalid category name"
Row 203: SKU "PRODUCT-202" - Error: "Duplicate SKU"
Row 458: SKU "PRODUCT-457" - Error: "Image download failed"
```

---

### **Step 3: Fix the Excel File**

Open your Excel file and fix each error:

1. **Go to the row number** (from `row_in_excel` field)
2. **Fix the issue:**
   - Invalid category → Fix category name
   - Duplicate SKU → Change SKU or delete existing product
   - Image error → Fix image URL or remove it
3. **Save the Excel file**

---

### **Step 4: Re-import Failed Products**

**Option A: Re-import entire file**
- Upload the fixed Excel file
- Start import again
- Products that succeeded before will be updated (not duplicated)

**Option B: Import only failed products**
- Create a new Excel file with only the failed products
- Copy the header row
- Copy only the rows that failed
- Import this smaller file

---

## 🔍 Checking Import Success

### **Look for Completion Message:**

```
[2025-11-18] local.INFO: === BATCH IMPORT COMPLETED === {
    "total_products": 30,
    "processed": 30,
    "errors": 0,
    "successful": 30
}
```

**Key fields:**
- **total_products:** How many were in this batch
- **processed:** How many were attempted
- **errors:** How many failed
- **successful:** How many succeeded

---

### **Calculate Success Rate:**

```
Success Rate = (successful / processed) × 100%
```

**Example:**
- Processed: 697 products
- Errors: 12 products
- Successful: 685 products
- Success Rate: 98.3% ✅

---

## 📊 Performance Analysis

### **Check Batch Performance:**

```
[2025-11-18] local.INFO: === BATCH IMPORT COMPLETED === {
    "total_time": "91.09s",
    "avg_time_per_product": "3140.63ms",
    "performance_breakdown": {
        "image_handling": "29806.51ms (32.7%)",
        "product_update": "18803.5ms (20.6%)",
        "locale_addition": "16504.84ms (18.1%)",
        "product_creation": "14193.89ms (15.6%)",
        "flat_creation": "11013.42ms (12.1%)"
    }
}
```

**What to look for:**
- **total_time:** How long the batch took
- **avg_time_per_product:** Average time per product
- **performance_breakdown:** Where time is spent

**If import is slow:**
- Check which operation takes the most time
- If `image_handling` > 50%: Images are slow, check URLs
- If `product_update` > 30%: Database might be slow, add indexes
- If `flat_creation` > 25%: Too many products, reduce batch size

---

## 🚨 Troubleshooting Tips

### **No Errors But Products Missing?**

1. **Check if products were created:**
   ```sql
   SELECT COUNT(*) FROM products WHERE created_at >= '2025-11-18 16:00:00';
   ```

2. **Check product_flat table:**
   ```sql
   SELECT COUNT(*) FROM product_flat WHERE product_id IN (
       SELECT id FROM products WHERE created_at >= '2025-11-18 16:00:00'
   );
   ```

3. **If products exist but not visible:**
   - Clear cache: `php artisan cache:clear`
   - Reindex: `php artisan indexer:reindex`

---

### **Import Stopped Mid-Way?**

1. **Check last log entry:**
   ```
   [2025-11-18 16:45:23] local.INFO: Processing batch {"processed":120,"total":697}
   ```

2. **Calculate how many succeeded:**
   - Last processed: 120
   - Batch size: 30
   - Completed batches: 4 (0-30, 30-60, 60-90, 90-120)

3. **Resume or re-import:**
   - If session still active: Click "Continue Import"
   - If session expired: Create Excel with products 121-697 and import

---

### **Too Many Errors?**

If you have many errors (>10%), check:

1. **Common error pattern:**
   - Same error for all products? → Fix the root cause
   - Different errors? → Fix each individually

2. **Data quality:**
   - Are category names correct?
   - Are SKUs unique?
   - Are image URLs valid?
   - Are prices in correct format?

3. **System issues:**
   - Is database accessible?
   - Is there enough memory?
   - Are required tables/columns present?

---

## ✅ Best Practices

### **Before Import:**

1. ✅ **Test with small file** (10-20 products)
2. ✅ **Verify data quality** (no duplicates, valid categories)
3. ✅ **Check image URLs** (make sure they're accessible)
4. ✅ **Clear cache** (`php artisan cache:clear`)

### **During Import:**

1. ✅ **Monitor progress** (watch the percentage)
2. ✅ **Don't close browser** (keep tab open)
3. ✅ **Don't start multiple imports** (one at a time)

### **After Import:**

1. ✅ **Check performance log** for errors
2. ✅ **Verify products were created**
3. ✅ **Fix any errors** and re-import failed products
4. ✅ **Clear cache** to refresh product data

---

## 📞 Quick Reference

### **Log File Location:**
```
storage/logs/import-performance.log
```

### **Search for Errors:**
```
=== PRODUCT IMPORT FAILED ===
```

### **Search for Batch Completion:**
```
=== BATCH IMPORT COMPLETED ===
```

### **Search for Batch Failures:**
```
=== BATCH IMPORT FAILED ===
```

### **Check Product in Database:**
```sql
SELECT * FROM products WHERE sku = 'YOUR-SKU-HERE';
```

### **Count Imported Products:**
```sql
SELECT COUNT(*) FROM products WHERE created_at >= 'IMPORT-START-TIME';
```

---

**Last Updated:** 2025-11-18  
**Version:** 2.0  
**Status:** Enhanced Error Logging Active ✅
