Query Interface
Logchef provides a powerful query interface that combines the flexibility of SQL with an intuitive visual builder. This guide will help you master log querying in Logchef.
Query Builder
The query builder consists of three main components:
- Visual Query Builder
- SQL Editor
- Results View
Visual Query Builder
The visual builder lets you construct queries without writing SQL:
- Time Range: Select predefined ranges or specify custom intervals
- Fields: Choose which log fields to display
- Filters: Add conditions to filter your logs
- Group By: Aggregate logs by specific fields
- Sort: Order results by timestamp or other fields
- Limit: Control the number of results returned
SQL Editor
For advanced users, the SQL editor provides full access to Clickhouse’s SQL capabilities:
SELECT toStartOfInterval(timestamp, INTERVAL 5 MINUTE) as time_bucket, level, count() as countFROM logsWHERE timestamp >= now() - INTERVAL 1 HOUR AND level IN ('error', 'warning')GROUP BY time_bucket, levelORDER BY time_bucket DESC
Results View
Results can be viewed in multiple formats:
- Table View: Traditional tabular format
- JSON View: Raw JSON format
- Time Series: Visualize time-based data
- Logs View: Optimized for log reading with syntax highlighting
Common Query Patterns
Error Analysis
Find error patterns in your logs:
SELECT error_type, count() as error_count, arrayJoin(groupArray(message)) as sample_messagesFROM logsWHERE level = 'error' AND timestamp >= now() - INTERVAL 24 HOURGROUP BY error_typeORDER BY error_count DESCLIMIT 10
Response Time Analysis
Analyze API response times:
SELECT path, count() as requests, avg(response_time) as avg_response_time, quantile(0.95)(response_time) as p95_response_timeFROM logsWHERE timestamp >= now() - INTERVAL 1 HOUR AND type = 'access_log'GROUP BY pathHAVING requests > 100ORDER BY avg_response_time DESC
Log Volume Analysis
Monitor log volume trends:
SELECT toStartOfHour(timestamp) as hour, service, count() as log_countFROM logsWHERE timestamp >= now() - INTERVAL 24 HOURGROUP BY hour, serviceORDER BY hour DESC, log_count DESC
Advanced Features
Saved Queries
Save frequently used queries:
- Write your query
- Click “Save Query”
- Give it a name and description
- Optionally share with team members
Query Variables
Use variables in your queries:
SELECT *FROM logsWHERE timestamp >= {start_time} AND timestamp <= {end_time} AND service = {service_name:String}
Query Scheduling
Schedule queries to run periodically:
- Save your query
- Click “Schedule”
- Set interval (hourly, daily, etc.)
- Configure notifications
Performance Tips
- Use Time Filters: Always include timestamp filters
- Limit Results: Use LIMIT clause for large queries
- Optimize Joins: Prefer pre-aggregation when possible
- Use Materialized Views: For common query patterns
Next Steps
- Learn about Dashboards
- Set up Alerts
- Explore Advanced SQL Features