Skip to content

Commit

Permalink
fix: add missing logger.Sync()
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent 4bb91f1 commit 8060c71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
5 changes: 2 additions & 3 deletions build/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"runtime/debug"
"time"

"github.com/JulesMike/spoty/logger"
"go.uber.org/fx"
)

Expand All @@ -22,7 +21,7 @@ type Info struct {
}

// New returns a new instance of Info.
func New(logger *logger.Logger) (*Info, error) {
func New() (*Info, error) {
bi, ok := debug.ReadBuildInfo()
if !ok {
return nil, fmt.Errorf("failed to read build info")
Expand All @@ -43,7 +42,7 @@ func New(logger *logger.Logger) (*Info, error) {
case "vcs.time":
hash, err := time.Parse(time.RFC3339, kv.Value)
if err != nil {
logger.Warnw("failed to parse vcs.time", "error", err, "value", kv.Value)
return nil, fmt.Errorf("failed to parse vcs.time: %w", err)
}

info.LastCommit = hash
Expand Down
11 changes: 2 additions & 9 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"os"

"github.com/JulesMike/spoty/build"
"github.com/JulesMike/spoty/config"
"github.com/JulesMike/spoty/logger"
"github.com/spf13/cobra"
)

Expand All @@ -15,17 +13,12 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Return the build information",
Run: func(cmd *cobra.Command, args []string) {
logger, err := logger.New(&config.Config{})
info, err := build.New()
if err != nil {
fmt.Printf("failed to start logger: %v", err)
fmt.Print(err)
os.Exit(1) //nolint:revive
}

info, err := build.New(logger)
if err != nil {
logger.Fatal(err)
}

fmt.Printf("Revision: %v\n", info.Revision)
fmt.Printf("Last Commit: %v\n", info.LastCommit)
fmt.Printf("Dirty Build: %v\n", info.DirtyBuild)
Expand Down
11 changes: 10 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"context"
"fmt"

"github.com/JulesMike/spoty/config"
Expand All @@ -20,7 +21,7 @@ type Logger struct {
}

// New creates a new Logger.
func New(cfg *config.Config) (*Logger, error) {
func New(lc fx.Lifecycle, cfg *config.Config) (*Logger, error) {
var (
logger *zap.Logger
err error
Expand All @@ -35,6 +36,14 @@ func New(cfg *config.Config) (*Logger, error) {
return nil, fmt.Errorf("failed to create logger: %w", err)
}

lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
logger.Sync()

return nil
},
})

otellogger := otelzap.New(logger)

return &Logger{otellogger.Sugar()}, nil
Expand Down

0 comments on commit 8060c71

Please sign in to comment.