diff --git a/README.md b/README.md index b042c896f..d1d4a85fd 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ the last thing you want from your Logging library (again...). This does not mean Logrus is dead. Logrus will continue to be maintained for security, (backwards compatible) bug fixes, and performance (where we are -limited by the interface). +limited by the interface). I believe Logrus' biggest contribution is to have played a part in today's widespread use of structured logging in Golang. There doesn't seem to be a @@ -43,7 +43,7 @@ plain text): With `log.SetFormatter(&log.JSONFormatter{})`, for easy parsing by logstash or Splunk: -```json +```text {"animal":"walrus","level":"info","msg":"A group of walrus emerges from the ocean","size":10,"time":"2014-03-10 19:57:38.562264131 -0400 EDT"} @@ -99,7 +99,7 @@ time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcr ``` Note that this does add measurable overhead - the cost will depend on the version of Go, but is between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your -environment via benchmarks: +environment via benchmarks: ``` go test -bench=.*CallerTracing ``` @@ -317,6 +317,8 @@ log.SetLevel(log.InfoLevel) It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose environment if your application has that. +Note: If you want different log levels for global (`log.SetLevel(...)`) and syslog logging, please check the [syslog hook README](hooks/syslog/README.md#different-log-levels-for-local-and-remote-logging). + #### Entries Besides the fields added with `WithField` or `WithFields` some fields are diff --git a/entry_test.go b/entry_test.go index 41c47a2fb..d70266104 100644 --- a/entry_test.go +++ b/entry_test.go @@ -10,6 +10,8 @@ import ( "github.com/stretchr/testify/assert" ) +type contextKeyType string + func TestEntryWithError(t *testing.T) { assert := assert.New(t) @@ -36,7 +38,8 @@ func TestEntryWithError(t *testing.T) { func TestEntryWithContext(t *testing.T) { assert := assert.New(t) - ctx := context.WithValue(context.Background(), "foo", "bar") + var contextKey contextKeyType = "foo" + ctx := context.WithValue(context.Background(), contextKey, "bar") assert.Equal(ctx, WithContext(ctx).Context) @@ -56,11 +59,13 @@ func TestEntryWithContextCopiesData(t *testing.T) { parentEntry := NewEntry(logger).WithField("parentKey", "parentValue") // Create two children Entry objects from the parent in different contexts - ctx1 := context.WithValue(context.Background(), "foo", "bar") + var contextKey1 contextKeyType = "foo" + ctx1 := context.WithValue(context.Background(), contextKey1, "bar") childEntry1 := parentEntry.WithContext(ctx1) assert.Equal(ctx1, childEntry1.Context) - ctx2 := context.WithValue(context.Background(), "bar", "baz") + var contextKey2 contextKeyType = "bar" + ctx2 := context.WithValue(context.Background(), contextKey2, "baz") childEntry2 := parentEntry.WithContext(ctx2) assert.Equal(ctx2, childEntry2.Context) assert.NotEqual(ctx1, ctx2) diff --git a/go.mod b/go.mod index e1623b1fc..8b3f6d373 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/sirupsen/logrus require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/stretchr/testify v1.7.0 - golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 ) go 1.13 diff --git a/go.sum b/go.sum index a2d89e4fd..e5fdc85bf 100644 --- a/go.sum +++ b/go.sum @@ -6,9 +6,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hooks/syslog/README.md b/hooks/syslog/README.md index 1bbc0f72d..67cb5ea85 100644 --- a/hooks/syslog/README.md +++ b/hooks/syslog/README.md @@ -37,3 +37,45 @@ func main() { } } ``` + +### Different log levels for local and remote logging + +By default `NewSyslogHook()` sends logs through the hook for all log levels. If you want to have +different log levels between local logging and syslog logging (i.e. respect the `priority` argument +passed to `NewSyslogHook()`), you need to implement the `logrus_syslog.SyslogHook` interface +overriding `Levels()` to return only the log levels you're interested on. + +The following example shows how to log at **DEBUG** level for local logging and **WARN** level for +syslog logging: + +```go +package main + +import ( + "log/syslog" + + log "github.com/sirupsen/logrus" + logrus_syslog "github.com/sirupsen/logrus/hooks/syslog" +) + +type customHook struct { + *logrus_syslog.SyslogHook +} + +func (h *customHook) Levels() []log.Level { + return []log.Level{log.WarnLevel} +} + +func main() { + log.SetLevel(log.DebugLevel) + + hook, err := logrus_syslog.NewSyslogHook("tcp", "localhost:5140", syslog.LOG_WARNING, "myTag") + if err != nil { + panic(err) + } + + log.AddHook(&customHook{hook}) + + //... +} +``` diff --git a/hooks/test/test.go b/hooks/test/test.go index b16d06654..046f0bf6c 100644 --- a/hooks/test/test.go +++ b/hooks/test/test.go @@ -32,7 +32,7 @@ func NewGlobal() *Hook { func NewLocal(logger *logrus.Logger) *Hook { hook := new(Hook) - logger.Hooks.Add(hook) + logger.AddHook(hook) return hook diff --git a/hooks/test/test_test.go b/hooks/test/test_test.go index 636bad512..9601491aa 100644 --- a/hooks/test/test_test.go +++ b/hooks/test/test_test.go @@ -83,3 +83,22 @@ func TestFatalWithAlternateExit(t *testing.T) { assert.Equal("something went very wrong", hook.LastEntry().Message) assert.Equal(1, len(hook.Entries)) } + +func TestNewLocal(t *testing.T) { + assert := assert.New(t) + logger := logrus.New() + + var wg sync.WaitGroup + defer wg.Wait() + + wg.Add(10) + for i := 0; i < 10; i++ { + go func(i int) { + logger.Info("info") + wg.Done() + }(i) + } + + hook := NewLocal(logger) + assert.NotNil(hook) +} diff --git a/logrus_test.go b/logrus_test.go index 4edee2834..72a02de1e 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -544,8 +544,7 @@ func TestParseLevel(t *testing.T) { } func TestLevelString(t *testing.T) { - var loggerlevel Level - loggerlevel = 32000 + var loggerlevel Level = 32000 _ = loggerlevel.String() }