> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# تسجيل DataStore

> إعداد تسجيل DataStore لأغراض تصحيح الأخطاء والمراقبة

يعتمد DataStore على وحدة التسجيل القياسية في Python. يوضح هذا الدليل كيفية إعداد التسجيل لأغراض تصحيح الأخطاء.

<div id="quick-start">
  ## البدء السريع
</div>

```python theme={null}
from pathlib import Path
Path("data.csv").write_text("""\
name,age,city,salary,department
Alice,25,NYC,55000,Engineering
Bob,30,LA,65000,Product
Charlie,35,NYC,80000,Engineering
Diana,28,SF,70000,Design
Eve,42,NYC,95000,Product
""")

from chdb import datastore as pd
from chdb.datastore.config import config

# Enable debug logging
config.enable_debug()

# Now all operations will log details
ds = pd.read_csv("data.csv")
result = ds.filter(ds['age'] > 25).to_df()
```

<div id="levels">
  ## مستويات السجل
</div>

| المستوى    | القيمة | الوصف                                           |
| ---------- | ------ | ----------------------------------------------- |
| `DEBUG`    | 10     | معلومات تفصيلية لأغراض استكشاف الأخطاء وإصلاحها |
| `INFO`     | 20     | معلومات تشغيلية عامة                            |
| `WARNING`  | 30     | رسائل تحذير (الافتراضي)                         |
| `ERROR`    | 40     | رسائل الخطأ                                     |
| `CRITICAL` | 50     | إخفاقات حرجة                                    |

<div id="setting-level">
  ## تعيين مستوى السجل
</div>

```python theme={null}
import logging
from chdb.datastore.config import config

# Using standard logging levels
config.set_log_level(logging.DEBUG)
config.set_log_level(logging.INFO)
config.set_log_level(logging.WARNING)  # Default
config.set_log_level(logging.ERROR)

# Using quick preset
config.enable_debug()  # Sets DEBUG level + verbose format
```

<div id="format">
  ## تنسيق السجل
</div>

<div id="simple">
  ### التنسيق البسيط (الافتراضية)
</div>

```python title="Query" theme={null}
config.set_log_format("simple")
```

```text title="Response" theme={null}
DEBUG - Executing SQL query
DEBUG - Cache miss for key abc123
```

<div id="verbose">
  ### تنسيق Verbose
</div>

```python title="Query" theme={null}
config.set_log_format("verbose")
```

```text title="Response" theme={null}
2024-01-15 10:30:45.123 DEBUG datastore.core - Executing SQL query
2024-01-15 10:30:45.456 DEBUG datastore.cache - Cache miss for key abc123
```

***

<div id="what-logged">
  ## ما الذي يُسجَّل
</div>

<div id="debug-logged">
  ### مستوى DEBUG
</div>

* استعلامات SQL التي تم إنشاؤها
* اختيار محرك التنفيذ
* عمليات ذاكرة التخزين المؤقت (hits/misses)
* توقيت العمليات
* معلومات مصدر البيانات

```text theme={null}
DEBUG - Creating DataStore from file 'data.csv'
DEBUG - SQL: SELECT * FROM file('data.csv', 'CSVWithNames') WHERE age > 25
DEBUG - Using engine: chdb
DEBUG - Execution time: 0.089s
DEBUG - Cache: Storing result (key: abc123)
```

<div id="info-logged">
  ### مستوى INFO
</div>

* اكتمال العمليات الرئيسية
* تغييرات الإعدادات
* عمليات الاتصال بمصادر البيانات

```text theme={null}
INFO - Loaded 1,000,000 rows from data.csv
INFO - Execution engine set to: chdb
INFO - Connected to MySQL: localhost:3306/mydb
```

<div id="warning-logged">
  ### مستوى WARNING
</div>

* استخدام ميزة لم يعد يُنصح بها
* تحذيرات الأداء
* مشكلات غير حرجة

```text theme={null}
WARNING - Large result set (>1M rows) may cause memory issues
WARNING - Cache TTL exceeded, re-executing query
WARNING - Column 'date' has mixed types, using string
```

<div id="error-logged">
  ### مستوى ERROR
</div>

* إخفاقات تنفيذ الاستعلامات
* أخطاء الاتصال
* أخطاء تحويل البيانات

```text theme={null}
ERROR - Failed to execute SQL: syntax error near 'FORM'
ERROR - Connection to MySQL failed: timeout
ERROR - Cannot convert column 'price' to float
```

***

<div id="custom">
  ## إعدادات التسجيل المخصّصة
</div>

<div id="python-logging">
  ### استخدام تسجيل في Python
</div>

```python theme={null}
import logging

# Configure root logger
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('datastore.log'),
        logging.StreamHandler()
    ]
)

# Get DataStore logger
ds_logger = logging.getLogger('chdb.datastore')
ds_logger.setLevel(logging.DEBUG)
```

<div id="log-file">
  ### التسجيل في ملف
</div>

```python theme={null}
import logging

# Create file handler
file_handler = logging.FileHandler('datastore_debug.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
))

# Add to DataStore logger
ds_logger = logging.getLogger('chdb.datastore')
ds_logger.addHandler(file_handler)
```

<div id="suppress">
  ### تعطيل التسجيل
</div>

```python theme={null}
import logging

# Suppress all DataStore logs
logging.getLogger('chdb.datastore').setLevel(logging.CRITICAL)

# Or using config
config.set_log_level(logging.CRITICAL)
```

***

<div id="scenarios">
  ## سيناريوهات استكشاف الأخطاء وإصلاحها
</div>

<div id="debug-sql">
  ### تصحيح أخطاء توليد SQL
</div>

```python theme={null}
config.enable_debug()

ds = pd.read_csv("data.csv")
result = ds.filter(ds['age'] > 25).groupby('city').sum()
```

مخرجات السجل:

```text theme={null}
DEBUG - Creating DataStore from file 'data.csv'
DEBUG - Building filter: age > 25
DEBUG - Building groupby: city
DEBUG - Building aggregation: sum
DEBUG - Generated SQL:
        SELECT city, SUM(*) 
        FROM file('data.csv', 'CSVWithNames')
        WHERE age > 25
        GROUP BY city
```

<div id="debug-engine">
  ### استكشاف أخطاء اختيار المحرّك وإصلاحها
</div>

```python theme={null}
config.enable_debug()

result = ds.filter(ds['x'] > 10).apply(custom_func)
```

مخرجات السجل:

```text theme={null}
DEBUG - filter: selecting engine (eligible: chdb, pandas)
DEBUG - filter: using chdb (SQL-compatible)
DEBUG - apply: selecting engine (eligible: pandas)
DEBUG - apply: using pandas (custom function)
```

<div id="debug-cache">
  ### استكشاف أخطاء عمليات ذاكرة التخزين المؤقت وإصلاحها
</div>

```python theme={null}
config.enable_debug()

# First execution
result1 = ds.filter(ds['age'] > 25).to_df()
# DEBUG - Cache miss for query hash abc123
# DEBUG - Executing query...
# DEBUG - Caching result (key: abc123, size: 1.2MB)

# Second execution (same query)
result2 = ds.filter(ds['age'] > 25).to_df()
# DEBUG - Cache hit for query hash abc123
# DEBUG - Returning cached result
```

<div id="debug-performance">
  ### استكشاف مشكلات الأداء وإصلاحها
</div>

```python theme={null}
config.enable_debug()
config.enable_profiling()

# Logs will show timing for each operation
result = (ds
    .filter(ds['amount'] > 100)
    .groupby('region')
    .agg({'amount': 'sum'})
    .to_df()
)
```

مخرجات السجل:

```text theme={null}
DEBUG - filter: 0.002ms
DEBUG - groupby: 0.001ms
DEBUG - agg: 0.003ms
DEBUG - SQL generation: 0.012ms
DEBUG - SQL execution: 89.456ms  <- Main time spent here
DEBUG - Result conversion: 2.345ms
```

***

<div id="production">
  ## إعدادات الإنتاج
</div>

<div id="recommended">
  ### الإعدادات الموصى بها
</div>

```python theme={null}
import logging
from chdb.datastore.config import config

# Production: minimal logging
config.set_log_level(logging.WARNING)
config.set_log_format("simple")
config.set_profiling_enabled(False)
```

<div id="rotation">
  ### تدوير السجلات
</div>

```python theme={null}
import logging
from logging.handlers import RotatingFileHandler

# Create rotating file handler
handler = RotatingFileHandler(
    'datastore.log',
    maxBytes=10*1024*1024,  # 10MB
    backupCount=5
)
handler.setLevel(logging.WARNING)

# Add to DataStore logger
logging.getLogger('chdb.datastore').addHandler(handler)
```

***

<div id="env-vars">
  ## متغيرات البيئة
</div>

يمكنك أيضًا ضبط إعدادات التسجيل باستخدام متغيرات البيئة:

```bash theme={null}
# Set log level
export CHDB_LOG_LEVEL=DEBUG

# Set log format
export CHDB_LOG_FORMAT=verbose
```

```python theme={null}
import os
import logging

# Read from environment
log_level = os.environ.get('CHDB_LOG_LEVEL', 'WARNING')
config.set_log_level(getattr(logging, log_level))
```

***

<div id="summary">
  ## الملخص
</div>

| المهمة         | الأمر                                    |
| -------------- | ---------------------------------------- |
| تفعيل التصحيح  | `config.enable_debug()`                  |
| تعيين المستوى  | `config.set_log_level(logging.DEBUG)`    |
| تعيين التنسيق  | `config.set_log_format("verbose")`       |
| التسجيل في ملف | استخدم معالجات التسجيل في Python         |
| كتم السجلات    | `config.set_log_level(logging.CRITICAL)` |
