2023-12-27 00:03:45 +00:00
|
|
|
package lifecycle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-06-15 23:30:37 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-05-04 18:46:01 +00:00
|
|
|
|
2024-05-24 21:57:15 +00:00
|
|
|
"github.com/ollama/ollama/envconfig"
|
2023-12-27 00:03:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitLogging() {
|
|
|
|
level := slog.LevelInfo
|
|
|
|
|
2024-05-04 18:46:01 +00:00
|
|
|
if envconfig.Debug {
|
2023-12-27 00:03:45 +00:00
|
|
|
level = slog.LevelDebug
|
|
|
|
}
|
|
|
|
|
|
|
|
var logFile *os.File
|
|
|
|
var err error
|
|
|
|
// Detect if we're a GUI app on windows, and if not, send logs to console
|
|
|
|
if os.Stderr.Fd() != 0 {
|
|
|
|
// Console app detected
|
|
|
|
logFile = os.Stderr
|
|
|
|
// TODO - write one-line to the app.log file saying we're running in console mode to help avoid confusion
|
|
|
|
} else {
|
2024-06-15 23:30:37 +00:00
|
|
|
rotateLogs(AppLogFile)
|
2023-12-27 00:03:45 +00:00
|
|
|
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))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler := slog.NewTextHandler(logFile, &slog.HandlerOptions{
|
|
|
|
Level: level,
|
|
|
|
AddSource: true,
|
|
|
|
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
|
|
|
|
if attr.Key == slog.SourceKey {
|
|
|
|
source := attr.Value.Any().(*slog.Source)
|
|
|
|
source.File = filepath.Base(source.File)
|
|
|
|
}
|
|
|
|
return attr
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
slog.SetDefault(slog.New(handler))
|
|
|
|
|
|
|
|
slog.Info("ollama app started")
|
|
|
|
}
|
2024-06-15 23:30:37 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|