mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
setup slog inside cmd with debug and json flag
This commit is contained in:
@@ -25,6 +25,17 @@ func main() {
|
|||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "config",
|
Name: "config",
|
||||||
Value: "./config.yaml",
|
Value: "./config.yaml",
|
||||||
|
Usage: "path to config file",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "debug",
|
||||||
|
Value: false,
|
||||||
|
Usage: "set log level to DEBUG",
|
||||||
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "json",
|
||||||
|
Value: false,
|
||||||
|
Usage: "log using JSON",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(ctx context.Context, c *cli.Command) error {
|
Action: func(ctx context.Context, c *cli.Command) error {
|
||||||
@@ -37,13 +48,37 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logLevel := slog.LevelInfo
|
||||||
|
|
||||||
|
if c.Bool("debug") {
|
||||||
|
logLevel = slog.LevelDebug
|
||||||
|
}
|
||||||
|
|
||||||
|
logHandlerOptions := &slog.HandlerOptions{
|
||||||
|
Level: logLevel,
|
||||||
|
}
|
||||||
|
|
||||||
|
logOutput := os.Stderr
|
||||||
|
|
||||||
|
var logHandler slog.Handler = slog.NewTextHandler(logOutput, logHandlerOptions)
|
||||||
|
|
||||||
|
if c.Bool("json") {
|
||||||
|
logHandler = slog.NewJSONHandler(logOutput, logHandlerOptions)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := slog.New(logHandler)
|
||||||
|
|
||||||
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config)
|
router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config)
|
||||||
|
|
||||||
for _, moduleError := range moduleErrors {
|
for _, moduleError := range moduleErrors {
|
||||||
slog.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
logger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, routeError := range routeErrors {
|
for _, routeError := range routeErrors {
|
||||||
slog.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
logger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||||
}
|
}
|
||||||
router.Run()
|
router.Run()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
20
router.go
20
router.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
@@ -18,16 +17,10 @@ type Router struct {
|
|||||||
ModuleInstances []module.Module
|
ModuleInstances []module.Module
|
||||||
RouteInstances []route.Route
|
RouteInstances []route.Route
|
||||||
moduleWait sync.WaitGroup
|
moduleWait sync.WaitGroup
|
||||||
|
logger *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouter(ctx context.Context, config config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
func NewRouter(ctx context.Context, config config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
|
||||||
Level: slog.LevelInfo,
|
|
||||||
}))
|
|
||||||
|
|
||||||
slog.SetDefault(logger)
|
|
||||||
|
|
||||||
slog.Debug("creating router")
|
|
||||||
|
|
||||||
routerContext, cancel := context.WithCancel(ctx)
|
routerContext, cancel := context.WithCancel(ctx)
|
||||||
router := Router{
|
router := Router{
|
||||||
@@ -35,8 +28,11 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
|||||||
contextCancel: cancel,
|
contextCancel: cancel,
|
||||||
ModuleInstances: []module.Module{},
|
ModuleInstances: []module.Module{},
|
||||||
RouteInstances: []route.Route{},
|
RouteInstances: []route.Route{},
|
||||||
|
logger: slog.Default().With("component", "router"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router.logger.Debug("creating router")
|
||||||
|
|
||||||
var moduleErrors []module.ModuleError
|
var moduleErrors []module.ModuleError
|
||||||
|
|
||||||
for moduleIndex, moduleDecl := range config.Modules {
|
for moduleIndex, moduleDecl := range config.Modules {
|
||||||
@@ -110,20 +106,20 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Run() {
|
func (r *Router) Run() {
|
||||||
slog.Info("running router")
|
r.logger.Info("running router")
|
||||||
for _, moduleInstance := range r.ModuleInstances {
|
for _, moduleInstance := range r.ModuleInstances {
|
||||||
r.moduleWait.Add(1)
|
r.moduleWait.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
err := moduleInstance.Run()
|
err := moduleInstance.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error encountered running module", "id", moduleInstance.Id(), "error", err)
|
r.logger.Error("error encountered running module", "error", err)
|
||||||
}
|
}
|
||||||
r.moduleWait.Done()
|
r.moduleWait.Done()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
<-r.Context.Done()
|
<-r.Context.Done()
|
||||||
r.moduleWait.Wait()
|
r.moduleWait.Wait()
|
||||||
slog.Info("router done")
|
r.logger.Info("router done")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Stop() {
|
func (r *Router) Stop() {
|
||||||
@@ -143,7 +139,7 @@ func (r *Router) HandleInput(sourceId string, payload any) []route.RouteIOError
|
|||||||
Index: routeIndex,
|
Index: routeIndex,
|
||||||
Error: err,
|
Error: err,
|
||||||
})
|
})
|
||||||
slog.Error("router unable to route input", "route", routeIndex, "source", sourceId, "error", err)
|
r.logger.Error("router unable to route input", "route", routeIndex, "source", sourceId, "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user