Log Analysis¶
Overview¶
Effective log analysis is crucial for troubleshooting, security monitoring, and understanding system behavior. This page covers techniques for analyzing logs in Loki using LogQL.
Basic Log Queries¶
Stream Selection¶
Select by label:
Multiple label values:
Exclude labels:
Filtering Log Lines¶
Text Matching¶
Contains:
Does not contain:
Case-insensitive:
Multiple conditions:
Log Parsing¶
Regex Parsing¶
Extract fields:
{service_type="fail2ban"}
| regexp `\[(?P<jail>[^\]]+)\]\s+(?P<action>Ban|Unban)\s+(?P<ip>\d+\.\d+\.\d+\.\d+)`
| jail="sshd"
Apache access log parsing:
{service_type="web", log_type="access"}
| regexp `^(?P<ip>\S+) \S+ \S+ \[(?P<timestamp>[^\]]+)\] "(?P<method>\S+) (?P<path>\S+) \S+" (?P<status>\d+) (?P<size>\d+)`
| status="500"
JSON Parsing¶
For JSON-formatted logs:
Pattern Matching¶
Use pattern for simpler parsing:
Aggregations¶
Count Logs¶
Count over time:
Group by label:
Top N results:
topk(20, sum by(ip) (
count_over_time(
{service_type="fail2ban"}
| regexp `Ban\s+(?P<ip>\d+\.\d+\.\d+\.\d+)` [7d]
)
))
Rate Calculations¶
Log rate:
Error rate:
Percentage:
Time-Based Analysis¶
Time Ranges¶
Last hour:
Specific time range: Use Grafana time picker or API parameters
Bucketed Counts¶
Count logs per hour:
Time Series¶
Logs over time:
Advanced Queries¶
Multi-Stage Pipeline¶
{service_type="fail2ban"}
| regexp `\[(?P<jail>[^\]]+)\]\s+(?P<action>Ban|Unban)\s+(?P<ip>\d+\.\d+\.\d+\.\d+)`
| action="Ban"
| jail=~"sshd|dovecot"
| ip!~"192\.168\..*"
Log Context¶
Logs before and after match: Use Grafana's "Show context" feature, or query with time range:
Metric Queries from Logs¶
Bytes transferred:
sum(
sum_over_time(
{service_type="web", log_type="access"}
| regexp `\s(?P<bytes>\d+)$`
| unwrap bytes [1h]
)
)
Common Analysis Patterns¶
Security Analysis¶
Failed login attempts:
{service_type="system"}
|= "Failed password"
| regexp `Failed password for (?P<user>\S+) from (?P<ip>\S+)`
Top banned IPs:
topk(20, sum by(ip) (
count_over_time(
{service_type="fail2ban"}
| regexp `Ban\s+(?P<ip>\d+\.\d+\.\d+\.\d+)` [7d]
)
))
Unusual activity hours:
Application Monitoring¶
Error frequency:
Slow requests (if duration in logs):
User activity:
System Monitoring¶
Service restarts:
Disk space warnings:
OOM events:
{service_type="system"}
|= "Out of memory"
| regexp `Killed process (?P<pid>\d+) \((?P<process>[^)]+)\)`
Query Optimization¶
Performance Tips¶
- Always use time ranges: Avoid unbounded queries
- Use specific labels: Filter by label before parsing
- Avoid regex when possible: Use literal matching when you can
- Limit results: Use
limitclause for exploratory queries - Use instant queries for tables: Faster than range queries
Slow:
Fast:
Query Examples¶
Debug query performance:
Grafana Integration¶
Log Panel¶
- Use "Logs" visualization
- Enable "Live" mode for real-time
- Use "Show context" for surrounding logs
- Apply filters interactively
Table Panel¶
Use instant queries for tables:
Add transformations: - "Labels to fields" for Loki queries - "Organize fields" to reorder columns - "Sort by" to order results
Time Series Panel¶
Use range queries for graphs:
Export and Reporting¶
Export Logs¶
Via API:
curl -G "http://localhost:3100/loki/api/v1/query_range" \
--data-urlencode 'query={service_type="fail2ban"}' \
--data-urlencode 'start=1735000000000000000' \
--data-urlencode 'end=1735100000000000000' \
-o logs-export.json
Parse JSON output:
Scheduled Reports¶
Use Grafana alerting/reporting to: 1. Create dashboard with key queries 2. Set up scheduled snapshot or PDF export 3. Send via email or webhook
Best Practices¶
- Start broad, narrow down: Begin with label filters, then add log line filters
- Test queries incrementally: Add one filter at a time
- Use descriptive labels: Make queries readable
- Document complex queries: Add comments in dashboards
- Save useful queries: Create dashboard or alert from working query
- Monitor query performance: Avoid expensive queries that timeout
- Use templates: Create reusable query patterns
Reference¶
- LogQL documentation: https://grafana.com/docs/loki/latest/logql/
- Query examples in Dashboard Development chapter