-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
logs.go
110 lines (94 loc) · 2.99 KB
/
logs.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
package dnclient
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/signal"
"syscall"
"time"
"golift.io/rotatorr"
"golift.io/rotatorr/timerotator"
)
// satisfy gomnd.
const (
helpLink = "GoLift Discord: https://golift.io/discord" // prints on start and on exit.
callDepth = 2 // log the line that called us.
megabyte = 1024 * 1024
)
// Debugf writes Debug log lines... to stdout and/or a file.
func (l *Logger) Debugf(msg string, v ...interface{}) {
if l.debug {
err := l.Logger.Output(callDepth, "[DEBUG] "+fmt.Sprintf(msg, v...))
if err != nil {
fmt.Println("Logger Error:", err)
}
}
}
// Print writes log lines... to stdout and/or a file.
func (l *Logger) Print(v ...interface{}) {
err := l.Logger.Output(callDepth, fmt.Sprintln(v...))
if err != nil {
fmt.Println("Logger Error:", err)
}
}
// Printf writes log lines... to stdout and/or a file.
func (l *Logger) Printf(msg string, v ...interface{}) {
err := l.Logger.Output(callDepth, fmt.Sprintf(msg, v...))
if err != nil {
fmt.Println("Logger Error:", err)
}
}
// setupLogging splits log write into a file and/or stdout.
func (c *Client) setupLogging() {
c.Logger = &Logger{
debug: c.Config.Debug,
Logger: log.New(ioutil.Discard, "", log.LstdFlags),
}
if c.Config.Debug {
c.Logger.Logger.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
}
rotate := &rotatorr.Config{
Filepath: c.Config.LogFile, // log file name.
FileSize: int64(c.Config.LogFileMb) * megabyte, // megabytes
Rotatorr: &timerotator.Layout{FileCount: c.Config.LogFiles}, // number of files to keep.
}
switch { // only use MultiWriter if we have > 1 writer.
case !c.Config.Quiet && c.Config.LogFile != "":
c.Logger.Logger.SetOutput(io.MultiWriter(rotatorr.NewMust(rotate), os.Stdout))
case !c.Config.Quiet && c.Config.LogFile == "":
c.Logger.Logger.SetOutput(os.Stdout)
case c.Config.LogFile == "":
c.Logger.Logger.SetOutput(ioutil.Discard) // default is "nothing"
default:
c.Logger.Logger.SetOutput(rotatorr.NewMust(rotate))
}
}
// logStartupInfo prints info about our startup config.
func (c *Client) logStartupInfo() {
c.Printf("==> %s <==", helpLink)
c.Print("==> Startup Settings <==")
c.logSonarr()
c.logRadarr()
c.logLidarr()
c.logReadarr()
c.Print(" => Debug / Quiet:", c.Config.Debug, "/", c.Config.Quiet)
c.Print(" => Web HTTP Listen:", c.Config.BindAddr)
if c.Config.LogFile != "" {
msg := "no rotation"
if c.Config.LogFiles > 0 {
msg = fmt.Sprintf("%d @ %dMb", c.Config.LogFiles, c.Config.LogFileMb)
}
c.Printf(" => Log File: %s (%s)", c.Config.LogFile, msg)
}
}
func (c *Client) logExitInfo() error {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
c.Printf("[%s] Need help? %s\n=====> Exiting! Caught Signal: %v", c.Flags.Name(), helpLink, <-sig)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
return c.server.Shutdown(ctx)
}