Skip to content

Commit

Permalink
Changed syslogs function to build only for non-windows OS (#8248)
Browse files Browse the repository at this point in the history
  • Loading branch information
idanovo authored Sep 30, 2024
1 parent c9ab2a8 commit e428977
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
36 changes: 0 additions & 36 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"log/syslog"
"os"
"reflect"
"runtime"
Expand All @@ -14,7 +13,6 @@ import (
"time"

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

Expand Down Expand Up @@ -397,40 +395,6 @@ func ContextUnavailable() Logger {
}
}

func ContextUnavailableWithSysLogs(sysLogsLevel string) Logger {
// wrap formatter with our own formatter that overrides caller
formatterInitOnce.Do(func() {
defaultLogger.SetReportCaller(true)
defaultLogger.SetNoLock()
defaultLogger.Formatter = logrusCallerFormatter{defaultLogger.Formatter}
})
syslogOnce.Do(func() {
var hook *lSyslog.SyslogHook
var err error
switch strings.ToLower(sysLogsLevel) {
// There's no syslog level for trace, using debug instead.
case "trace", "debug":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_DEBUG, "")
case "info":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
case "warn", "warning":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_WARNING, "")
case "error":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_ERR, "")
case "panic", "null", "none":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_CRIT, "")
}
if err != nil {
defaultLogger.WithError(err).Error("failed to set syslog hook")
} else {
defaultLogger.AddHook(hook)
}
})
return &logrusEntryWrapper{
e: logrus.NewEntry(defaultLogger),
}
}

// GetFieldsFromContext returns the logging fields on ctx or nil.
func GetFieldsFromContext(ctx context.Context) Fields {
fields := ctx.Value(LogFieldsContextKey)
Expand Down
45 changes: 45 additions & 0 deletions pkg/logging/syslogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build !windows

package logging

import (
"log/syslog"
"strings"

"github.com/sirupsen/logrus"
lSyslog "github.com/sirupsen/logrus/hooks/syslog"
)

func ContextUnavailableWithSysLogs(sysLogsLevel string) Logger {
// wrap formatter with our own formatter that overrides caller
formatterInitOnce.Do(func() {
defaultLogger.SetReportCaller(true)
defaultLogger.SetNoLock()
defaultLogger.Formatter = logrusCallerFormatter{defaultLogger.Formatter}
})
syslogOnce.Do(func() {
var hook *lSyslog.SyslogHook
var err error
switch strings.ToLower(sysLogsLevel) {
// There's no syslog level for trace, using debug instead.
case "trace", "debug":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_DEBUG, "")
case "info":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "")
case "warn", "warning":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_WARNING, "")
case "error":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_ERR, "")
case "panic", "null", "none":
hook, err = lSyslog.NewSyslogHook("", "", syslog.LOG_CRIT, "")
}
if err != nil {
defaultLogger.WithError(err).Error("failed to set syslog hook")
} else {
defaultLogger.AddHook(hook)
}
})
return &logrusEntryWrapper{
e: logrus.NewEntry(defaultLogger),
}
}

0 comments on commit e428977

Please sign in to comment.