add logger to nats output
@@ -2,11 +2,13 @@ package nats_output
import (
"log"
+ "os"
"github.com/google/uuid"
"github.com/karimra/gnmiClient/outputs"
"github.com/mitchellh/mapstructure"
"github.com/nats-io/nats.go"
+ "github.com/prometheus/client_golang/prometheus"
)
func init() {
@@ -21,6 +23,8 @@ func init() {
type NatsOutput struct {
Cfg *Config
conn *nats.Conn
+ metrics []prometheus.Collector
+ logger *log.Logger
stopChan chan struct{}
}
@@ -34,7 +38,7 @@ type Config struct {
}
// Initialize //
-func (n *NatsOutput) Initialize(cfg map[string]interface{}) error {
+func (n *NatsOutput) Init(cfg map[string]interface{}, logger *log.Logger) error {
err := mapstructure.Decode(cfg, n.Cfg)
if err != nil {
return err
@@ -46,6 +50,11 @@ func (n *NatsOutput) Initialize(cfg map[string]interface{}) error {
if err != nil {
return err
}
+ n.logger = log.New(os.Stderr, "nats_output ", log.LstdFlags|log.Lmicroseconds)
+ if logger != nil {
+ n.logger.SetOutput(logger.Writer())
+ n.logger.SetFlags(logger.Flags())
+ }
n.stopChan = make(chan struct{})
return nil
}
@@ -57,6 +66,7 @@ func (n *NatsOutput) Write(b []byte) {
log.Printf("failed to write to nats subject '%s': %v", n.Cfg.Subject, err)
return
}
+ // n.logger.Printf("wrote %d bytes to nats_subject=%s", len(b), n.Cfg.Subject)
}
// Close //
@@ -66,6 +76,9 @@ func (n *NatsOutput) Close() error {
return nil
}
+// Metrics //
+func (n *NatsOutput) Metrics() []prometheus.Collector { return n.metrics }
+
func createNATSConn(c *Config) (*nats.Conn, error) {
opts := []nats.Option{
nats.Name(c.Name),
