Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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
```
Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/stretchr/testify/assert"
)

type contextKeyType string

func TestEntryWithError(t *testing.T) {

assert := assert.New(t)
Expand All @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
42 changes: 42 additions & 0 deletions hooks/syslog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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})

//...
}
```
2 changes: 1 addition & 1 deletion hooks/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions hooks/test/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down