From 93a323c62c52b156207922414e31a1890d235d26 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 28 Jun 2019 23:45:04 +0100 Subject: [PATCH 1/2] Add support for logging to file or syslog instead of stdout --- cmd/yggdrasil/main.go | 20 +++++++++++++++++++- src/yggdrasil/api.go | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index fb6cccbd..32069c01 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -7,6 +7,7 @@ import ( "flag" "fmt" "io/ioutil" + "log/syslog" "os" "os/signal" "strings" @@ -114,6 +115,7 @@ func main() { autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") version := flag.Bool("version", false, "prints the version of this build") logging := flag.String("logging", "info,warn,error", "comma-separated list of logging levels to enable") + logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"") flag.Parse() var cfg *config.NodeConfig @@ -161,7 +163,23 @@ func main() { return } // Create a new logger that logs output to stdout. - logger := log.New(os.Stdout, "", log.Flags()) + var logger *log.Logger + switch *logto { + case "stdout": + logger = log.New(os.Stdout, "", log.Flags()) + case "syslog": + if syslogwriter, err := syslog.New(syslog.LOG_INFO, yggdrasil.BuildName()); err == nil { + logger = log.New(syslogwriter, "", log.Flags()) + } + default: + if logfd, err := os.Create(*logto); err == nil { + logger = log.New(logfd, "", log.Flags()) + } + } + if logger == nil { + logger = log.New(os.Stdout, "", log.Flags()) + logger.Warnln("Logging defaulting to stdout") + } //logger.EnableLevel("error") //logger.EnableLevel("warn") //logger.EnableLevel("info") diff --git a/src/yggdrasil/api.go b/src/yggdrasil/api.go index 25f9869c..462353c0 100644 --- a/src/yggdrasil/api.go +++ b/src/yggdrasil/api.go @@ -232,7 +232,7 @@ func (c *Core) GetSessions() []Session { // from git, or returns "unknown" otherwise. func BuildName() string { if buildName == "" { - return "unknown" + return "yggdrasil" } return buildName } From 23108e268b2888c27d62687a2e8adb0a4bba722a Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 29 Jun 2019 00:32:23 +0100 Subject: [PATCH 2/2] Use go-syslog to fix builds on Windows --- cmd/yggdrasil/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 32069c01..6af27725 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -7,7 +7,6 @@ import ( "flag" "fmt" "io/ioutil" - "log/syslog" "os" "os/signal" "strings" @@ -16,6 +15,7 @@ import ( "golang.org/x/text/encoding/unicode" "github.com/gologme/log" + gsyslog "github.com/hashicorp/go-syslog" "github.com/hjson/hjson-go" "github.com/kardianos/minwinsvc" "github.com/mitchellh/mapstructure" @@ -168,8 +168,8 @@ func main() { case "stdout": logger = log.New(os.Stdout, "", log.Flags()) case "syslog": - if syslogwriter, err := syslog.New(syslog.LOG_INFO, yggdrasil.BuildName()); err == nil { - logger = log.New(syslogwriter, "", log.Flags()) + if syslogger, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "DAEMON", yggdrasil.BuildName()); err == nil { + logger = log.New(syslogger, "", log.Flags()) } default: if logfd, err := os.Create(*logto); err == nil {