agent: fix logging

* use agent logger for consul/serf/raft/dns/agent/...
* support optional id for concurrent tests
This commit is contained in:
Frank Schroeder 2017-05-23 19:04:06 +02:00
parent fe777852cd
commit 10540f8d5f
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
3 changed files with 33 additions and 10 deletions

View File

@ -77,6 +77,10 @@ type clientServer interface {
// mode, it runs a full Consul server. In client-only mode, it only forwards
// requests to other Consul servers.
type Agent struct {
// id is an optional log prefix.
id string
// config is the agent configuration.
config *Config
// Used for writing our logs
@ -207,10 +211,13 @@ func NewAgent(c *Config) (*Agent, error) {
func (a *Agent) Start() error {
c := a.config
if a.LogOutput == nil {
a.LogOutput = os.Stderr
logOutput := a.LogOutput
if a.logger == nil {
if logOutput == nil {
logOutput = os.Stderr
}
a.logger = log.New(logOutput, a.id, log.LstdFlags)
}
a.logger = log.New(a.LogOutput, "", log.LstdFlags)
// Retrieve or generate the node ID before setting up the rest of the
// agent, which depends on it.
@ -280,7 +287,7 @@ func (a *Agent) Start() error {
// start dns server
if c.Ports.DNS > 0 {
srv, err := NewDNSServer(a, &c.DNSConfig, a.LogOutput, c.Domain, a.dnsAddr.String(), c.DNSRecursors)
srv, err := NewDNSServer(a, &c.DNSConfig, logOutput, a.logger, c.Domain, a.dnsAddr.String(), c.DNSRecursors)
if err != nil {
return fmt.Errorf("error starting DNS server: %s", err)
}
@ -765,7 +772,7 @@ func (a *Agent) makeServer() (*consul.Server, error) {
if err := a.setupKeyrings(config); err != nil {
return nil, fmt.Errorf("Failed to configure keyring: %v", err)
}
server, err := consul.NewServer(config)
server, err := consul.NewServerLogger(config, a.logger)
if err != nil {
return nil, fmt.Errorf("Failed to start Consul server: %v", err)
}
@ -1615,7 +1622,7 @@ func (a *Agent) RemoveCheck(checkID types.CheckID, persist bool) error {
return err
}
}
log.Printf("[DEBUG] agent: removed check %q", checkID)
a.logger.Printf("[DEBUG] agent: removed check %q", checkID)
return nil
}

View File

@ -6,6 +6,7 @@ import (
"io"
"log"
"net"
"os"
"strings"
"sync"
"time"
@ -53,7 +54,13 @@ func (d *DNSServer) Shutdown() {
}
// NewDNSServer starts a new DNS server to provide an agent interface
func NewDNSServer(agent *Agent, config *DNSConfig, logOutput io.Writer, domain string, bind string, recursors []string) (*DNSServer, error) {
func NewDNSServer(agent *Agent, config *DNSConfig, logOutput io.Writer, logger *log.Logger, domain string, bind string, recursors []string) (*DNSServer, error) {
if logger == nil {
if logOutput == nil {
logOutput = os.Stderr
}
logger = log.New(logOutput, "", log.LstdFlags)
}
// Make sure domain is FQDN, make it case insensitive for ServeMux
domain = dns.Fqdn(strings.ToLower(domain))
@ -86,7 +93,7 @@ func NewDNSServer(agent *Agent, config *DNSConfig, logOutput io.Writer, domain s
dnsServerTCP: serverTCP,
domain: domain,
recursors: recursors,
logger: log.New(logOutput, "", log.LstdFlags),
logger: logger,
}
// Register mux handler, for reverse lookup

View File

@ -210,9 +210,13 @@ type endpoints struct {
Txn *Txn
}
func NewServer(config *Config) (*Server, error) {
return NewServerLogger(config, nil)
}
// NewServer is used to construct a new Consul server from the
// configuration, potentially returning an error
func NewServer(config *Config) (*Server, error) {
func NewServerLogger(config *Config, l *log.Logger) (*Server, error) {
// Check the protocol version.
if err := config.CheckProtocolVersion(); err != nil {
return nil, err
@ -232,7 +236,10 @@ func NewServer(config *Config) (*Server, error) {
if config.LogOutput == nil {
config.LogOutput = os.Stderr
}
logger := log.New(config.LogOutput, "", log.LstdFlags)
logger := l
if logger == nil {
logger = log.New(config.LogOutput, "", log.LstdFlags)
}
// Check if TLS is enabled
if config.CAFile != "" || config.CAPath != "" {
@ -403,6 +410,7 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string, w
}
conf.MemberlistConfig.LogOutput = s.config.LogOutput
conf.LogOutput = s.config.LogOutput
conf.Logger = s.logger
conf.EventCh = ch
if !s.config.DevMode {
conf.SnapshotPath = filepath.Join(s.config.DataDir, path)
@ -454,6 +462,7 @@ func (s *Server) setupRaft() error {
// Make sure we set the LogOutput.
s.config.RaftConfig.LogOutput = s.config.LogOutput
s.config.RaftConfig.Logger = s.logger
// Versions of the Raft protocol below 3 require the LocalID to match the network
// address of the transport.