diff --git a/formatter/formatter.go b/formatter/formatter.go deleted file mode 100644 index 74de38603..000000000 --- a/formatter/formatter.go +++ /dev/null @@ -1,83 +0,0 @@ -package formatter - -import ( - "fmt" - "strings" - "time" - - "github.com/sirupsen/logrus" -) - -// TextFormatter formats logs into text with included source code's path -type TextFormatter struct { - timestampFormat string - levelDesc []string -} - -// SyslogFormatter formats logs into text -type SyslogFormatter struct { - levelDesc []string -} - -var validLevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG", "TRAC"} - - -// NewTextFormatter create new MyTextFormatter instance -func NewTextFormatter() *TextFormatter { - return &TextFormatter{ - levelDesc: validLevelDesc, - timestampFormat: time.RFC3339, // or RFC3339 - } -} - -// NewSyslogFormatter create new MySyslogFormatter instance -func NewSyslogFormatter() *SyslogFormatter { - return &SyslogFormatter{ - levelDesc: validLevelDesc, - } -} - -// Format renders a single log entry -func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) { - var fields string - keys := make([]string, 0, len(entry.Data)) - for k, v := range entry.Data { - if k == "source" { - continue - } - keys = append(keys, fmt.Sprintf("%s: %v", k, v)) - } - - if len(keys) > 0 { - fields = fmt.Sprintf("[%s] ", strings.Join(keys, ", ")) - } - - level := f.parseLevel(entry.Level) - - return []byte(fmt.Sprintf("%s %s %s%s: %s\n", entry.Time.Format(f.timestampFormat), level, fields, entry.Data["source"], entry.Message)), nil -} - -func (f *TextFormatter) parseLevel(level logrus.Level) string { - if len(f.levelDesc) < int(level) { - return "" - } - - return f.levelDesc[level] -} - -// Format renders a single log entry -func (f *SyslogFormatter) Format(entry *logrus.Entry) ([]byte, error) { - var fields string - keys := make([]string, 0, len(entry.Data)) - for k, v := range entry.Data { - if k == "source" { - continue - } - keys = append(keys, fmt.Sprintf("%s: %v", k, v)) - } - - if len(keys) > 0 { - fields = fmt.Sprintf("[%s] ", strings.Join(keys, ", ")) - } - return []byte(fmt.Sprintf("%s%s\n", fields, entry.Message)), nil -} diff --git a/formatter/hook/additional_empty.go b/formatter/hook/additional_empty.go new file mode 100644 index 000000000..4f5069482 --- /dev/null +++ b/formatter/hook/additional_empty.go @@ -0,0 +1,9 @@ +//go:build !loggoroutine + +package hook + +import log "github.com/sirupsen/logrus" + +func additionalEntries(_ *log.Entry) { + // This function is empty and is used to demonstrate the use of additional hooks. +} diff --git a/formatter/hook/additional_goroutine.go b/formatter/hook/additional_goroutine.go new file mode 100644 index 000000000..fb4e09f47 --- /dev/null +++ b/formatter/hook/additional_goroutine.go @@ -0,0 +1,12 @@ +//go:build loggoroutine + +package hook + +import ( + "github.com/petermattis/goid" + log "github.com/sirupsen/logrus" +) + +func additionalEntries(entry *log.Entry) { + entry.Data[EntryKeyGoroutineID] = goid.Get() +} diff --git a/formatter/hook.go b/formatter/hook/hook.go similarity index 96% rename from formatter/hook.go rename to formatter/hook/hook.go index 12f27e67d..1b6ceccc9 100644 --- a/formatter/hook.go +++ b/formatter/hook/hook.go @@ -1,4 +1,4 @@ -package formatter +package hook import ( "fmt" @@ -41,7 +41,8 @@ func (hook ContextHook) Levels() []logrus.Level { // Fire extend with the source information the entry.Data func (hook ContextHook) Fire(entry *logrus.Entry) error { src := hook.parseSrc(entry.Caller.File) - entry.Data["source"] = fmt.Sprintf("%s:%v", src, entry.Caller.Line) + entry.Data[EntryKeySource] = fmt.Sprintf("%s:%v", src, entry.Caller.Line) + additionalEntries(entry) if entry.Context == nil { return nil diff --git a/formatter/hook_test.go b/formatter/hook/hook_test.go similarity index 98% rename from formatter/hook_test.go rename to formatter/hook/hook_test.go index a4bcb0284..802163244 100644 --- a/formatter/hook_test.go +++ b/formatter/hook/hook_test.go @@ -1,4 +1,4 @@ -package formatter +package hook import ( "testing" diff --git a/formatter/hook/keys.go b/formatter/hook/keys.go new file mode 100644 index 000000000..09781a88b --- /dev/null +++ b/formatter/hook/keys.go @@ -0,0 +1,6 @@ +package hook + +const ( + EntryKeySource = "source" + EntryKeyGoroutineID = "goroutine_id" +) diff --git a/formatter/levels/levels.go b/formatter/levels/levels.go new file mode 100644 index 000000000..41ae80db3 --- /dev/null +++ b/formatter/levels/levels.go @@ -0,0 +1,3 @@ +package levels + +var ValidLevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG", "TRAC"} diff --git a/formatter/logcat.go b/formatter/logcat/logcat.go similarity index 63% rename from formatter/logcat.go rename to formatter/logcat/logcat.go index e8f606229..c561d3283 100644 --- a/formatter/logcat.go +++ b/formatter/logcat/logcat.go @@ -1,26 +1,28 @@ -package formatter +package logcat import ( "fmt" "strings" "github.com/sirupsen/logrus" + + "github.com/netbirdio/netbird/formatter/levels" ) -// LogcatFormatter formats logs into text what is fit for logcat -type LogcatFormatter struct { +// Formatter formats logs into text what is fit for logcat +type Formatter struct { levelDesc []string } // NewLogcatFormatter create new LogcatFormatter instance -func NewLogcatFormatter() *LogcatFormatter { - return &LogcatFormatter{ - levelDesc: []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG", "TRAC"}, +func NewLogcatFormatter() *Formatter { + return &Formatter{ + levelDesc: levels.ValidLevelDesc, } } // Format renders a single log entry -func (f *LogcatFormatter) Format(entry *logrus.Entry) ([]byte, error) { +func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { var fields string keys := make([]string, 0, len(entry.Data)) for k, v := range entry.Data { @@ -39,7 +41,7 @@ func (f *LogcatFormatter) Format(entry *logrus.Entry) ([]byte, error) { return []byte(fmt.Sprintf("[%s] %s%s %s\n", level, fields, entry.Data["source"], entry.Message)), nil } -func (f *LogcatFormatter) parseLevel(level logrus.Level) string { +func (f *Formatter) parseLevel(level logrus.Level) string { if len(f.levelDesc) < int(level) { return "" } diff --git a/formatter/logcat_test.go b/formatter/logcat/logcat_test.go similarity index 97% rename from formatter/logcat_test.go rename to formatter/logcat/logcat_test.go index 45ba5bc46..fd4d92881 100644 --- a/formatter/logcat_test.go +++ b/formatter/logcat/logcat_test.go @@ -1,4 +1,4 @@ -package formatter +package logcat import ( "testing" @@ -25,4 +25,5 @@ func TestLogcatMessageFormat(t *testing.T) { if parsedString != expectedString && parsedString != expectedStringVariant { t.Errorf("The log messages don't match. Expected: '%s', got: '%s'", expectedString, parsedString) } + } diff --git a/formatter/set.go b/formatter/set.go index 9dfea5a7f..a609e7b48 100644 --- a/formatter/set.go +++ b/formatter/set.go @@ -2,31 +2,37 @@ package formatter import ( "github.com/sirupsen/logrus" + + "github.com/netbirdio/netbird/formatter/hook" + "github.com/netbirdio/netbird/formatter/logcat" + "github.com/netbirdio/netbird/formatter/syslog" + "github.com/netbirdio/netbird/formatter/txt" ) // SetTextFormatter set the text formatter for given logger. func SetTextFormatter(logger *logrus.Logger) { - logger.Formatter = NewTextFormatter() + logger.Formatter = txt.NewTextFormatter() logger.ReportCaller = true - logger.AddHook(NewContextHook()) + logger.AddHook(hook.NewContextHook()) } + // SetSyslogFormatter set the text formatter for given logger. func SetSyslogFormatter(logger *logrus.Logger) { - logger.Formatter = NewSyslogFormatter() + logger.Formatter = syslog.NewSyslogFormatter() logger.ReportCaller = true - logger.AddHook(NewContextHook()) + logger.AddHook(hook.NewContextHook()) } // SetJSONFormatter set the JSON formatter for given logger. func SetJSONFormatter(logger *logrus.Logger) { logger.Formatter = &logrus.JSONFormatter{} logger.ReportCaller = true - logger.AddHook(NewContextHook()) + logger.AddHook(hook.NewContextHook()) } // SetLogcatFormatter set the logcat formatter for given logger. func SetLogcatFormatter(logger *logrus.Logger) { - logger.Formatter = NewLogcatFormatter() + logger.Formatter = logcat.NewLogcatFormatter() logger.ReportCaller = true - logger.AddHook(NewContextHook()) + logger.AddHook(hook.NewContextHook()) } diff --git a/formatter/syslog/formatter.go b/formatter/syslog/formatter.go new file mode 100644 index 000000000..e72c30347 --- /dev/null +++ b/formatter/syslog/formatter.go @@ -0,0 +1,39 @@ +package syslog + +import ( + "fmt" + "strings" + + "github.com/sirupsen/logrus" + + "github.com/netbirdio/netbird/formatter/levels" +) + +// Formatter formats logs into text +type Formatter struct { + levelDesc []string +} + +// NewSyslogFormatter create new MySyslogFormatter instance +func NewSyslogFormatter() *Formatter { + return &Formatter{ + levelDesc: levels.ValidLevelDesc, + } +} + +// Format renders a single log entry +func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { + var fields string + keys := make([]string, 0, len(entry.Data)) + for k, v := range entry.Data { + if k == "source" { + continue + } + keys = append(keys, fmt.Sprintf("%s: %v", k, v)) + } + + if len(keys) > 0 { + fields = fmt.Sprintf("[%s] ", strings.Join(keys, ", ")) + } + return []byte(fmt.Sprintf("%s%s\n", fields, entry.Message)), nil +} diff --git a/formatter/syslog/formatter_test.go b/formatter/syslog/formatter_test.go new file mode 100644 index 000000000..110a3390b --- /dev/null +++ b/formatter/syslog/formatter_test.go @@ -0,0 +1,26 @@ +package syslog + +import ( + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestLogSyslogFormat(t *testing.T) { + + someEntry := &logrus.Entry{ + Data: logrus.Fields{"att1": 1, "att2": 2, "source": "some/fancy/path.go:46"}, + Time: time.Date(2021, time.Month(2), 21, 1, 10, 30, 0, time.UTC), + Level: 3, + Message: "Some Message", + } + + formatter := NewSyslogFormatter() + result, _ := formatter.Format(someEntry) + + parsedString := string(result) + expectedString := "^\\[(att1: 1, att2: 2|att2: 2, att1: 1)\\] Some Message\\s+$" + assert.Regexp(t, expectedString, parsedString) +} diff --git a/formatter/txt/format.go b/formatter/txt/format.go new file mode 100644 index 000000000..a88c41044 --- /dev/null +++ b/formatter/txt/format.go @@ -0,0 +1,31 @@ +//go:build !loggoroutine + +package txt + +import ( + "fmt" + "strings" + + "github.com/sirupsen/logrus" + + "github.com/netbirdio/netbird/formatter/hook" +) + +func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) { + var fields string + keys := make([]string, 0, len(entry.Data)) + for k, v := range entry.Data { + if k == hook.EntryKeySource { + continue + } + keys = append(keys, fmt.Sprintf("%s: %v", k, v)) + } + + if len(keys) > 0 { + fields = fmt.Sprintf("[%s] ", strings.Join(keys, ", ")) + } + + level := f.parseLevel(entry.Level) + + return []byte(fmt.Sprintf("%s %s %s%s: %s\n", entry.Time.Format(f.timestampFormat), level, fields, entry.Data[hook.EntryKeySource], entry.Message)), nil +} diff --git a/formatter/txt/format_gorutines.go b/formatter/txt/format_gorutines.go new file mode 100644 index 000000000..a39aee633 --- /dev/null +++ b/formatter/txt/format_gorutines.go @@ -0,0 +1,35 @@ +//go:build loggoroutine + +package txt + +import ( + "fmt" + "strings" + + "github.com/sirupsen/logrus" + + "github.com/netbirdio/netbird/formatter/hook" +) + +func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) { + var fields string + keys := make([]string, 0, len(entry.Data)) + for k, v := range entry.Data { + if k == hook.EntryKeySource { + continue + } + + if k == hook.EntryKeyGoroutineID { + continue + } + keys = append(keys, fmt.Sprintf("%s: %v", k, v)) + } + + if len(keys) > 0 { + fields = fmt.Sprintf("[%s] ", strings.Join(keys, ", ")) + } + + level := f.parseLevel(entry.Level) + + return []byte(fmt.Sprintf("%s %s %d %s%s: %s\n", entry.Time.Format(f.timestampFormat), level, entry.Data[hook.EntryKeyGoroutineID], fields, entry.Data[hook.EntryKeySource], entry.Message)), nil +} diff --git a/formatter/txt/formatter.go b/formatter/txt/formatter.go new file mode 100644 index 000000000..3b2a3fb4d --- /dev/null +++ b/formatter/txt/formatter.go @@ -0,0 +1,31 @@ +package txt + +import ( + "time" + + "github.com/sirupsen/logrus" + + "github.com/netbirdio/netbird/formatter/levels" +) + +// TextFormatter formats logs into text with included source code's path +type TextFormatter struct { + timestampFormat string + levelDesc []string +} + +// NewTextFormatter create new MyTextFormatter instance +func NewTextFormatter() *TextFormatter { + return &TextFormatter{ + levelDesc: levels.ValidLevelDesc, + timestampFormat: time.RFC3339, // or RFC3339 + } +} + +func (f *TextFormatter) parseLevel(level logrus.Level) string { + if len(f.levelDesc) < int(level) { + return "" + } + + return f.levelDesc[level] +} diff --git a/formatter/formatter_test.go b/formatter/txt/formatter_test.go similarity index 55% rename from formatter/formatter_test.go rename to formatter/txt/formatter_test.go index 1ed207958..590af5d50 100644 --- a/formatter/formatter_test.go +++ b/formatter/txt/formatter_test.go @@ -1,4 +1,4 @@ -package formatter +package txt import ( "testing" @@ -24,20 +24,3 @@ func TestLogTextFormat(t *testing.T) { expectedString := "^2021-02-21T01:10:30Z WARN \\[(att1: 1, att2: 2|att2: 2, att1: 1)\\] some/fancy/path.go:46: Some Message\\s+$" assert.Regexp(t, expectedString, parsedString) } - -func TestLogSyslogFormat(t *testing.T) { - - someEntry := &logrus.Entry{ - Data: logrus.Fields{"att1": 1, "att2": 2, "source": "some/fancy/path.go:46"}, - Time: time.Date(2021, time.Month(2), 21, 1, 10, 30, 0, time.UTC), - Level: 3, - Message: "Some Message", - } - - formatter := NewSyslogFormatter() - result, _ := formatter.Format(someEntry) - - parsedString := string(result) - expectedString := "^\\[(att1: 1, att2: 2|att2: 2, att1: 1)\\] Some Message\\s+$" - assert.Regexp(t, expectedString, parsedString) -} diff --git a/go.mod b/go.mod index 3d71e8eb1..76605e155 100644 --- a/go.mod +++ b/go.mod @@ -65,6 +65,7 @@ require ( github.com/okta/okta-sdk-golang/v2 v2.18.0 github.com/oschwald/maxminddb-golang v1.12.0 github.com/patrickmn/go-cache v2.1.0+incompatible + github.com/petermattis/goid v0.0.0-20250303134427-723919f7f203 github.com/pion/logging v0.2.2 github.com/pion/randutil v0.1.0 github.com/pion/stun/v2 v2.0.0 diff --git a/go.sum b/go.sum index 36bca22d3..069b378d6 100644 --- a/go.sum +++ b/go.sum @@ -571,6 +571,8 @@ github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCko github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0= github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/petermattis/goid v0.0.0-20250303134427-723919f7f203 h1:E7Kmf11E4K7B5hDti2K2NqPb1nlYlGYsu02S1JNd/Bs= +github.com/petermattis/goid v0.0.0-20250303134427-723919f7f203/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4= github.com/pion/dtls/v2 v2.2.7/go.mod h1:8WiMkebSHFD0T+dIU+UeBaoV7kDhOW5oDCzZ7WZ/F9s= github.com/pion/dtls/v2 v2.2.10 h1:u2Axk+FyIR1VFTPurktB+1zoEPGIW3bmyj3LEFrXjAA= github.com/pion/dtls/v2 v2.2.10/go.mod h1:d9SYc9fch0CqK90mRk1dC7AkzzpwJj6u2GU3u+9pqFE= diff --git a/management/cmd/management.go b/management/cmd/management.go index 9712f04aa..e087a3d67 100644 --- a/management/cmd/management.go +++ b/management/cmd/management.go @@ -36,7 +36,7 @@ import ( "github.com/netbirdio/management-integrations/integrations" "github.com/netbirdio/netbird/encryption" - "github.com/netbirdio/netbird/formatter" + "github.com/netbirdio/netbird/formatter/hook" mgmtProto "github.com/netbirdio/netbird/management/proto" "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/auth" @@ -90,7 +90,7 @@ var ( flag.Parse() //nolint - ctx := context.WithValue(cmd.Context(), formatter.ExecutionContextKey, formatter.SystemSource) + ctx := context.WithValue(cmd.Context(), hook.ExecutionContextKey, hook.SystemSource) err := util.InitLog(logLevel, logFile) if err != nil { @@ -136,7 +136,7 @@ var ( ctx, cancel := context.WithCancel(cmd.Context()) defer cancel() //nolint - ctx = context.WithValue(ctx, formatter.ExecutionContextKey, formatter.SystemSource) + ctx = context.WithValue(ctx, hook.ExecutionContextKey, hook.SystemSource) err := handleRebrand(cmd) if err != nil { @@ -374,7 +374,7 @@ func unaryInterceptor( ) (interface{}, error) { reqID := uuid.New().String() //nolint - ctx = context.WithValue(ctx, formatter.ExecutionContextKey, formatter.GRPCSource) + ctx = context.WithValue(ctx, hook.ExecutionContextKey, hook.GRPCSource) //nolint ctx = context.WithValue(ctx, nbContext.RequestIDKey, reqID) return handler(ctx, req) @@ -389,7 +389,7 @@ func streamInterceptor( reqID := uuid.New().String() wrapped := grpcMiddleware.WrapServerStream(ss) //nolint - ctx := context.WithValue(ss.Context(), formatter.ExecutionContextKey, formatter.GRPCSource) + ctx := context.WithValue(ss.Context(), hook.ExecutionContextKey, hook.GRPCSource) //nolint wrapped.WrappedContext = context.WithValue(ctx, nbContext.RequestIDKey, reqID) return handler(srv, wrapped) diff --git a/management/cmd/migration_up.go b/management/cmd/migration_up.go index 183fc554d..de061dca2 100644 --- a/management/cmd/migration_up.go +++ b/management/cmd/migration_up.go @@ -8,7 +8,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/netbirdio/netbird/formatter" + "github.com/netbirdio/netbird/formatter/hook" "github.com/netbirdio/netbird/management/server/store" "github.com/netbirdio/netbird/util" ) @@ -30,7 +30,7 @@ var upCmd = &cobra.Command{ } //nolint - ctx := context.WithValue(cmd.Context(), formatter.ExecutionContextKey, formatter.SystemSource) + ctx := context.WithValue(cmd.Context(), hook.ExecutionContextKey, hook.SystemSource) if err := store.MigrateFileStoreToSqlite(ctx, mgmtDataDir); err != nil { return err diff --git a/management/server/management_proto_test.go b/management/server/management_proto_test.go index 4d0630f0f..28f5abf57 100644 --- a/management/server/management_proto_test.go +++ b/management/server/management_proto_test.go @@ -20,7 +20,7 @@ import ( "google.golang.org/grpc/keepalive" "github.com/netbirdio/netbird/encryption" - "github.com/netbirdio/netbird/formatter" + "github.com/netbirdio/netbird/formatter/hook" mgmtProto "github.com/netbirdio/netbird/management/proto" "github.com/netbirdio/netbird/management/server/activity" "github.com/netbirdio/netbird/management/server/settings" @@ -424,7 +424,7 @@ func startManagementForTest(t *testing.T, testFile string, config *Config) (*grp peersUpdateManager := NewPeersUpdateManager(nil) eventStore := &activity.InMemoryEventStore{} - ctx := context.WithValue(context.Background(), formatter.ExecutionContextKey, formatter.SystemSource) //nolint:staticcheck + ctx := context.WithValue(context.Background(), hook.ExecutionContextKey, hook.SystemSource) //nolint:staticcheck metrics, err := telemetry.NewDefaultAppMetrics(context.Background()) require.NoError(t, err) diff --git a/management/server/telemetry/http_api_metrics.go b/management/server/telemetry/http_api_metrics.go index 5ef9e6d02..ae27466d9 100644 --- a/management/server/telemetry/http_api_metrics.go +++ b/management/server/telemetry/http_api_metrics.go @@ -13,7 +13,7 @@ import ( "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" - "github.com/netbirdio/netbird/formatter" + "github.com/netbirdio/netbird/formatter/hook" nbContext "github.com/netbirdio/netbird/management/server/context" ) @@ -167,7 +167,7 @@ func (m *HTTPMiddleware) Handler(h http.Handler) http.Handler { reqStart := time.Now() //nolint - ctx := context.WithValue(r.Context(), formatter.ExecutionContextKey, formatter.HTTPSource) + ctx := context.WithValue(r.Context(), hook.ExecutionContextKey, hook.HTTPSource) reqID := uuid.New().String() //nolint