Skip to content

Commit

Permalink
agent: fix logging
Browse files Browse the repository at this point in the history
* use agent logger for consul/serf/raft/dns/agent/...
* support optional id for concurrent tests
  • Loading branch information
magiconair committed May 30, 2017
1 parent fe77785 commit 10540f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
19 changes: 13 additions & 6 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down
11 changes: 9 additions & 2 deletions command/agent/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions consul/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 10540f8

Please sign in to comment.