Skip to content

Commit

Permalink
feat(client): send logs to syslog
Browse files Browse the repository at this point in the history
closes #2255
  • Loading branch information
mrl5 committed Jul 11, 2024
1 parent e8c2faf commit 2ba4443
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 12 additions & 1 deletion util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package util

import (
"io"
"log/syslog"
"os"
"path/filepath"
"slices"

log "github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
"gopkg.in/natefinch/lumberjack.v2"

"github.com/netbirdio/netbird/formatter"
Expand All @@ -18,8 +21,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),
Expand All @@ -29,6 +33,13 @@ func InitLog(logLevel string, logPath string) error {
Compress: true,
}
log.SetOutput(io.Writer(lumberjackLogger))
} else if logPath == "syslog" {
hook, err := lSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "")

if err != nil {
log.Errorf("Failed creating syslog hook: %s", err)
}
log.AddHook(hook)
}

if os.Getenv("NB_LOG_FORMAT") == "json" {
Expand Down

0 comments on commit 2ba4443

Please sign in to comment.