-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog.go
83 lines (74 loc) · 2.13 KB
/
log.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
/*
Hockeypuck - OpenPGP key server
Copyright (C) 2012-2014 Casey Marshall
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package hockeypuck
import (
"io"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
)
// Logfile option
func (s *Settings) LogFile() string {
return s.GetString("hockeypuck.logfile")
}
var logOut io.Writer = nil
// InitLog initializes the logging output to the globally configured settings.
// It also registers SIGHUP, SIGUSR1 and SIGUSR2 to close and reopen the log file
// for logrotate(8) support.
//
// BUG: If InitLog is called before the application is properly configured, it will automatically
// configure the application with an empty TOML (accept all defaults).
func InitLog() {
if Config() == nil {
SetConfig("")
}
if Config().LogFile() != "" {
// Handle signals for log rotation
sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGUSR2)
go func() {
for {
select {
case _ = <-sigChan:
closeable, canClose := logOut.(io.WriteCloser)
openLog()
if canClose {
closeable.Close()
}
log.Println("Reopened logfile")
}
}
}()
}
// Open the log
openLog()
}
func openLog() {
if Config().LogFile() != "" {
var err error
logOut, err = os.OpenFile(Config().LogFile(), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Println("Failed to open logfile", err)
logOut = os.Stderr
} else {
log.SetOutput(logOut)
}
} else {
log.SetOutput(os.Stderr)
}
log.SetPrefix(filepath.Base(os.Args[0]))
log.SetFlags(log.LstdFlags | log.Lshortfile)
}