Commit 3615e331 authored by Janos Guljas's avatar Janos Guljas

use logrus for logging

parent 58507fc7
...@@ -15,10 +15,12 @@ import ( ...@@ -15,10 +15,12 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/janos/bee/pkg/api" "github.com/janos/bee/pkg/api"
"github.com/janos/bee/pkg/debugapi" "github.com/janos/bee/pkg/debugapi"
"github.com/janos/bee/pkg/logging"
"github.com/janos/bee/pkg/p2p/libp2p" "github.com/janos/bee/pkg/p2p/libp2p"
"github.com/janos/bee/pkg/pingpong" "github.com/janos/bee/pkg/pingpong"
) )
...@@ -45,6 +47,12 @@ func (c *command) initStartCmd() (err error) { ...@@ -45,6 +47,12 @@ func (c *command) initStartCmd() (err error) {
return cmd.Help() return cmd.Help()
} }
logger := logging.New(cmd.OutOrStdout())
logger.SetLevel(logrus.TraceLevel)
errorLogWriter := logger.WriterLevel(logrus.ErrorLevel)
defer errorLogWriter.Close()
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
...@@ -63,7 +71,10 @@ func (c *command) initStartCmd() (err error) { ...@@ -63,7 +71,10 @@ func (c *command) initStartCmd() (err error) {
} }
// Construct protocols. // Construct protocols.
pingPong := pingpong.New(p2ps) pingPong := pingpong.New(pingpong.Options{
Streamer: p2ps,
Logger: logger,
})
// Add protocols to the P2P service. // Add protocols to the P2P service.
if err = p2ps.AddProtocol(pingPong.Protocol()); err != nil { if err = p2ps.AddProtocol(pingPong.Protocol()); err != nil {
...@@ -76,7 +87,7 @@ func (c *command) initStartCmd() (err error) { ...@@ -76,7 +87,7 @@ func (c *command) initStartCmd() (err error) {
} }
for _, addr := range addrs { for _, addr := range addrs {
cmd.Println(addr) logger.Infof("address: %s", addr)
} }
// API server // API server
...@@ -88,13 +99,17 @@ func (c *command) initStartCmd() (err error) { ...@@ -88,13 +99,17 @@ func (c *command) initStartCmd() (err error) {
if err != nil { if err != nil {
return fmt.Errorf("api listener: %w", err) return fmt.Errorf("api listener: %w", err)
} }
apiServer := &http.Server{Handler: apiService}
apiServer := &http.Server{
Handler: apiService,
ErrorLog: log.New(errorLogWriter, "", 0),
}
go func() { go func() {
cmd.Println("api address:", apiListener.Addr()) logger.Infof("api address: %s", apiListener.Addr())
if err := apiServer.Serve(apiListener); err != nil && err != http.ErrServerClosed { if err := apiServer.Serve(apiListener); err != nil && err != http.ErrServerClosed {
log.Println("api server:", err) logger.Errorf("api server: %v", err)
} }
}() }()
...@@ -112,13 +127,16 @@ func (c *command) initStartCmd() (err error) { ...@@ -112,13 +127,16 @@ func (c *command) initStartCmd() (err error) {
return fmt.Errorf("debug api listener: %w", err) return fmt.Errorf("debug api listener: %w", err)
} }
debugAPIServer := &http.Server{Handler: debugAPIService} debugAPIServer := &http.Server{
Handler: debugAPIService,
ErrorLog: log.New(errorLogWriter, "", 0),
}
go func() { go func() {
cmd.Println("debug api address:", debugAPIListener.Addr()) logger.Infof("debug api address: %s", debugAPIListener.Addr())
if err := debugAPIServer.Serve(debugAPIListener); err != nil && err != http.ErrServerClosed { if err := debugAPIServer.Serve(debugAPIListener); err != nil && err != http.ErrServerClosed {
log.Println("debug api server:", err) logger.Errorf("debug api server: %v", err)
} }
}() }()
} }
...@@ -131,14 +149,14 @@ func (c *command) initStartCmd() (err error) { ...@@ -131,14 +149,14 @@ func (c *command) initStartCmd() (err error) {
// Block main goroutine until it is interrupted // Block main goroutine until it is interrupted
sig := <-interruptChannel sig := <-interruptChannel
log.Println("received signal:", sig) logger.Debugf("received signal: %v", sig)
// Shutdown // Shutdown
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Println("shutdown panic:", err) logger.Errorf("shutdown panic: %v", err)
} }
}() }()
defer close(done) defer close(done)
...@@ -147,17 +165,17 @@ func (c *command) initStartCmd() (err error) { ...@@ -147,17 +165,17 @@ func (c *command) initStartCmd() (err error) {
defer cancel() defer cancel()
if err := apiServer.Shutdown(ctx); err != nil { if err := apiServer.Shutdown(ctx); err != nil {
log.Println("api server shutdown:", err) logger.Errorf("api server shutdown: %v", err)
} }
if debugAPIServer != nil { if debugAPIServer != nil {
if err := debugAPIServer.Shutdown(ctx); err != nil { if err := debugAPIServer.Shutdown(ctx); err != nil {
log.Println("debug api server shutdown:", err) logger.Errorf("debug api server shutdown: %v", err)
} }
} }
if err := p2ps.Close(); err != nil { if err := p2ps.Close(); err != nil {
log.Println("p2p server shutdown:", err) logger.Errorf("p2p server shutdown: %v", err)
} }
}() }()
...@@ -165,7 +183,7 @@ func (c *command) initStartCmd() (err error) { ...@@ -165,7 +183,7 @@ func (c *command) initStartCmd() (err error) {
// allow process termination by receiving another signal. // allow process termination by receiving another signal.
select { select {
case sig := <-interruptChannel: case sig := <-interruptChannel:
log.Printf("received signal: %v\n", sig) logger.Debugf("received signal: %v", sig)
case <-done: case <-done:
} }
......
...@@ -19,6 +19,7 @@ require ( ...@@ -19,6 +19,7 @@ require (
github.com/multiformats/go-multiaddr v0.2.0 github.com/multiformats/go-multiaddr v0.2.0
github.com/multiformats/go-multistream v0.1.0 github.com/multiformats/go-multistream v0.1.0
github.com/prometheus/client_golang v1.3.0 github.com/prometheus/client_golang v1.3.0
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.6.1 github.com/spf13/viper v1.6.1
resenje.org/web v0.4.0 resenje.org/web v0.4.0
......
This diff is collapsed.
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
import (
"io"
"github.com/sirupsen/logrus"
)
func New(w io.Writer) *logrus.Logger {
logger := logrus.New()
logger.SetOutput(w)
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
return logger
}
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"log"
"time" "time"
"github.com/janos/bee/pkg/p2p" "github.com/janos/bee/pkg/p2p"
...@@ -25,12 +24,24 @@ const ( ...@@ -25,12 +24,24 @@ const (
type Service struct { type Service struct {
streamer p2p.Streamer streamer p2p.Streamer
logger Logger
metrics metrics metrics metrics
} }
func New(streamer p2p.Streamer) *Service { type Options struct {
Streamer p2p.Streamer
Logger Logger
}
type Logger interface {
Debugf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
func New(o Options) *Service {
return &Service{ return &Service{
streamer: streamer, streamer: o.Streamer,
logger: o.Logger,
metrics: newMetrics(), metrics: newMetrics(),
} }
} }
...@@ -74,7 +85,7 @@ func (s *Service) Ping(ctx context.Context, peerID string, msgs ...string) (rtt ...@@ -74,7 +85,7 @@ func (s *Service) Ping(ctx context.Context, peerID string, msgs ...string) (rtt
return 0, err return 0, err
} }
log.Printf("got pong: %q\n", pong.Response) s.logger.Debugf("got pong: %q", pong.Response)
s.metrics.PongReceivedCount.Inc() s.metrics.PongReceivedCount.Inc()
} }
return time.Since(start) / time.Duration(len(msgs)), nil return time.Since(start) / time.Duration(len(msgs)), nil
...@@ -90,16 +101,16 @@ func (s *Service) Handler(p p2p.Peer) { ...@@ -90,16 +101,16 @@ func (s *Service) Handler(p p2p.Peer) {
if err == io.EOF { if err == io.EOF {
break break
} }
log.Printf("pingpong handler: read message: %v\n", err) s.logger.Errorf("pingpong handler: read message: %v\n", err)
return return
} }
log.Printf("got ping: %q\n", ping.Greeting) s.logger.Debugf("got ping: %q", ping.Greeting)
s.metrics.PingReceivedCount.Inc() s.metrics.PingReceivedCount.Inc()
if err := w.WriteMsg(&Pong{ if err := w.WriteMsg(&Pong{
Response: "{" + ping.Greeting + "}", Response: "{" + ping.Greeting + "}",
}); err != nil { }); err != nil {
log.Printf("pingpong handler: write message: %v\n", err) s.logger.Errorf("pingpong handler: write message: %v\n", err)
return return
} }
s.metrics.PongSentCount.Inc() s.metrics.PongSentCount.Inc()
......
...@@ -8,22 +8,31 @@ import ( ...@@ -8,22 +8,31 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil"
"testing" "testing"
"github.com/janos/bee/pkg/logging"
"github.com/janos/bee/pkg/p2p/mock" "github.com/janos/bee/pkg/p2p/mock"
"github.com/janos/bee/pkg/p2p/protobuf" "github.com/janos/bee/pkg/p2p/protobuf"
"github.com/janos/bee/pkg/pingpong" "github.com/janos/bee/pkg/pingpong"
) )
func TestPing(t *testing.T) { func TestPing(t *testing.T) {
logger := logging.New(ioutil.Discard)
// create a pingpong server that handles the incoming stream // create a pingpong server that handles the incoming stream
server := pingpong.New(nil) server := pingpong.New(pingpong.Options{
Logger: logger,
})
// setup the stream recorder to record stream data // setup the stream recorder to record stream data
recorder := mock.NewRecorder(server.Protocol()) recorder := mock.NewRecorder(server.Protocol())
// create a pingpong client that will do pinging // create a pingpong client that will do pinging
client := pingpong.New(recorder) client := pingpong.New(pingpong.Options{
Streamer: recorder,
Logger: logger,
})
// ping // ping
peerID := "/p2p/QmZt98UimwpW9ptJumKTq7B7t3FzNfyoWVNGcd8PFCd7XS" peerID := "/p2p/QmZt98UimwpW9ptJumKTq7B7t3FzNfyoWVNGcd8PFCd7XS"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment