Skip to main content

Exploring Logs in Grafana

Introduction

Log exploration is a fundamental skill for any developer or system administrator working with modern applications. Grafana provides powerful tools for exploring and analyzing logs, allowing you to troubleshoot issues, monitor application behavior, and gain insights into your systems.

In this guide, we'll dive into Grafana's log exploration capabilities, focusing on how to effectively query, filter, and analyze logs from various data sources, with special attention to Loki integration.

Understanding Grafana's Explore UI

The Explore UI in Grafana is specifically designed for ad-hoc data exploration and troubleshooting. It's the primary interface for working with logs.

Accessing Explore

To start exploring logs in Grafana:

  1. Log in to your Grafana instance
  2. Click on the Explore icon in the left sidebar (it looks like a compass)
  3. Select your log data source from the dropdown menu at the top

![Explore UI]

The Explore UI is divided into several key sections:

  • Query editor: Where you write your log queries
  • Time range control: For selecting the time period to analyze
  • Results panel: Displays your logs in various visualization formats
  • Live logs streaming: For viewing logs in real-time

Querying Logs in Grafana

Grafana supports multiple logging data sources, including:

  • Loki
  • Elasticsearch
  • CloudWatch
  • Azure Monitor
  • Google Cloud Logging

The query syntax will vary depending on your data source. We'll focus on Loki, which is Grafana's native logging solution.

Basic LogQL Queries

Loki uses LogQL, a query language inspired by PromQL but designed for logs. Here are some basic query patterns:

{app="myapp"}

This simple query returns all logs from the label app with value myapp.

To filter logs containing specific text:

{app="myapp"} |= "error"

This returns all logs from myapp that contain the word "error".

Advanced Filtering

You can use various operators to create more advanced filters:

OperatorDescription
|=Log line contains string
!=Log line does not contain string
|~Log line matches regular expression
!~Log line does not match regular expression

Example of combining filters:

{app="myapp", environment="production"} |= "error" != "timeout"

This query finds logs from the production environment for myapp that contain "error" but not "timeout".

Working with Log Results

Once you've executed a query, Grafana presents the logs in a structured way, allowing for deeper analysis.

Log Level Visualization

Grafana automatically detects common log levels and color-codes them:

  • Critical/Fatal: Purple
  • Error: Red
  • Warning: Yellow
  • Info: Green
  • Debug/Trace: Blue

This visual cue helps you quickly identify problematic logs.

Viewing Log Details

Click on any log line to expand it and see:

  • Full text content
  • Parsed fields
  • Labels
  • Detected links

Log Context

To understand the events surrounding a specific log entry:

  1. Find a log of interest
  2. Click the "Show context" button
  3. Grafana will display logs that occurred before and after your selected log

This helps establish the sequence of events that led to an error or behavior you're investigating.

Analyzing Log Patterns

Log Volume Analysis

The log volume graph at the top of the results shows the distribution of logs over time, helping you identify:

  • Spikes in log volume
  • Periods of silence
  • Patterns related to deployments or system events

Using Live Tailing

For real-time monitoring:

  1. Select the desired time range
  2. Click the "Live" button in the upper right corner
  3. Logs will stream in real-time, automatically refreshing

Live tailing is useful during deployments or when actively troubleshooting issues.

Advanced Techniques

Using Log Labels for Dynamic Filtering

Grafana allows you to filter logs by clicking on labels directly from the log output:

  1. Hover over a log entry
  2. Click on any label value that appears
  3. Grafana will update your query to include this label filter

This dynamic filtering makes exploration more interactive and efficient.

Creating Metrics from Logs

With Loki as your data source, you can extract metrics from your logs using LogQL:

sum(rate({app="myapp"} |= "error" [5m])) by (service)

This query counts error rates across different services.

Visualizing Log Data

You can visualize log query results in various ways:

  1. Bar charts: Show log volume distribution
  2. Graphs: Display extracted metrics
  3. Tables: Present structured log data

To create visualizations from Explore:

  1. Run your query
  2. Click "Add to dashboard"
  3. Select the visualization type
  4. Configure the panel options

Real-World Example: Troubleshooting Application Errors

Let's walk through a practical example of troubleshooting an application using Grafana log exploration.

Scenario

Your users are reporting intermittent errors on your e-commerce platform. Here's how to investigate:

  1. Open Explore and select your log data source
  2. Query for error logs from the application:
{app="ecommerce-app", environment="production"} |= "error"
  1. Notice a spike in errors around 2:00 PM
  2. Refine your query to focus on that time period
  3. Expand error logs to see details
  4. Notice many errors related to the payment service
  5. Further refine your query:
{app="ecommerce-app", component="payment-service"} |= "error"
  1. Identify a pattern: errors occurring when processing specific types of credit cards
  2. Check for recent deployments or changes to the payment service
  3. Find the root cause: a recent update introduced a validation bug for certain card types

This workflow demonstrates how effective log exploration can quickly pinpoint issues that would be difficult to identify through other means.

Integration with Metrics and Traces

Grafana's power comes from its ability to correlate logs with metrics and traces, providing a complete observability solution.

Split View

To compare logs with metrics:

  1. In Explore, run your log query
  2. Click the "Split" button at the top
  3. Select a metrics data source in the new pane
  4. Query related metrics
  5. Both results will share the same time range, allowing you to correlate events

Trace Integration

If your logs contain trace IDs:

  1. Find a log entry with a trace ID
  2. Click on the trace ID link
  3. Grafana will open the corresponding trace, showing the full request journey

This connection between logs, metrics, and traces is known as "correlations" and is a powerful feature for complex troubleshooting.

Best Practices for Log Exploration

To make the most of Grafana's log exploration capabilities:

  1. Use structured logging: Structure your application logs with consistent fields
  2. Add meaningful labels: Labels like service, environment, and component make filtering more effective
  3. Include trace IDs: Enable correlation between logs and traces
  4. Set appropriate log levels: Reserve ERROR for actual errors, not expected conditions
  5. Create log exploration dashboards: Save common queries as dashboard panels
  6. Use log volume alerts: Set up alerts for unusual log patterns

Summary

Grafana's log exploration features provide a powerful toolkit for understanding and troubleshooting your applications and infrastructure. By mastering log queries, filters, and analysis techniques, you can:

  • Reduce mean time to resolution (MTTR) for incidents
  • Gain insights into application behavior
  • Identify patterns and trends
  • Correlate logs with metrics and traces for complete observability

As you become more familiar with log exploration in Grafana, you'll develop your own techniques and workflows tailored to your specific systems and challenges.

Additional Resources

Here are some resources to further expand your knowledge:

Exercises

To practice your log exploration skills:

  1. Set up a local Grafana and Loki instance using Docker Compose
  2. Configure an application to send logs to Loki
  3. Write queries to filter logs by different criteria
  4. Create a dashboard that shows error rates extracted from logs
  5. Set up an alert that triggers when specific log patterns appear


If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)