diff --git a/client/cmd/root.go b/client/cmd/root.go index f0b5d2bdf..1e5c56366 100644 --- a/client/cmd/root.go +++ b/client/cmd/root.go @@ -121,7 +121,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&serviceName, "service", "s", defaultServiceName, "Netbird system service name") rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", defaultConfigPath, "Netbird config file location") rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "sets Netbird log level") - rootCmd.PersistentFlags().StringVar(&logFile, "log-file", defaultLogFile, "sets Netbird log path. If console is specified the log will be output to stdout") + rootCmd.PersistentFlags().StringVar(&logFile, "log-file", defaultLogFile, "sets Netbird log path. If console is specified the log will be output to stdout. If syslog is specified the log will be sent to syslog daemon.") rootCmd.PersistentFlags().StringVarP(&setupKey, "setup-key", "k", "", "Setup key obtained from the Management Service Dashboard (used to register peer)") rootCmd.PersistentFlags().StringVar(&preSharedKey, preSharedKeyFlag, "", "Sets Wireguard PreSharedKey property. If set, then only peers that have the same key can communicate.") rootCmd.PersistentFlags().StringVarP(&hostName, "hostname", "n", "", "Sets a custom hostname for the device") diff --git a/util/log.go b/util/log.go index 90ccea48f..11bb7efa7 100644 --- a/util/log.go +++ b/util/log.go @@ -4,6 +4,7 @@ import ( "io" "os" "path/filepath" + "slices" log "github.com/sirupsen/logrus" "gopkg.in/natefinch/lumberjack.v2" @@ -18,8 +19,9 @@ func InitLog(logLevel string, logPath string) error { log.Errorf("Failed parsing log-level %s: %s", logLevel, err) return err } + custom_outputs := []string{"console", "syslog"}; - if logPath != "" && logPath != "console" { + if logPath != "" && !slices.Contains(custom_outputs, logPath) { lumberjackLogger := &lumberjack.Logger{ // Log file absolute path, os agnostic Filename: filepath.ToSlash(logPath), @@ -29,6 +31,8 @@ func InitLog(logLevel string, logPath string) error { Compress: true, } log.SetOutput(io.Writer(lumberjackLogger)) + } else if logPath == "syslog" { + AddSyslogHook() } if os.Getenv("NB_LOG_FORMAT") == "json" { diff --git a/util/syslog_nonwindows.go b/util/syslog_nonwindows.go new file mode 100644 index 000000000..6ffbcb8be --- /dev/null +++ b/util/syslog_nonwindows.go @@ -0,0 +1,20 @@ +//go:build !windows +// +build !windows + +package util + +import ( + "log/syslog" + + log "github.com/sirupsen/logrus" + lSyslog "github.com/sirupsen/logrus/hooks/syslog" +) + +func AddSyslogHook() { + hook, err := lSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "") + + if err != nil { + log.Errorf("Failed creating syslog hook: %s", err) + } + log.AddHook(hook) +} diff --git a/util/syslog_windows.go b/util/syslog_windows.go new file mode 100644 index 000000000..a38d90054 --- /dev/null +++ b/util/syslog_windows.go @@ -0,0 +1,3 @@ +package util + +func AddSyslogHook() {}