forked from evolutek/cellaserv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
117 lines (102 loc) · 3.02 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
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
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"flag"
"github.com/op/go-logging"
golog "log"
"os"
"path"
"time"
)
var (
// log is the main cellaserv logger, use it everywhere you want!
log *logging.Logger
// Default log level
logLevel = logging.WARNING
// Command line flags
logRootDirectory = flag.String("log-root", ".", "root directory of logs")
logSubDir string
logLevelFlag = flag.String("log-level", "", "logger verbosity")
logToFile = flag.String("log-file", "", "log to custom file instead of stderr")
// Map of the logger associated with a service
servicesLogs map[string]*golog.Logger
)
// Setup that must be done before any log is made. Command line arguments parsing must be done
// before calling logPreSetup()
func logPreSetup() {
format := logging.MustStringFormatter("%{level:-7s} %{time:Jan _2 15:04:05.000} %{message}")
var logBackend *logging.LogBackend
if *logToFile != "" {
// Use has specified a log file to use
logFile, err := os.OpenFile(*logToFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
golog.Println(err)
golog.Println("Falling back on log on stderr")
logBackend = logging.NewLogBackend(os.Stderr, "", 0)
} else {
logBackend = logging.NewLogBackend(logFile, "", 0)
}
} else {
// Log on stderr
logBackend = logging.NewLogBackend(os.Stderr, "", 0)
}
logBackend.Color = true
logging.SetBackend(logBackend)
logging.SetFormatter(format)
log = logging.MustGetLogger("cellaserv")
}
func logSetup() {
logging.SetLevel(logLevel, "cellaserv")
// Set default log subDirectory to now
logRotateTimeNow()
}
// logRotateName set the new log subdirectory to name
func logRotateName(name string) {
log.Debug("[Log] Rotating to \"%s\"", name)
logSubDir = name
logFullDir := path.Join(*logRootDirectory, logSubDir)
err := os.MkdirAll(logFullDir, 0755)
if err != nil {
log.Error("[Log] Could not create log directories, %s: %s", logFullDir, err)
}
// XXX: close old log files?
servicesLogs = make(map[string]*golog.Logger)
pub_data, err := json.Marshal(logSubDir)
if err != nil {
log.Error("[Publish] Could not publish new log session, json error: %s: %s",
logSubDir, err)
}
cellaservPublish(logNewLogSession, pub_data)
}
// logRotateTimeNow switch the current log subdirectory to current time
func logRotateTimeNow() {
now := time.Now()
newSubDir := now.Format(time.Stamp)
logRotateName(newSubDir)
}
func logSetupFile(what string) (l *golog.Logger) {
l, ok := servicesLogs[what]
if !ok {
logFilename := path.Join(*logRootDirectory, logSubDir, what+".log")
logFd, err := os.OpenFile(logFilename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
log.Error("[Log] Could not create log file: %s", logFilename)
return
}
l = golog.New(logFd, what, golog.LstdFlags)
l.SetPrefix("")
servicesLogs[what] = l
}
return
}
func logEvent(event string, what string) {
logger, ok := servicesLogs[event]
if !ok {
logger = logSetupFile(event)
if logger == nil {
return
}
}
logger.Println(what)
}
// vim: set nowrap tw=100 noet sw=8: