diff --git a/client/client.go b/client/client.go index 633ff06fe..79f50ac01 100644 --- a/client/client.go +++ b/client/client.go @@ -17,6 +17,7 @@ import ( metrics "github.com/armon/go-metrics" consulapi "github.com/hashicorp/consul/api" + hclog "github.com/hashicorp/go-hclog" multierror "github.com/hashicorp/go-multierror" consulApi "github.com/hashicorp/nomad/client/consul" cstructs "github.com/hashicorp/nomad/client/structs" @@ -26,7 +27,6 @@ import ( "github.com/boltdb/bolt" "github.com/hashicorp/consul/lib" - "github.com/hashicorp/go-hclog" "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/allocrunner" "github.com/hashicorp/nomad/client/config" @@ -212,16 +212,19 @@ func NewClient(cfg *config.Config, consulCatalog consul.CatalogAPI, consulServic } } + // Create the logger + logger := cfg.Logger.ResetNamed("") + // Create the client c := &Client{ config: cfg, consulCatalog: consulCatalog, consulService: consulService, start: time.Now(), - connPool: pool.NewPool(cfg.LogOutput, clientRPCCache, clientMaxStreams, tlsWrap), + connPool: pool.NewPool(logger, clientRPCCache, clientMaxStreams, tlsWrap), tlsWrap: tlsWrap, streamingRpcs: structs.NewStreamingRpcRegistry(), - logger: cfg.Logger.ResetNamed("").StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}), + logger: logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}), allocs: make(map[string]*allocrunner.AllocRunner), allocUpdates: make(chan *structs.Allocation, 64), shutdownCh: make(chan struct{}), diff --git a/helper/pool/pool.go b/helper/pool/pool.go index b1a57c2df..31a55dd6d 100644 --- a/helper/pool/pool.go +++ b/helper/pool/pool.go @@ -4,12 +4,14 @@ import ( "container/list" "fmt" "io" + "log" "net" "net/rpc" "sync" "sync/atomic" "time" + hclog "github.com/hashicorp/go-hclog" msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc" "github.com/hashicorp/nomad/helper/tlsutil" "github.com/hashicorp/nomad/nomad/structs" @@ -121,8 +123,8 @@ func (c *Conn) returnClient(client *StreamClient) { type ConnPool struct { sync.Mutex - // LogOutput is used to control logging - logOutput io.Writer + // logger is the logger to be used + logger *log.Logger // The maximum time to keep a connection open maxTime time.Duration @@ -156,9 +158,9 @@ type ConnPool struct { // Set maxTime to 0 to disable reaping. maxStreams is used to control // the number of idle streams allowed. // If TLS settings are provided outgoing connections use TLS. -func NewPool(logOutput io.Writer, maxTime time.Duration, maxStreams int, tlsWrap tlsutil.RegionWrapper) *ConnPool { +func NewPool(logger hclog.Logger, maxTime time.Duration, maxStreams int, tlsWrap tlsutil.RegionWrapper) *ConnPool { pool := &ConnPool{ - logOutput: logOutput, + logger: logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}), maxTime: maxTime, maxStreams: maxStreams, pool: make(map[string]*Conn), @@ -335,7 +337,8 @@ func (p *ConnPool) getNewConn(region string, addr net.Addr, version int) (*Conn, // Setup the logger conf := yamux.DefaultConfig() - conf.LogOutput = p.logOutput + conf.LogOutput = nil + conf.Logger = p.logger // Create a multiplexed session session, err := yamux.Client(conn, conf) diff --git a/helper/pool/pool_test.go b/helper/pool/pool_test.go index becf7d468..953c85745 100644 --- a/helper/pool/pool_test.go +++ b/helper/pool/pool_test.go @@ -14,8 +14,8 @@ import ( ) func newTestPool(t *testing.T) *ConnPool { - w := testlog.NewWriter(t) - p := NewPool(w, 1*time.Minute, 10, nil) + l := testlog.HCLogger(t) + p := NewPool(l, 1*time.Minute, 10, nil) return p } diff --git a/nomad/rpc.go b/nomad/rpc.go index 39baca7e0..7695217f0 100644 --- a/nomad/rpc.go +++ b/nomad/rpc.go @@ -13,6 +13,8 @@ import ( "strings" "time" + golog "log" + metrics "github.com/armon/go-metrics" log "github.com/hashicorp/go-hclog" memdb "github.com/hashicorp/go-memdb" @@ -47,13 +49,16 @@ const ( type rpcHandler struct { *Server - logger log.Logger + logger log.Logger + gologger *golog.Logger } func newRpcHandler(s *Server) *rpcHandler { + logger := s.logger.Named("rpc") return &rpcHandler{ - Server: s, - logger: s.logger.Named("rpc"), + Server: s, + logger: logger, + gologger: logger.StandardLogger(&log.StandardLoggerOptions{InferLevels: true}), } } @@ -207,7 +212,8 @@ func (r *rpcHandler) handleMultiplex(ctx context.Context, conn net.Conn, rpcCtx }() conf := yamux.DefaultConfig() - conf.LogOutput = r.config.LogOutput + conf.LogOutput = nil + conf.Logger = r.gologger server, err := yamux.Server(conn, conf) if err != nil { r.logger.Error("multiplex failed to create yamux server", "error", err) @@ -315,7 +321,8 @@ func (r *rpcHandler) handleMultiplexV2(ctx context.Context, conn net.Conn, rpcCt }() conf := yamux.DefaultConfig() - conf.LogOutput = r.config.LogOutput + conf.LogOutput = nil + conf.Logger = r.gologger server, err := yamux.Server(conn, conf) if err != nil { r.logger.Error("multiplex_v2 failed to create yamux server", "error", err) diff --git a/nomad/rpc_test.go b/nomad/rpc_test.go index ec885cc65..4af2d6750 100644 --- a/nomad/rpc_test.go +++ b/nomad/rpc_test.go @@ -297,7 +297,7 @@ func TestRPC_handleMultiplexV2(t *testing.T) { // Make two streams conf := yamux.DefaultConfig() - conf.LogOutput = testlog.NewWriter(t) + conf.Logger = testlog.Logger(t) session, err := yamux.Client(p1, conf) require.Nil(err) diff --git a/nomad/server.go b/nomad/server.go index af10e14ec..4bb10f346 100644 --- a/nomad/server.go +++ b/nomad/server.go @@ -286,12 +286,15 @@ func NewServer(config *Config, consulCatalog consul.CatalogAPI) (*Server, error) return nil, err } + // Create the logger + logger := config.Logger.ResetNamed("nomad") + // Create the server s := &Server{ config: config, consulCatalog: consulCatalog, - connPool: pool.NewPool(config.LogOutput, serverRPCCache, serverMaxStreams, tlsWrap), - logger: config.Logger.ResetNamed("nomad"), + connPool: pool.NewPool(logger, serverRPCCache, serverMaxStreams, tlsWrap), + logger: logger, tlsWrap: tlsWrap, rpcServer: rpc.NewServer(), streamingRpcs: structs.NewStreamingRpcRegistry(), @@ -1089,8 +1092,10 @@ func (s *Server) setupRaft() error { s.config.LogOutput) s.raftTransport = trans - // Make sure we set the LogOutput. - s.config.RaftConfig.LogOutput = s.config.LogOutput + // Make sure we set the Logger. + logger := s.logger.StandardLogger(&log.StandardLoggerOptions{InferLevels: true}) + s.config.RaftConfig.Logger = logger + s.config.RaftConfig.LogOutput = nil // Our version of Raft protocol requires the LocalID to match the network // address of the transport. @@ -1253,8 +1258,11 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string) ( if s.config.UpgradeVersion != "" { conf.Tags[AutopilotVersionTag] = s.config.UpgradeVersion } - conf.MemberlistConfig.LogOutput = s.config.LogOutput - conf.LogOutput = s.config.LogOutput + logger := s.logger.StandardLogger(&log.StandardLoggerOptions{InferLevels: true}) + conf.MemberlistConfig.Logger = logger + conf.Logger = logger + conf.MemberlistConfig.LogOutput = nil + conf.LogOutput = nil conf.EventCh = ch if !s.config.DevMode { conf.SnapshotPath = filepath.Join(s.config.DataDir, path) diff --git a/vendor/github.com/hashicorp/yamux/mux.go b/vendor/github.com/hashicorp/yamux/mux.go index 7abc7c744..18a078c8a 100644 --- a/vendor/github.com/hashicorp/yamux/mux.go +++ b/vendor/github.com/hashicorp/yamux/mux.go @@ -3,6 +3,7 @@ package yamux import ( "fmt" "io" + "log" "os" "time" ) @@ -30,8 +31,13 @@ type Config struct { // window size that we allow for a stream. MaxStreamWindowSize uint32 - // LogOutput is used to control the log destination + // LogOutput is used to control the log destination. Either Logger or + // LogOutput can be set, not both. LogOutput io.Writer + + // Logger is used to pass in the logger to be used. Either Logger or + // LogOutput can be set, not both. + Logger *log.Logger } // DefaultConfig is used to return a default configuration @@ -57,6 +63,11 @@ func VerifyConfig(config *Config) error { if config.MaxStreamWindowSize < initialStreamWindow { return fmt.Errorf("MaxStreamWindowSize must be larger than %d", initialStreamWindow) } + if config.LogOutput != nil && config.Logger != nil { + return fmt.Errorf("both Logger and LogOutput may not be set, select one") + } else if config.LogOutput == nil && config.Logger == nil { + return fmt.Errorf("one of Logger or LogOutput must be set, select one") + } return nil } diff --git a/vendor/github.com/hashicorp/yamux/session.go b/vendor/github.com/hashicorp/yamux/session.go index d8446fa65..a80ddec35 100644 --- a/vendor/github.com/hashicorp/yamux/session.go +++ b/vendor/github.com/hashicorp/yamux/session.go @@ -86,9 +86,14 @@ type sendReady struct { // newSession is used to construct a new session func newSession(config *Config, conn io.ReadWriteCloser, client bool) *Session { + logger := config.Logger + if logger == nil { + logger = log.New(config.LogOutput, "", log.LstdFlags) + } + s := &Session{ config: config, - logger: log.New(config.LogOutput, "", log.LstdFlags), + logger: logger, conn: conn, bufRead: bufio.NewReader(conn), pings: make(map[uint32]chan struct{}), @@ -309,8 +314,10 @@ func (s *Session) keepalive() { case <-time.After(s.config.KeepAliveInterval): _, err := s.Ping() if err != nil { - s.logger.Printf("[ERR] yamux: keepalive failed: %v", err) - s.exitErr(ErrKeepAliveTimeout) + if err != ErrSessionShutdown { + s.logger.Printf("[ERR] yamux: keepalive failed: %v", err) + s.exitErr(ErrKeepAliveTimeout) + } return } case <-s.shutdownCh: diff --git a/vendor/vendor.json b/vendor/vendor.json index 98a87c833..e9796f46c 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -202,7 +202,7 @@ {"path":"github.com/hashicorp/vault/helper/jsonutil","checksumSHA1":"POgkM3GrjRFw6H3sw95YNEs552A=","revision":"8575f8fedcf8f5a6eb2b4701cb527b99574b5286","revisionTime":"2018-09-06T17:45:45Z"}, {"path":"github.com/hashicorp/vault/helper/parseutil","checksumSHA1":"HA2MV/2XI0HcoThSRxQCaBZR2ps=","revision":"8575f8fedcf8f5a6eb2b4701cb527b99574b5286","revisionTime":"2018-09-06T17:45:45Z"}, {"path":"github.com/hashicorp/vault/helper/strutil","checksumSHA1":"HdVuYhZ5TuxeIFqi0jy2GHW7a4o=","revision":"8575f8fedcf8f5a6eb2b4701cb527b99574b5286","revisionTime":"2018-09-06T17:45:45Z"}, - {"path":"github.com/hashicorp/yamux","checksumSHA1":"NnWv17i1tpvBNJtpdRRWpE6j4LY=","revision":"2658be15c5f05e76244154714161f17e3e77de2e","revisionTime":"2018-03-14T20:07:45Z"}, + {"path":"github.com/hashicorp/yamux","checksumSHA1":"m9OKUPd/iliwKxs+LCSmAGpDJOs=","revision":"7221087c3d281fda5f794e28c2ea4c6e4d5c4558","revisionTime":"2018-09-17T20:50:41Z"}, {"path":"github.com/hpcloud/tail/util","checksumSHA1":"0xM336Lb25URO/1W1/CtGoRygVU=","revision":"37f4271387456dd1bf82ab1ad9229f060cc45386","revisionTime":"2017-08-14T16:06:53Z"}, {"path":"github.com/hpcloud/tail/watch","checksumSHA1":"TP4OAv5JMtzj2TB6OQBKqauaKDc=","revision":"37f4271387456dd1bf82ab1ad9229f060cc45386","revisionTime":"2017-08-14T16:06:53Z"}, {"path":"github.com/jmespath/go-jmespath","comment":"0.2.2-2-gc01cf91","revision":"c01cf91b011868172fdcd9f41838e80c9d716264"},