mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Generate syslog tag based on executable/link name
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
||||
"log"
|
||||
"log/syslog"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SysLogger provides a system logger facility
|
||||
@@ -18,9 +20,24 @@ type SysLogger struct {
|
||||
trace bool
|
||||
}
|
||||
|
||||
// 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. "gnatsd" is the default
|
||||
// for the NATS server.
|
||||
func GetSysLoggerTag() string {
|
||||
procName := os.Args[0]
|
||||
if strings.ContainsRune(procName, os.PathSeparator) {
|
||||
parts := strings.FieldsFunc(procName, func(c rune) bool {
|
||||
return c == os.PathSeparator
|
||||
})
|
||||
procName = parts[len(parts)-1]
|
||||
}
|
||||
return procName
|
||||
}
|
||||
|
||||
// NewSysLogger creates a new system logger
|
||||
func NewSysLogger(debug, trace bool) *SysLogger {
|
||||
w, err := syslog.New(syslog.LOG_DAEMON|syslog.LOG_NOTICE, "gnatsd")
|
||||
w, err := syslog.New(syslog.LOG_DAEMON|syslog.LOG_NOTICE, GetSysLoggerTag())
|
||||
if err != nil {
|
||||
log.Fatalf("error connecting to syslog: %q", err.Error())
|
||||
}
|
||||
@@ -35,7 +52,7 @@ func NewSysLogger(debug, trace bool) *SysLogger {
|
||||
// NewRemoteSysLogger creates a new remote system logger
|
||||
func NewRemoteSysLogger(fqn string, debug, trace bool) *SysLogger {
|
||||
network, addr := getNetworkAndAddr(fqn)
|
||||
w, err := syslog.Dial(network, addr, syslog.LOG_DEBUG, "gnatsd")
|
||||
w, err := syslog.Dial(network, addr, syslog.LOG_DEBUG, GetSysLoggerTag())
|
||||
if err != nil {
|
||||
log.Fatalf("error connecting to syslog: %q", err.Error())
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -37,6 +39,53 @@ func TestSysLoggerWithDebugAndTrace(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testTag(t *testing.T, exePath, expected string) {
|
||||
os.Args[0] = exePath
|
||||
if result := GetSysLoggerTag(); result != expected {
|
||||
t.Fatalf("Expected %s, received %s", expected, result)
|
||||
}
|
||||
}
|
||||
|
||||
func restoreArg(orig string) {
|
||||
os.Args[0] = orig
|
||||
}
|
||||
|
||||
func TestSysLoggerTagGen(t *testing.T) {
|
||||
origArg := os.Args[0]
|
||||
defer restoreArg(origArg)
|
||||
|
||||
testTag(t, "gnatsd", "gnatsd")
|
||||
testTag(t, filepath.Join(".", "gnatsd"), "gnatsd")
|
||||
testTag(t, filepath.Join("home", "bin", "gnatsd"), "gnatsd")
|
||||
testTag(t, filepath.Join("..", "..", "gnatsd"), "gnatsd")
|
||||
testTag(t, "gnatsd.service1", "gnatsd.service1")
|
||||
testTag(t, "gnatsd_service1", "gnatsd_service1")
|
||||
testTag(t, "gnatsd-service1", "gnatsd-service1")
|
||||
testTag(t, "gnatsd service1", "gnatsd service1")
|
||||
}
|
||||
|
||||
func TestSysLoggerTag(t *testing.T) {
|
||||
origArg := os.Args[0]
|
||||
defer restoreArg(origArg)
|
||||
|
||||
os.Args[0] = "ServerLoggerTag"
|
||||
|
||||
done := make(chan string)
|
||||
startServer(done)
|
||||
logger := NewRemoteSysLogger(serverFQN, true, true)
|
||||
logger.Noticef("foo")
|
||||
|
||||
line := <-done
|
||||
data := strings.Split(line, "[")
|
||||
if len(data) != 2 {
|
||||
t.Fatalf("Unexpected syslog line %s\n", line)
|
||||
}
|
||||
|
||||
if !strings.Contains(data[0], os.Args[0]) {
|
||||
t.Fatalf("Expected '%s', received '%s'\n", os.Args[0], data[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteSysLogger(t *testing.T) {
|
||||
done := make(chan string)
|
||||
startServer(done)
|
||||
|
||||
Reference in New Issue
Block a user