-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
logging.go
73 lines (61 loc) · 1.65 KB
/
logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package logging
import (
"os"
"path/filepath"
"strings"
"github.com/natefinch/lumberjack"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
DebugLogger zerolog.Logger
InfoLogger zerolog.Logger
ErrorLogger zerolog.Logger
)
func Init(logLevel string, logFilePath string) error {
// Set default log file path if none is provided
if logFilePath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
logFilePath = filepath.Join(homeDir, ".config", "gollama", "gollama.log")
}
// Expand the ~ to the user's home directory
if strings.HasPrefix(logFilePath, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
logFilePath = filepath.Join(homeDir, logFilePath[1:])
}
// Create the directory if it doesn't exist
if err := os.MkdirAll(filepath.Dir(logFilePath), 0755); err != nil {
return err
}
// Configure log rotation with lumberjack
rotate := &lumberjack.Logger{
Filename: logFilePath,
MaxSize: 2, // megabytes
MaxBackups: 3, // number of files
MaxAge: 60, // days
Compress: false, // disabled by default
}
// Set the log level
level, err := zerolog.ParseLevel(logLevel)
if err != nil {
return err
}
zerolog.SetGlobalLevel(level)
// Use lumberjack for logging to a file
fileWriter := zerolog.MultiLevelWriter(rotate)
// Initialize loggers
log.Logger = zerolog.New(fileWriter).With().Timestamp().Logger()
DebugLogger = log.Logger.Level(zerolog.DebugLevel)
InfoLogger = log.Logger.Level(zerolog.InfoLevel)
ErrorLogger = log.Logger.Level(zerolog.ErrorLevel)
if logLevel == "debug" {
DebugLogger.Printf("Logging to: %s\n", logFilePath)
}
return nil
}