mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-28 05:45:30 +00:00
centralize module logger creation
This commit is contained in:
@@ -24,7 +24,7 @@ func init() {
|
|||||||
Type: "http.client",
|
Type: "http.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
|
|
||||||
return &HTTPClient{config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &HTTPClient{config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func init() {
|
|||||||
return nil, errors.New("http.server port must be uint16")
|
return nil, errors.New("http.server port must be uint16")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func init() {
|
|||||||
return nil, errors.New("gen.interval duration must be number")
|
return nil, errors.New("gen.interval duration must be number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func init() {
|
|||||||
return nil, errors.New("midi.input port must be a string")
|
return nil, errors.New("midi.input port must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MIDIInput{config: config, Port: portString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &MIDIInput{config: config, Port: portString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func init() {
|
|||||||
return nil, errors.New("midi.output port must be a string")
|
return nil, errors.New("midi.output port must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MIDIOutput{config: config, Port: portString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &MIDIOutput{config: config, Port: portString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package module
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
@@ -49,3 +50,7 @@ var (
|
|||||||
moduleRegistryMu sync.RWMutex
|
moduleRegistryMu sync.RWMutex
|
||||||
ModuleRegistry = make(map[string]ModuleRegistration)
|
ModuleRegistry = make(map[string]ModuleRegistration)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func CreateLogger(config config.ModuleConfig) *slog.Logger {
|
||||||
|
return slog.Default().With("component", "module", "id", config.Id, "type", config.Type)
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ func init() {
|
|||||||
return nil, errors.New("mqtt.client clientId must be string")
|
return nil, errors.New("mqtt.client clientId must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func init() {
|
|||||||
return nil, errors.New("nats.client subject must be string")
|
return nil, errors.New("nats.client subject must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func init() {
|
|||||||
Type: "psn.client",
|
Type: "psn.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
|
|
||||||
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ func (pc *PSNClient) Run() error {
|
|||||||
message := buffer[:numBytes]
|
message := buffer[:numBytes]
|
||||||
err := pc.decoder.Decode(message)
|
err := pc.decoder.Decode(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pc.logger.Error("psn.client problem decoding psn traffic", "error", err)
|
pc.logger.Error("problem decoding psn traffic", "error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pc.router != nil {
|
if pc.router != nil {
|
||||||
@@ -85,7 +85,7 @@ func (pc *PSNClient) Run() error {
|
|||||||
pc.router.HandleInput(pc.Id(), tracker)
|
pc.router.HandleInput(pc.Id(), tracker)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pc.logger.Error("psn.client has no router")
|
pc.logger.Error("has no router")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func init() {
|
|||||||
BaudRate: int(baudRateNum),
|
BaudRate: int(baudRateNum),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
userAgentString = specificTransportString
|
userAgentString = specificTransportString
|
||||||
}
|
}
|
||||||
return &SIPCallServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &SIPCallServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ func init() {
|
|||||||
if !strings.ContainsRune("0123456789*#ABCD", rune(separatorString[0])) {
|
if !strings.ContainsRune("0123456789*#ABCD", rune(separatorString[0])) {
|
||||||
return nil, errors.New("sip.dtmf.server separator must be a valid DTMF character")
|
return nil, errors.New("sip.dtmf.server separator must be a valid DTMF character")
|
||||||
}
|
}
|
||||||
return &SIPDTMFServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &SIPDTMFServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethod)
|
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethod)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func init() {
|
|||||||
return nil, errors.New("gen.timer duration must be a number")
|
return nil, errors.New("gen.timer duration must be a number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ func init() {
|
|||||||
bufferSizeNum = int(bufferSizeFloat)
|
bufferSizeNum = int(bufferSizeFloat)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UDPServer{Addr: addr, BufferSize: bufferSizeNum, config: config, ctx: ctx, router: router, logger: slog.Default().With("component", "module", "id", config.Id)}, nil
|
return &UDPServer{Addr: addr, BufferSize: bufferSizeNum, config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user