[PR #14147] feat: add JSON logging format support via OLLAMA_LOG_FORMAT env var #14538

Open
opened 2026-04-13 00:57:10 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/14147
Author: @veeceey
Created: 2/7/2026
Status: 🔄 Open

Base: mainHead: feat/issue-14018-json-logging


📝 Commits (2)

  • 1fd3578 docs: add num_gpu parameter to Modelfile documentation
  • c7eddfc feat: add JSON logging format support via OLLAMA_LOG_FORMAT env var

📊 Changes

3 files changed (+170 additions, -2 deletions)

View changed files

📝 docs/modelfile.mdx (+1 -0)
📝 logutil/logutil.go (+13 -2)
logutil/logutil_test.go (+156 -0)

📄 Description

Summary

This PR adds support for structured JSON logging to improve production observability and integration with log aggregation systems.

Changes

  • Modified logutil/logutil.go to support the OLLAMA_LOG_FORMAT environment variable
  • When set to "json" (case-insensitive), uses slog.NewJSONHandler for structured JSON output
  • Defaults to text format for backward compatibility (existing behavior preserved)
  • Added comprehensive test coverage in logutil/logutil_test.go

Environment Variable

  • Variable: OLLAMA_LOG_FORMAT
  • Values: "json" for JSON format, unset or any other value for text format (default)
  • Case sensitivity: Case-insensitive (accepts "json", "JSON", "Json", etc.)

Benefits

  • Enables structured logging for production deployments
  • Easier integration with log aggregation tools (ELK, Splunk, CloudWatch, etc.)
  • Maintains full backward compatibility (default behavior unchanged)
  • All existing log attributes (level, source, custom fields) work in both formats

Test Plan

All tests pass:

cd logutil && go test -v

Tests verify:

  • Default text format when env var is unset
  • JSON format when OLLAMA_LOG_FORMAT=json
  • Case-insensitive format detection
  • Invalid values default to text format
  • TRACE level works in JSON format
  • Source field formatting preserved in both formats

Example Usage

Text format (default):

./ollama serve
# Output: time=2024-01-01T12:00:00.000Z level=INFO source=server.go:123 msg="server started"

JSON format:

OLLAMA_LOG_FORMAT=json ./ollama serve
# Output: {"time":"2024-01-01T12:00:00.000Z","level":"INFO","source":{"file":"server.go","line":123},"msg":"server started"}

Fixes #14018


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/14147 **Author:** [@veeceey](https://github.com/veeceey) **Created:** 2/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/issue-14018-json-logging` --- ### 📝 Commits (2) - [`1fd3578`](https://github.com/ollama/ollama/commit/1fd357881a0419d940e9ec19ace494dab77af0c6) docs: add num_gpu parameter to Modelfile documentation - [`c7eddfc`](https://github.com/ollama/ollama/commit/c7eddfcb0a71a1fb7ff2ba1d5714da5194934ddb) feat: add JSON logging format support via OLLAMA_LOG_FORMAT env var ### 📊 Changes **3 files changed** (+170 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `docs/modelfile.mdx` (+1 -0) 📝 `logutil/logutil.go` (+13 -2) ➕ `logutil/logutil_test.go` (+156 -0) </details> ### 📄 Description ## Summary This PR adds support for structured JSON logging to improve production observability and integration with log aggregation systems. ## Changes - Modified `logutil/logutil.go` to support the `OLLAMA_LOG_FORMAT` environment variable - When set to `"json"` (case-insensitive), uses `slog.NewJSONHandler` for structured JSON output - Defaults to text format for backward compatibility (existing behavior preserved) - Added comprehensive test coverage in `logutil/logutil_test.go` ## Environment Variable - **Variable**: `OLLAMA_LOG_FORMAT` - **Values**: `"json"` for JSON format, unset or any other value for text format (default) - **Case sensitivity**: Case-insensitive (accepts "json", "JSON", "Json", etc.) ## Benefits - Enables structured logging for production deployments - Easier integration with log aggregation tools (ELK, Splunk, CloudWatch, etc.) - Maintains full backward compatibility (default behavior unchanged) - All existing log attributes (level, source, custom fields) work in both formats ## Test Plan All tests pass: ```bash cd logutil && go test -v ``` Tests verify: - ✅ Default text format when env var is unset - ✅ JSON format when `OLLAMA_LOG_FORMAT=json` - ✅ Case-insensitive format detection - ✅ Invalid values default to text format - ✅ TRACE level works in JSON format - ✅ Source field formatting preserved in both formats ## Example Usage **Text format (default):** ```bash ./ollama serve # Output: time=2024-01-01T12:00:00.000Z level=INFO source=server.go:123 msg="server started" ``` **JSON format:** ```bash OLLAMA_LOG_FORMAT=json ./ollama serve # Output: {"time":"2024-01-01T12:00:00.000Z","level":"INFO","source":{"file":"server.go","line":123},"msg":"server started"} ``` Fixes #14018 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 00:57:10 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#14538