-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
110 lines (94 loc) · 2.76 KB
/
logger.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//go:build production
package main
import (
"errors"
"io"
"os"
"path/filepath"
"runtime"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
const logFileName = "run.log"
// Will configure log.SetOutput with the default user-specific logs directory, else /var/log/syslog
// Logs, if as files, will be rotated
func init() {
log.SetLevel(log.TraceLevel) // default to trace level
var outputs = []io.Writer{log.StandardLogger().Out}
// get the system user log directory
logDir, err := UserLogDir()
log.Traceln("user log directory:", logDir)
switch err == nil {
case true:
// We have the system user log directory, use it
rotateLogger := configureRotateLogger(logDir, AppDisplayName)
outputs = append(outputs, rotateLogger)
case false:
// Error, use the system log
configureSyslogLogger()
}
log.SetOutput(io.MultiWriter(outputs...))
}
// configureRotateLogger return a lumberjack.Logger to use as log.SetOutput
func configureRotateLogger(path string, name string) *lumberjack.Logger {
// Make a folder into if not exist
appLogDir := filepath.Join(path, name)
if _, err := os.Stat(appLogDir); errors.Is(err, os.ErrNotExist) {
_ = os.Mkdir(appLogDir, 0755)
}
file := filepath.Join(appLogDir, logFileName)
log.Printf("using log file: %s", file)
return &lumberjack.Logger{
Filename: file,
MaxSize: 5, // megabytes
MaxBackups: 2,
MaxAge: 2, //days
LocalTime: true,
Compress: false, // disabled by default
}
}
// UserLogDir returns the default root directory to use for user-specific
// logs file. Users should create their own Application-specific subdirectory
// within this one and use that.
//
// On Unix systems, it returns $XDG_CACHE_HOME as specified by
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if
// non-empty, else $HOME/.logs.
// On Darwin, it returns $HOME/Library/Logs.
// On Windows, it returns %LocalAppData%.
// On Plan 9, it returns $home/lib/logs.
//
// If the location cannot be determined (for example, $HOME is not defined),
// then it will return an error.
func UserLogDir() (string, error) {
var dir string
switch runtime.GOOS {
case "windows":
dir = os.Getenv("LocalAppData")
if dir == "" {
return "", errors.New("%LocalAppData% is not defined")
}
case "darwin", "ios":
dir = os.Getenv("HOME")
if dir == "" {
return "", errors.New("$HOME is not defined")
}
dir += "/Library/Logs"
case "plan9":
dir = os.Getenv("home")
if dir == "" {
return "", errors.New("$home is not defined")
}
dir += "/lib/logs"
default: // Unix
dir = os.Getenv("XDG_STATE_HOME")
if dir == "" {
dir = os.Getenv("HOME")
if dir == "" {
return "", errors.New("neither $XDG_STATE_HOME nor $HOME are defined")
}
dir += "/.logs"
}
}
return dir, nil
}