Improve logging #9

Merged
amigan merged 3 commits from logging into trunk 2024-10-18 08:09:24 -04:00
7 changed files with 50 additions and 19 deletions
Showing only changes of commit c1e3eece4d - Show all commits

View file

@ -1,5 +1,8 @@
VER!=git describe --tags --always
HASH!=git describe --always
LDFLAGS=-ldflags="-X 'main.Version=${VER}' -X 'main.Commit=${HASH}'"
all: checkcalls all: checkcalls
go build -o gordio ./cmd/gordio/ go build -o gordio ${LDFLAGS} ./cmd/gordio/
go build -o calls ./cmd/calls/ go build -o calls ./cmd/calls/
clean: clean:

View file

@ -1,7 +1,9 @@
package main package main
import ( import (
"fmt"
"os" "os"
"runtime"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -13,15 +15,30 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
Version = "unset"
Commit = "unset"
)
func version() {
fmt.Printf("gordio %s (%s)\nbuilt for %s-%s\n",
Version, Commit, runtime.GOOS, runtime.GOARCH)
}
func main() { func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: gordio.AppName, Use: gordio.AppName,
} }
rootCmd.PersistentFlags().BoolP("version", "V", false, "show version")
cfg := config.New(rootCmd) cfg := config.New(rootCmd)
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { rootCmd.Run = func(cmd *cobra.Command, args []string) {
return cfg.ReadConfig() v, _ := rootCmd.PersistentFlags().GetBool("version")
if v {
version()
os.Exit(0)
}
} }
cmds := append([]*cobra.Command{gordio.Command(cfg)}, admin.Command(cfg)...) cmds := append([]*cobra.Command{gordio.Command(cfg)}, admin.Command(cfg)...)

View file

@ -120,6 +120,7 @@ func Command(cfg *config.Config) []*cobra.Command {
Use: "users", Use: "users",
Aliases: []string{"u"}, Aliases: []string{"u"},
Short: "administers the server", Short: "administers the server",
PersistentPreRunE: cfg.PreRunE(),
} }
userCmd.AddCommand(addUserCommand(cfg), passwdCommand(cfg)) userCmd.AddCommand(addUserCommand(cfg), passwdCommand(cfg))

View file

@ -39,9 +39,17 @@ type Logger struct {
Level *string `yaml:"level"` Level *string `yaml:"level"`
} }
func New(cmd *cobra.Command) *Config { func (c *Config) PreRunE() func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
return c.ReadConfig()
}
}
func New(rootCommand *cobra.Command) *Config {
c := &Config{} c := &Config{}
cmd.PersistentFlags().StringVarP(&c.configPath, "config", "c", "config.yaml", "configuration file")
rootCommand.PersistentFlags().StringVarP(&c.configPath, "config", "c", "config.yaml", "configuration file")
return c return c
} }

View file

@ -19,6 +19,7 @@ func Command(cfg *config.Config) *cobra.Command {
serveCmd := &cobra.Command{ serveCmd := &cobra.Command{
Use: "serve", Use: "serve",
Short: "starts the" + AppName + " server", Short: "starts the" + AppName + " server",
PersistentPreRunE: cfg.PreRunE(),
RunE: common.RunE(opts), RunE: common.RunE(opts),
} }

View file

@ -27,6 +27,7 @@ func NewLogger(cfg *config.Config) (*Logger, error) {
l := &Logger{ l := &Logger{
console: &zerolog.ConsoleWriter{Out: os.Stderr}, console: &zerolog.ConsoleWriter{Out: os.Stderr},
} }
l.hup = make(chan os.Signal, 1) l.hup = make(chan os.Signal, 1)
go func() { go func() {
for sig := range l.hup { for sig := range l.hup {