Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
97c59be6
Unverified
Commit
97c59be6
authored
Jun 19, 2024
by
Daniel Hiltgen
Committed by
GitHub
Jun 19, 2024
Browse files
Merge pull request #5074 from dhiltgen/app_log_rotation
Implement log rotation for tray app
parents
1ae0750a
9d8a4988
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
86 additions
and
9 deletions
+86
-9
app/lifecycle/logging.go
app/lifecycle/logging.go
+32
-0
app/lifecycle/logging_test.go
app/lifecycle/logging_test.go
+44
-0
app/lifecycle/paths.go
app/lifecycle/paths.go
+6
-5
app/lifecycle/server.go
app/lifecycle/server.go
+1
-1
docs/troubleshooting.md
docs/troubleshooting.md
+1
-1
docs/windows.md
docs/windows.md
+2
-2
No files found.
app/lifecycle/logging.go
View file @
97c59be6
...
@@ -5,6 +5,8 @@ import (
...
@@ -5,6 +5,8 @@ import (
"log/slog"
"log/slog"
"os"
"os"
"path/filepath"
"path/filepath"
"strconv"
"strings"
"github.com/ollama/ollama/envconfig"
"github.com/ollama/ollama/envconfig"
)
)
...
@@ -24,6 +26,7 @@ func InitLogging() {
...
@@ -24,6 +26,7 @@ func InitLogging() {
logFile
=
os
.
Stderr
logFile
=
os
.
Stderr
// TODO - write one-line to the app.log file saying we're running in console mode to help avoid confusion
// TODO - write one-line to the app.log file saying we're running in console mode to help avoid confusion
}
else
{
}
else
{
rotateLogs
(
AppLogFile
)
logFile
,
err
=
os
.
OpenFile
(
AppLogFile
,
os
.
O_APPEND
|
os
.
O_WRONLY
|
os
.
O_CREATE
,
0755
)
logFile
,
err
=
os
.
OpenFile
(
AppLogFile
,
os
.
O_APPEND
|
os
.
O_WRONLY
|
os
.
O_CREATE
,
0755
)
if
err
!=
nil
{
if
err
!=
nil
{
slog
.
Error
(
fmt
.
Sprintf
(
"failed to create server log %v"
,
err
))
slog
.
Error
(
fmt
.
Sprintf
(
"failed to create server log %v"
,
err
))
...
@@ -46,3 +49,32 @@ func InitLogging() {
...
@@ -46,3 +49,32 @@ func InitLogging() {
slog
.
Info
(
"ollama app started"
)
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
)
}
}
}
}
app/lifecycle/logging_test.go
0 → 100644
View file @
97c59be6
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"
))
}
}
app/lifecycle/paths.go
View file @
97c59be6
...
@@ -16,11 +16,12 @@ var (
...
@@ -16,11 +16,12 @@ var (
AppDir
=
"/opt/Ollama"
AppDir
=
"/opt/Ollama"
AppDataDir
=
"/opt/Ollama"
AppDataDir
=
"/opt/Ollama"
// TODO - should there be a distinct log dir?
// TODO - should there be a distinct log dir?
UpdateStageDir
=
"/tmp"
UpdateStageDir
=
"/tmp"
AppLogFile
=
"/tmp/ollama_app.log"
AppLogFile
=
"/tmp/ollama_app.log"
ServerLogFile
=
"/tmp/ollama.log"
ServerLogFile
=
"/tmp/ollama.log"
UpgradeLogFile
=
"/tmp/ollama_update.log"
UpgradeLogFile
=
"/tmp/ollama_update.log"
Installer
=
"OllamaSetup.exe"
Installer
=
"OllamaSetup.exe"
LogRotationCount
=
5
)
)
func
init
()
{
func
init
()
{
...
...
app/lifecycle/server.go
View file @
97c59be6
...
@@ -54,7 +54,7 @@ func start(ctx context.Context, command string) (*exec.Cmd, error) {
...
@@ -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
)
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
)
logFile
,
err
:=
os
.
OpenFile
(
ServerLogFile
,
os
.
O_APPEND
|
os
.
O_WRONLY
|
os
.
O_CREATE
,
0755
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to create server log: %w"
,
err
)
return
nil
,
fmt
.
Errorf
(
"failed to create server log: %w"
,
err
)
...
...
docs/troubleshooting.md
View file @
97c59be6
...
@@ -22,7 +22,7 @@ docker logs <container-name>
...
@@ -22,7 +22,7 @@ docker logs <container-name>
If manually running
`ollama serve`
in a terminal, the logs will be on that terminal.
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:
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 %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 %HOMEPATH%\.ollama`
to browse where models and configuration is stored
-
`explorer %TEMP%`
where temporary executable files are stored in one or more
`ollama*`
directories
-
`explorer %TEMP%`
where temporary executable files are stored in one or more
`ollama*`
directories
...
...
docs/windows.md
View file @
97c59be6
...
@@ -39,8 +39,8 @@ server.
...
@@ -39,8 +39,8 @@ server.
Ollama on Windows stores files in a few different locations. You can view them in
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:
the explorer window by hitting
`<cmd>+R`
and type in:
-
`explorer %LOCALAPPDATA%\Ollama`
contains logs, and downloaded updates
-
`explorer %LOCALAPPDATA%\Ollama`
contains logs, and downloaded updates
-
*app.log*
contains logs from the GUI application
-
*app.log*
contains
most resent
logs from the GUI application
-
*server.log*
contains the server logs
-
*server.log*
contains the
most recent
server logs
-
*upgrade.log*
contains log output for upgrades
-
*upgrade.log*
contains log output for upgrades
-
`explorer %LOCALAPPDATA%\Programs\Ollama`
contains the binaries (The installer adds this to your user PATH)
-
`explorer %LOCALAPPDATA%\Programs\Ollama`
contains the binaries (The installer adds this to your user PATH)
-
`explorer %HOMEPATH%\.ollama`
contains models and configuration
-
`explorer %HOMEPATH%\.ollama`
contains models and configuration
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment