[GH-ISSUE #14018] Add JSON logging format support via environment variable #34923

Open
opened 2026-04-22 18:55:26 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @BOPOHA on GitHub (Feb 1, 2026).
Original GitHub issue: https://github.com/ollama/ollama/issues/14018

Problem

Currently, Ollama only supports text-based logging output, which makes it difficult to integrate with structured log aggregation systems like ELK stack, Splunk, or Grafana Loki in production environments.

Proposed Solution

Add support for JSON logging format via an OLLAMA_LOG_FORMAT environment variable. When set to "json", the logger would use slog.NewJSONHandler instead of the default text handler.

Why This Change is Important

  • Enables structured logging for production deployments
  • Improves integration with modern observability tools
  • Maintains backward compatibility (defaults to existing text format)
  • Follows Go's standard log/slog patterns

Usage

export OLLAMA_LOG_FORMAT=json  # Enable JSON format
# Default behavior (text format) when not set
Originally created by @BOPOHA on GitHub (Feb 1, 2026). Original GitHub issue: https://github.com/ollama/ollama/issues/14018 ## Problem Currently, Ollama only supports text-based logging output, which makes it difficult to integrate with structured log aggregation systems like ELK stack, Splunk, or Grafana Loki in production environments. ## Proposed Solution Add support for JSON logging format via an `OLLAMA_LOG_FORMAT` environment variable. When set to "json", the logger would use `slog.NewJSONHandler` instead of the default text handler. ## Why This Change is Important - Enables structured logging for production deployments - Improves integration with modern observability tools - Maintains backward compatibility (defaults to existing text format) - Follows Go's standard `log/slog` patterns ## Usage ```bash export OLLAMA_LOG_FORMAT=json # Enable JSON format # Default behavior (text format) when not set ```
GiteaMirror added the feature request label 2026-04-22 18:55:27 -05:00
Author
Owner

@dhirajlochib commented on GitHub (Apr 2, 2026):

Implemented in PR #15216.

Changed in logutil/logutil.go: NewLogger checks OLLAMA_LOG_FORMAT via os.Getenv (direct read to avoid a circular import with envconfig) and switches to slog.NewJSONHandler when the value equals json (case-insensitive). A LogFormat var is also wired into envconfig so it shows up in env var documentation / ollama serve output.

<!-- gh-comment-id:4178492718 --> @dhirajlochib commented on GitHub (Apr 2, 2026): Implemented in PR #15216. Changed in `logutil/logutil.go`: `NewLogger` checks `OLLAMA_LOG_FORMAT` via `os.Getenv` (direct read to avoid a circular import with `envconfig`) and switches to `slog.NewJSONHandler` when the value equals `json` (case-insensitive). A `LogFormat` var is also wired into `envconfig` so it shows up in env var documentation / `ollama serve` output.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#34923