-
Notifications
You must be signed in to change notification settings - Fork 84
/
log.go
83 lines (73 loc) · 2.66 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
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package main
import (
"fmt"
"os"
"path/filepath"
"decred.org/dcrdex/dex"
"github.com/jrick/logrotate/rotator"
)
// logWriter implements an io.Writer that outputs to both standard output and
// the write-end pipe of an initialized log rotator.
type logWriter struct{}
// Write writes the data in p to standard out and the log rotator.
func (logWriter) Write(p []byte) (n int, err error) {
if logRotator == nil {
return os.Stdout.Write(p)
}
os.Stdout.Write(p)
return logRotator.Write(p) // not safe concurrent writes, so only one logWriter{} allowed!
}
// Loggers per subsystem. A single backend logger is created and all subsystem
// loggers created from it will write to the backend. When adding new
// subsystems, define it in the subsystemLoggers map.
//
// For packages with package-level loggers, subsystem logging calls should not
// be done before actually setting the logger in parseAndSetDebugLevels.
//
// Loggers should not be used before the log rotator has been initialized with a
// log file. This must be performed early during application startup by calling
// initLogRotator.
var (
// logRotator is one of the logging outputs. Use initLogRotator to set it.
// It should be closed on application shutdown.
logRotator *rotator.Rotator
// package main's Logger.
log = dex.Disabled
// subsystemLoggers maps each subsystem identifier to its associated logger.
// The loggers are disabled until parseAndSetDebugLevels is called.
subsystemLoggers = map[string]dex.Logger{
"MAIN": dex.Disabled,
"DEX": dex.Disabled,
"DB": dex.Disabled,
"COMM": dex.Disabled,
"AUTH": dex.Disabled,
"SWAP": dex.Disabled,
"MKT": dex.Disabled,
"BOOK": dex.Disabled,
"MTCH": dex.Disabled,
"WAIT": dex.Disabled,
"ADMN": dex.Disabled,
// Individual assets get their own subsystem loggers. This is here to
// register the ASSET subsystem ID, allowing the user to set the log
// level for the asset subsystems.
"ASSET": dex.Disabled,
}
)
// initLogRotator initializes the logging rotater to write logs to logFile and
// create roll files in the same directory. It must be called before the
// package-global log rotater variables are used.
func initLogRotator(logFile string, maxRolls int) {
logDir, _ := filepath.Split(logFile)
err := os.MkdirAll(logDir, 0700)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err)
os.Exit(1)
}
logRotator, err = rotator.New(logFile, 32*1024, false, maxRolls)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
os.Exit(1)
}
}