Move internal/cmd to cmd
Some checks failed
Test and build / Build receiver (amd64, , linux) (push) Failing after 40s
Test and build / Build receiver (arm, 6, linux) (push) Failing after 41s
Test and build / Build receiver (arm, 7, linux) (push) Failing after 40s
Test and build / Build collector (push) Failing after 54s
Test and build / test (push) Successful in 59s
Some checks failed
Test and build / Build receiver (amd64, , linux) (push) Failing after 40s
Test and build / Build receiver (arm, 6, linux) (push) Failing after 41s
Test and build / Build receiver (arm, 7, linux) (push) Failing after 40s
Test and build / Build collector (push) Failing after 54s
Test and build / test (push) Successful in 59s
This commit is contained in:
69
cmd/logger.go
Normal file
69
cmd/logger.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v3"
|
||||
|
||||
"git.maze.io/go/ham/protocol/meshcore"
|
||||
"git.maze.io/ham/hamview"
|
||||
)
|
||||
|
||||
const (
|
||||
FlagQuiet = "quiet"
|
||||
FlagDebug = "debug"
|
||||
FlagTrace = "trace"
|
||||
)
|
||||
|
||||
func LoggerFlags() []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: FlagQuiet,
|
||||
Aliases: []string{"q"},
|
||||
Usage: "Disable informational logging",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: FlagDebug,
|
||||
Aliases: []string{"D"},
|
||||
Usage: "Enable debug level logging",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: FlagTrace,
|
||||
Aliases: []string{"T"},
|
||||
Usage: "Enable trace level logging",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func ConfigureLogging(logger **logrus.Logger) cli.BeforeFunc {
|
||||
return func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
||||
*logger = NewLogger(cmd)
|
||||
hamview.Logger = *logger
|
||||
return ctx, nil
|
||||
}
|
||||
}
|
||||
|
||||
func NewLogger(cmd *cli.Command) *logrus.Logger {
|
||||
logger := logrus.New()
|
||||
logger.SetFormatter(&logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
TimestampFormat: time.RFC3339,
|
||||
})
|
||||
|
||||
if cmd != nil {
|
||||
if cmd.Bool(FlagTrace) {
|
||||
logger.SetLevel(logrus.TraceLevel)
|
||||
} else if cmd.Bool(FlagDebug) {
|
||||
logger.SetLevel(logrus.DebugLevel)
|
||||
} else if cmd.Bool(FlagQuiet) {
|
||||
logger.SetLevel(logrus.ErrorLevel)
|
||||
}
|
||||
}
|
||||
|
||||
// Update package loggers:
|
||||
meshcore.Logger = logger
|
||||
|
||||
return logger
|
||||
}
|
||||
Reference in New Issue
Block a user