forked from nats-io/nats-server
/
log.go
119 lines (102 loc) · 2.15 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
118
119
// Copyright 2012-2013 Apcera Inc. All rights reserved.
package server
import (
"fmt"
"log"
"os"
"strings"
"sync/atomic"
)
// logging functionality, compatible with the original nats-server.
var trace int32
var debug int32
var nolog int32
// LogSetup will properly setup logging and the logging flags.
func LogSetup() {
log.SetFlags(0)
atomic.StoreInt32(&nolog, 0)
atomic.StoreInt32(&debug, 0)
atomic.StoreInt32(&trace, 0)
}
// LogInit parses option flags and sets up logging.
func (s *Server) LogInit() {
// Reset
LogSetup()
if s.opts.Logtime {
log.SetFlags(log.LstdFlags)
}
if s.opts.NoLog {
atomic.StoreInt32(&nolog, 1)
}
if s.opts.LogFile != "" {
flags := os.O_WRONLY | os.O_APPEND | os.O_CREATE
file, err := os.OpenFile(s.opts.LogFile, flags, 0660)
if err != nil {
PrintAndDie(fmt.Sprintf("Error opening logfile: %q", s.opts.LogFile))
}
log.SetOutput(file)
}
if s.opts.Debug {
Log(s.opts)
atomic.StoreInt32(&debug, 1)
Log("DEBUG is on")
}
if s.opts.Trace {
atomic.StoreInt32(&trace, 1)
Log("TRACE is on")
}
}
func alreadyFormatted(s string) bool {
return strings.HasPrefix(s, "[")
}
func logStr(v []interface{}) string {
args := make([]string, 0, len(v))
for _, vt := range v {
switch t := vt.(type) {
case string:
if alreadyFormatted(t) {
args = append(args, t)
} else {
t = strings.Replace(t, "\"", "\\\"", -1)
args = append(args, fmt.Sprintf("\"%s\"", t))
}
default:
args = append(args, fmt.Sprintf("%+v", vt))
}
}
return fmt.Sprintf("[%s]", strings.Join(args, ", "))
}
func Log(v ...interface{}) {
if nolog == 0 {
log.Print(logStr(v))
}
}
func Logf(format string, v ...interface{}) {
Log(fmt.Sprintf(format, v...))
}
func Fatal(v ...interface{}) {
log.Fatalf(logStr(v))
}
func Fatalf(format string, v ...interface{}) {
Fatal(fmt.Sprintf(format, v...))
}
func Debug(v ...interface{}) {
if debug > 0 {
Log(v...)
}
}
func Debugf(format string, v ...interface{}) {
if debug > 0 {
Debug(fmt.Sprintf(format, v...))
}
}
func Trace(v ...interface{}) {
if trace > 0 {
Log(v...)
}
}
func Tracef(format string, v ...interface{}) {
if trace > 0 {
Trace(fmt.Sprintf(format, v...))
}
}