mirror of https://github.com/status-im/consul.git
agent: Filter messages logged to syslog. Fixes #272
This commit is contained in:
parent
bee5fe280c
commit
b9977338fd
|
@ -235,7 +235,7 @@ func (c *Command) setupLoggers(config *Config) (*GatedWriter, *logWriter, io.Wri
|
||||||
c.Ui.Error(fmt.Sprintf("Syslog setup failed: %v", err))
|
c.Ui.Error(fmt.Sprintf("Syslog setup failed: %v", err))
|
||||||
return nil, nil, nil
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
syslog = &SyslogWrapper{l}
|
syslog = &SyslogWrapper{l, c.logFilter}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a log writer, and wrap a logOutput around it
|
// Create a log writer, and wrap a logOutput around it
|
||||||
|
|
|
@ -3,6 +3,7 @@ package agent
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/hashicorp/go-syslog"
|
"github.com/hashicorp/go-syslog"
|
||||||
|
"github.com/hashicorp/logutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// levelPriority is used to map a log level to a
|
// levelPriority is used to map a log level to a
|
||||||
|
@ -20,11 +21,17 @@ var levelPriority = map[string]gsyslog.Priority{
|
||||||
// writing them to a Syslogger. Implements the io.Writer
|
// writing them to a Syslogger. Implements the io.Writer
|
||||||
// interface.
|
// interface.
|
||||||
type SyslogWrapper struct {
|
type SyslogWrapper struct {
|
||||||
l gsyslog.Syslogger
|
l gsyslog.Syslogger
|
||||||
|
filt *logutils.LevelFilter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write is used to implement io.Writer
|
// Write is used to implement io.Writer
|
||||||
func (s *SyslogWrapper) Write(p []byte) (int, error) {
|
func (s *SyslogWrapper) Write(p []byte) (int, error) {
|
||||||
|
// Skip syslog if the log level doesn't apply
|
||||||
|
if !s.filt.Check(p) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Extract log level
|
// Extract log level
|
||||||
var level string
|
var level string
|
||||||
afterLevel := p
|
afterLevel := p
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package agent
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-syslog"
|
||||||
|
"github.com/hashicorp/logutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSyslogFilter(t *testing.T) {
|
||||||
|
l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "LOCAL0", "consul")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
filt := LevelFilter()
|
||||||
|
filt.MinLevel = logutils.LogLevel("INFO")
|
||||||
|
|
||||||
|
s := &SyslogWrapper{l, filt}
|
||||||
|
n, err := s.Write([]byte("[INFO] test"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
t.Fatalf("should have logged")
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err = s.Write([]byte("[DEBUG] test"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
if n != 0 {
|
||||||
|
t.Fatalf("should not have logged")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue