[PATCH 1/1] Add support for logging to syslog(3) on operating systems that support it (i.e., non-Windows, non-Plan9).
J.R. Oldroyd
wgrd at opal.com
Tue Mar 17 11:09:57 CET 2020
Signed-off-by: J.R. Oldroyd <wgrd at opal.com>
---
device/logger.go | 45 ++++++----------
device/logger_syslog.go | 112 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 129 insertions(+), 28 deletions(-)
create mode 100644 device/logger_syslog.go
diff --git a/device/logger.go b/device/logger.go
index 7c8b704..8095b3d 100644
--- a/device/logger.go
+++ b/device/logger.go
@@ -1,3 +1,5 @@
+// +build windows,plan9
+
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
@@ -6,7 +8,6 @@
package device
import (
- "io"
"io/ioutil"
"log"
"os"
@@ -26,34 +27,22 @@ type Logger struct {
}
func NewLogger(level int, prepend string) *Logger {
- output := os.Stdout
logger := new(Logger)
- logErr, logInfo, logDebug := func() (io.Writer, io.Writer, io.Writer) {
- if level >= LogLevelDebug {
- return output, output, output
- }
- if level >= LogLevelInfo {
- return output, output, ioutil.Discard
- }
- if level >= LogLevelError {
- return output, ioutil.Discard, ioutil.Discard
- }
- return ioutil.Discard, ioutil.Discard, ioutil.Discard
- }()
-
- logger.Debug = log.New(logDebug,
- "DEBUG: "+prepend,
- log.Ldate|log.Ltime,
- )
-
- logger.Info = log.New(logInfo,
- "INFO: "+prepend,
- log.Ldate|log.Ltime,
- )
- logger.Error = log.New(logErr,
- "ERROR: "+prepend,
- log.Ldate|log.Ltime,
- )
+ nullLog := log.New(ioutil.Discard, "", 0)
+ logger.Debug = nullLog
+ logger.Info = nullLog
+ logger.Error = nullLog
+
+ if level >= LogLevelDebug {
+ logger.Debug = log.New(os.Stdout, "DEBUG: "+prepend, log.Ldate|log.Ltime)
+ }
+ if level >= LogLevelInfo {
+ logger.Info = log.New(os.Stdout, "INFO: "+prepend, log.Ldate|log.Ltime)
+ }
+ if level >= LogLevelError {
+ logger.Error = log.New(os.Stdout, "ERROR: "+prepend, log.Ldate|log.Ltime)
+ }
+
return logger
}
diff --git a/device/logger_syslog.go b/device/logger_syslog.go
new file mode 100644
index 0000000..1271155
--- /dev/null
+++ b/device/logger_syslog.go
@@ -0,0 +1,112 @@
+// +build !windows,!plan9
+
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+import (
+ "io/ioutil"
+ "log"
+ "log/syslog"
+ "os"
+ "strings"
+)
+
+const (
+ LogLevelSilent = iota
+ LogLevelError
+ LogLevelInfo
+ LogLevelDebug
+)
+
+type Logger struct {
+ Debug *log.Logger
+ Info *log.Logger
+ Error *log.Logger
+}
+
+func NewLogger(level int, prepend string) *Logger {
+ logger := new(Logger)
+
+ nullLog := log.New(ioutil.Discard, "", 0)
+ logger.Debug = nullLog
+ logger.Info = nullLog
+ logger.Error = nullLog
+
+ logdest := os.Getenv("WG_LOG_DEST")
+
+ logfacility := syslog.LOG_DAEMON
+ if logdest == "syslog" {
+ facility := os.Getenv("WG_LOG_FACILITY")
+ facility = strings.ToLower(facility)
+ facility = strings.TrimPrefix(facility, "log_")
+ // the commented-out ones exist on BSD but not in Go
+ switch facility {
+ case "auth": logfacility = syslog.LOG_AUTH
+ case "authpriv": logfacility = syslog.LOG_AUTHPRIV
+ //case "console": logfacility = syslog.LOG_CONSOLE
+ case "cron": logfacility = syslog.LOG_CRON
+ case "daemon": logfacility = syslog.LOG_DAEMON
+ case "ftp": logfacility = syslog.LOG_FTP
+ case "kern": logfacility = syslog.LOG_KERN
+ case "local0": logfacility = syslog.LOG_LOCAL0
+ case "local1": logfacility = syslog.LOG_LOCAL1
+ case "local2": logfacility = syslog.LOG_LOCAL2
+ case "local3": logfacility = syslog.LOG_LOCAL3
+ case "local4": logfacility = syslog.LOG_LOCAL4
+ case "local5": logfacility = syslog.LOG_LOCAL5
+ case "local6": logfacility = syslog.LOG_LOCAL6
+ case "local7": logfacility = syslog.LOG_LOCAL7
+ case "lpr": logfacility = syslog.LOG_LPR
+ case "mail": logfacility = syslog.LOG_MAIL
+ //case "ntp": logfacility = syslog.LOG_NTP
+ case "news": logfacility = syslog.LOG_NEWS
+ //case "security": logfacility = syslog.LOG_SECURITY
+ case "syslog": logfacility = syslog.LOG_SYSLOG
+ case "user": logfacility = syslog.LOG_USER
+ case "uucp": logfacility = syslog.LOG_UUCP
+ }
+ }
+
+ if level >= LogLevelDebug {
+ if logdest == "syslog" {
+ sysLog, err := syslog.NewLogger(logfacility | syslog.LOG_DEBUG, 0)
+ if err == nil {
+ logger.Debug = sysLog
+ } else {
+ logger.Debug = log.New(os.Stdout, "DEBUG: "+prepend, log.Ldate|log.Ltime)
+ }
+ } else {
+ logger.Debug = log.New(os.Stdout, "DEBUG: "+prepend, log.Ldate|log.Ltime)
+ }
+ }
+ if level >= LogLevelInfo {
+ if logdest == "syslog" {
+ sysLog, err := syslog.NewLogger(logfacility | syslog.LOG_INFO, 0)
+ if err == nil {
+ logger.Info = sysLog
+ } else {
+ logger.Info = log.New(os.Stdout, "INFO: "+prepend, log.Ldate|log.Ltime)
+ }
+ } else {
+ logger.Info = log.New(os.Stdout, "INFO: "+prepend, log.Ldate|log.Ltime)
+ }
+ }
+ if level >= LogLevelError {
+ if logdest == "syslog" {
+ sysLog, err := syslog.NewLogger(logfacility | syslog.LOG_ERR, 0)
+ if err == nil {
+ logger.Error = sysLog
+ } else {
+ logger.Error = log.New(os.Stdout, "ERROR: "+prepend, log.Ldate|log.Ltime)
+ }
+ } else {
+ logger.Error = log.New(os.Stdout, "ERROR: "+prepend, log.Ldate|log.Ltime)
+ }
+ }
+
+ return logger
+}
--
2.24.0
More information about the WireGuard
mailing list