-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.go
77 lines (63 loc) · 1.62 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
package main
import (
"P2P-Crypto-Chat/p2p"
"P2P-Crypto-Chat/peer"
"fmt"
"github.com/btcsuite/btclog"
"github.com/jrick/logrotate/rotator"
"os"
"path/filepath"
)
type logWriter struct{}
func (logWriter) Write(p []byte) (n int, err error) {
_, _ = os.Stdout.Write(p)
_, _ = logRotator.Write(p)
return len(p), nil
}
var (
backendLog = btclog.NewBackend(logWriter{}, btclog.WithFlags(btclog.Lshortfile))//增加文件名和行号
logRotator *rotator.Rotator
log = backendLog.Logger("MAIN")
p2pLogger = backendLog.Logger("P2P")
peerLogger = backendLog.Logger("PEER")
)
func init() {
p2p.UseLogger(p2pLogger)
peer.UseLogger(peerLogger)
}
var subsystemLoggers = map[string]btclog.Logger{
"PEER": peerLogger,
"P2P": p2pLogger,
"MAIN": log,
}
func initLogRotator(logFile string) {
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)
}
r, err := rotator.New(logFile, 10*1024, false, 3)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
os.Exit(1)
}
logRotator = r
}
func setLogLevels(logLevel string) {
// Configure all sub-systems with the new logging level. Dynamically
// create loggers as needed.
for subsystemID := range subsystemLoggers {
setLogLevel(subsystemID, logLevel)
}
}
func setLogLevel(subsystemID string, logLevel string) {
// Ignore invalid subsystems.
logger, ok := subsystemLoggers[subsystemID]
if !ok {
return
}
// Defaults to info if the log level is invalid.
level, _ := btclog.LevelFromString(logLevel)
logger.SetLevel(level)
}