- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathsyslog.go
132 lines (113 loc) · 3.27 KB
/
syslog.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
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2012-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !windows
package logger
import (
"fmt"
"log"
"log/syslog"
"net/url"
"os"
"strings"
)
// SysLogger provides a system logger facility
typeSysLoggerstruct {
writer*syslog.Writer
debugbool
tracebool
}
// SetSyslogName sets the name to use for the syslog.
// Currently used only on Windows.
funcSetSyslogName(namestring) {}
// GetSysLoggerTag generates the tag name for use in syslog statements. If
// the executable is linked, the name of the link will be used as the tag,
// otherwise, the name of the executable is used. "nats-server" is the default
// for the NATS server.
funcGetSysLoggerTag() string {
procName:=os.Args[0]
ifstrings.ContainsRune(procName, os.PathSeparator) {
parts:=strings.FieldsFunc(procName, func(crune) bool {
returnc==os.PathSeparator
})
procName=parts[len(parts)-1]
}
returnprocName
}
// NewSysLogger creates a new system logger
funcNewSysLogger(debug, tracebool) *SysLogger {
w, err:=syslog.New(syslog.LOG_DAEMON|syslog.LOG_NOTICE, GetSysLoggerTag())
iferr!=nil {
log.Fatalf("error connecting to syslog: %q", err.Error())
}
return&SysLogger{
writer: w,
debug: debug,
trace: trace,
}
}
// NewRemoteSysLogger creates a new remote system logger
funcNewRemoteSysLogger(fqnstring, debug, tracebool) *SysLogger {
network, addr:=getNetworkAndAddr(fqn)
w, err:=syslog.Dial(network, addr, syslog.LOG_DEBUG, GetSysLoggerTag())
iferr!=nil {
log.Fatalf("error connecting to syslog: %q", err.Error())
}
return&SysLogger{
writer: w,
debug: debug,
trace: trace,
}
}
funcgetNetworkAndAddr(fqnstring) (network, addrstring) {
u, err:=url.Parse(fqn)
iferr!=nil {
log.Fatal(err)
}
network=u.Scheme
ifnetwork=="udp"||network=="tcp" {
addr=u.Host
} elseifnetwork=="unix" {
addr=u.Path
} else {
log.Fatalf("error invalid network type: %q", u.Scheme)
}
return
}
// Noticef logs a notice statement
func (l*SysLogger) Noticef(formatstring, v...any) {
l.writer.Notice(fmt.Sprintf(format, v...))
}
// Warnf logs a warning statement
func (l*SysLogger) Warnf(formatstring, v...any) {
l.writer.Warning(fmt.Sprintf(format, v...))
}
// Fatalf logs a fatal error
func (l*SysLogger) Fatalf(formatstring, v...any) {
l.writer.Crit(fmt.Sprintf(format, v...))
}
// Errorf logs an error statement
func (l*SysLogger) Errorf(formatstring, v...any) {
l.writer.Err(fmt.Sprintf(format, v...))
}
// Debugf logs a debug statement
func (l*SysLogger) Debugf(formatstring, v...any) {
ifl.debug {
l.writer.Debug(fmt.Sprintf(format, v...))
}
}
// Tracef logs a trace statement
func (l*SysLogger) Tracef(formatstring, v...any) {
ifl.trace {
l.writer.Notice(fmt.Sprintf(format, v...))
}
}