Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

Implement log rotation for tray app

Daniel Hiltgen9d8a4988e8eb
diff --git a/app/lifecycle/logging.go b/app/lifecycle/logging.go
index df2597a83..a8f1f7cdf 100644
--- a/app/lifecycle/logging.go
+++ b/app/lifecycle/logging.go
@@ -5,6 +5,8 @@ import (
"log/slog"
"os"
"path/filepath"
+ "strconv"
+ "strings"
"github.com/ollama/ollama/envconfig"
)
@@ -24,6 +26,7 @@ func InitLogging() {
logFile = os.Stderr
// TODO - write one-line to the app.log file saying we're running in console mode to help avoid confusion
} else {
+ rotateLogs(AppLogFile)
logFile, err = os.OpenFile(AppLogFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
slog.Error(fmt.Sprintf("failed to create server log %v", err))
@@ -46,3 +49,32 @@ func InitLogging() {
slog.Info("ollama app started")
}
+
+func rotateLogs(logFile string) {
+ if _, err := os.Stat(logFile); os.IsNotExist(err) {
+ return
+ }
+ index := strings.LastIndex(logFile, ".")
+ pre := logFile[:index]
+ post := "." + logFile[index+1:]
+ for i := LogRotationCount; i > 0; i-- {
+ older := pre + "-" + strconv.Itoa(i) + post
+ newer := pre + "-" + strconv.Itoa(i-1) + post
+ if i == 1 {
+ newer = pre + post
+ }
+ if _, err := os.Stat(newer); err == nil {
+ if _, err := os.Stat(older); err == nil {
+ err := os.Remove(older)
+ if err != nil {
+ slog.Warn("Failed to remove older log", "older", older, "error", err)
+ continue
+ }
+ }
+ err := os.Rename(newer, older)
+ if err != nil {
+ slog.Warn("Failed to rotate log", "older", older, "newer", newer, "error", err)
+ }
+ }
+ }
+}
diff --git a/app/lifecycle/logging_test.go b/app/lifecycle/logging_test.go
new file mode 100644
index 000000000..a2157ca2c
--- /dev/null
+++ b/app/lifecycle/logging_test.go
@@ -0,0 +1,44 @@
+package lifecycle
+
+import (
+ "os"
+ "path/filepath"
+ "strconv"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestRotateLogs(t *testing.T) {
+ logDir := t.TempDir()
+ logFile := filepath.Join(logDir, "testlog.log")
+
+ // No log exists
+ rotateLogs(logFile)
+
+ require.NoError(t, os.WriteFile(logFile, []byte("1"), 0644))
+ assert.FileExists(t, logFile)
+ // First rotation
+ rotateLogs(logFile)
+ assert.FileExists(t, filepath.Join(logDir, "testlog-1.log"))
+ assert.NoFileExists(t, filepath.Join(logDir, "testlog-2.log"))
+ assert.NoFileExists(t, logFile)
+
+ // Should be a no-op without a new log
+ rotateLogs(logFile)
+ assert.FileExists(t, filepath.Join(logDir, "testlog-1.log"))
+ assert.NoFileExists(t, filepath.Join(logDir, "testlog-2.log"))
+ assert.NoFileExists(t, logFile)
+
+ for i := 2; i <= LogRotationCount+1; i++ {
+ require.NoError(t, os.WriteFile(logFile, []byte(strconv.Itoa(i)), 0644))
+ assert.FileExists(t, logFile)
+ rotateLogs(logFile)
+ assert.NoFileExists(t, logFile)
+ for j := 1; j < i; j++ {
+ assert.FileExists(t, filepath.Join(logDir, "testlog-"+strconv.Itoa(j)+".log"))
+ }
+ assert.NoFileExists(t, filepath.Join(logDir, "testlog-"+strconv.Itoa(i+1)+".log"))
+ }
+}
diff --git a/app/lifecycle/paths.go b/app/lifecycle/paths.go
index fe07bce10..4d9f4c5a1 100644
--- a/app/lifecycle/paths.go
+++ b/app/lifecycle/paths.go
@@ -16,11 +16,12 @@ var (
AppDir = "/opt/Ollama"
AppDataDir = "/opt/Ollama"
// TODO - should there be a distinct log dir?
- UpdateStageDir = "/tmp"
- AppLogFile = "/tmp/ollama_app.log"
- ServerLogFile = "/tmp/ollama.log"
- UpgradeLogFile = "/tmp/ollama_update.log"
- Installer = "OllamaSetup.exe"
+ UpdateStageDir = "/tmp"
+ AppLogFile = "/tmp/ollama_app.log"
+ ServerLogFile = "/tmp/ollama.log"
+ UpgradeLogFile = "/tmp/ollama_update.log"
+ Installer = "OllamaSetup.exe"
+ LogRotationCount = 5
)
func init() {
diff --git a/app/lifecycle/server.go b/app/lifecycle/server.go
index 0152ccd11..c178a1abf 100644
--- a/app/lifecycle/server.go
+++ b/app/lifecycle/server.go
@@ -54,7 +54,7 @@ func start(ctx context.Context, command string) (*exec.Cmd, error) {
return nil, fmt.Errorf("failed to spawn server stderr pipe: %w", err)
}
- // TODO - rotation
+ rotateLogs(ServerLogFile)
logFile, err := os.OpenFile(ServerLogFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return nil, fmt.Errorf("failed to create server log: %w", err)
diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md
index 60d63c7d9..de29b344c 100644
--- a/docs/troubleshooting.md
+++ b/docs/troubleshooting.md
@@ -22,7 +22,7 @@ docker logs <container-name>
If manually running `ollama serve` in a terminal, the logs will be on that terminal.
When you run Ollama on **Windows**, there are a few different locations. You can view them in the explorer window by hitting `<cmd>+R` and type in:
-- `explorer %LOCALAPPDATA%\Ollama` to view logs
+- `explorer %LOCALAPPDATA%\Ollama` to view logs. The most recent server logs will be in `server.log` and older logs will be in `server-#.log`
- `explorer %LOCALAPPDATA%\Programs\Ollama` to browse the binaries (The installer adds this to your user PATH)
- `explorer %HOMEPATH%\.ollama` to browse where models and configuration is stored
- `explorer %TEMP%` where temporary executable files are stored in one or more `ollama*` directories
diff --git a/docs/windows.md b/docs/windows.md
index 832b3d431..abc0eb300 100644
--- a/docs/windows.md
+++ b/docs/windows.md
@@ -39,8 +39,8 @@ server.
Ollama on Windows stores files in a few different locations. You can view them in
the explorer window by hitting `<cmd>+R` and type in:
- `explorer %LOCALAPPDATA%\Ollama` contains logs, and downloaded updates
- - *app.log* contains logs from the GUI application
- - *server.log* contains the server logs
+ - *app.log* contains most resent logs from the GUI application
+ - *server.log* contains the most recent server logs
- *upgrade.log* contains log output for upgrades
- `explorer %LOCALAPPDATA%\Programs\Ollama` contains the binaries (The installer adds this to your user PATH)
- `explorer %HOMEPATH%\.ollama` contains models and configuration