Skip to content

Commit 70234da

Browse files
committed
Add instructions to use different log levels for local and syslog
This commit adds instructions to the syslog readme about how to send different log levels to local logging (`log.SetLevel`) and syslog hook. fixes #1369
1 parent a448f82 commit 70234da

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ the last thing you want from your Logging library (again...).
99

1010
This does not mean Logrus is dead. Logrus will continue to be maintained for
1111
security, (backwards compatible) bug fixes, and performance (where we are
12-
limited by the interface).
12+
limited by the interface).
1313

1414
I believe Logrus' biggest contribution is to have played a part in today's
1515
widespread use of structured logging in Golang. There doesn't seem to be a
@@ -99,7 +99,7 @@ time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcr
9999
```
100100
Note that this does add measurable overhead - the cost will depend on the version of Go, but is
101101
between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your
102-
environment via benchmarks:
102+
environment via benchmarks:
103103
```
104104
go test -bench=.*CallerTracing
105105
```
@@ -317,6 +317,8 @@ log.SetLevel(log.InfoLevel)
317317
It may be useful to set `log.Level = logrus.DebugLevel` in a debug or verbose
318318
environment if your application has that.
319319

320+
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).
321+
320322
#### Entries
321323

322324
Besides the fields added with `WithField` or `WithFields` some fields are

hooks/syslog/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,45 @@ func main() {
3737
}
3838
}
3939
```
40+
41+
### Different log levels for local and remote logging
42+
43+
By default `NewSyslogHook()` sends logs through the hook for all log levels. If you want to have
44+
different log levels between local logging and syslog logging (i.e. respect the `priority` argument
45+
passed to `NewSyslogHook()`), you need to implement the `logrus_syslog.SyslogHook` interface
46+
overriding `Levels()` to return only the log levels you're interested on.
47+
48+
The following example shows how to log at **DEBUG** level for local logging and **WARN** level for
49+
syslog logging:
50+
51+
```go
52+
package main
53+
54+
import (
55+
"log/syslog"
56+
57+
log "github.com/sirupsen/logrus"
58+
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
59+
)
60+
61+
type customHook struct {
62+
*logrus_syslog.SyslogHook
63+
}
64+
65+
func (h *customHook) Levels() []log.Level {
66+
return []log.Level{log.WarnLevel}
67+
}
68+
69+
func main() {
70+
log.SetLevel(log.DebugLevel)
71+
72+
hook, err := logrus_syslog.NewSyslogHook("tcp", "localhost:5140", syslog.LOG_WARNING, "myTag")
73+
if err != nil {
74+
panic(err)
75+
}
76+
77+
log.AddHook(&customHook{hook})
78+
79+
//...
80+
}
81+
```

0 commit comments

Comments
 (0)