mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 04:16:25 +08:00
Add logging for service
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/datarhei/core/v16/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type API interface {
|
type API interface {
|
||||||
@@ -19,6 +21,7 @@ type Config struct {
|
|||||||
URL string
|
URL string
|
||||||
Token string
|
Token string
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
|
Logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type api struct {
|
type api struct {
|
||||||
@@ -29,6 +32,8 @@ type api struct {
|
|||||||
accessTokenType string
|
accessTokenType string
|
||||||
|
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
|
||||||
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config Config) (API, error) {
|
func New(config Config) (API, error) {
|
||||||
@@ -36,6 +41,11 @@ func New(config Config) (API, error) {
|
|||||||
url: config.URL,
|
url: config.URL,
|
||||||
token: config.Token,
|
token: config.Token,
|
||||||
client: config.Client,
|
client: config.Client,
|
||||||
|
logger: config.Logger,
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.logger == nil {
|
||||||
|
a.logger = log.New("")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasSuffix(a.url, "/") {
|
if !strings.HasSuffix(a.url, "/") {
|
||||||
@@ -95,7 +105,7 @@ func (c *copyReader) Read(p []byte) (int, error) {
|
|||||||
|
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
c.reader = c.copy
|
c.reader = c.copy
|
||||||
c.copy = new(bytes.Buffer)
|
c.copy = &bytes.Buffer{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return i, err
|
return i, err
|
||||||
|
@@ -55,7 +55,7 @@ func New(config Config) (Service, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.logger == nil {
|
if s.logger == nil {
|
||||||
s.logger = log.New("Service")
|
s.logger = log.New("")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger = s.logger.WithField("url", config.URL)
|
s.logger = s.logger.WithField("url", config.URL)
|
||||||
@@ -214,7 +214,10 @@ func (s *service) collect() (time.Duration, error) {
|
|||||||
return 15 * time.Minute, fmt.Errorf("failed to send monitor data to service: %w", err)
|
return 15 * time.Minute, fmt.Errorf("failed to send monitor data to service: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger.Debug().WithField("next", r.Next).Log("Sent monitor data")
|
s.logger.Debug().WithFields(log.Fields{
|
||||||
|
"next": r.Next,
|
||||||
|
"data": data,
|
||||||
|
}).Log("Sent monitor data")
|
||||||
|
|
||||||
if r.Next == 0 {
|
if r.Next == 0 {
|
||||||
r.Next = 5 * 60
|
r.Next = 5 * 60
|
||||||
@@ -230,6 +233,8 @@ func (s *service) Start() {
|
|||||||
go s.tick(ctx, time.Second)
|
go s.tick(ctx, time.Second)
|
||||||
|
|
||||||
s.stopOnce = sync.Once{}
|
s.stopOnce = sync.Once{}
|
||||||
|
|
||||||
|
s.logger.Info().Log("Connected")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,6 +242,8 @@ func (s *service) Stop() {
|
|||||||
s.stopOnce.Do(func() {
|
s.stopOnce.Do(func() {
|
||||||
s.stopTicker()
|
s.stopTicker()
|
||||||
s.startOnce = sync.Once{}
|
s.startOnce = sync.Once{}
|
||||||
|
|
||||||
|
s.logger.Info().Log("Disconnected")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user