Skip to content

Commit

Permalink
Logging refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Maya Sergeeva committed Jun 17, 2022
1 parent 3ced5ef commit 96ae01f
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 70 deletions.
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ var (
ErrSeedClassNameNotRegistered = errors.New("seed class name not registered")
ErrSeedClassIsNotValid = errors.New("seed class name not valid")
ErrSeedClassNotImplementInterface = errors.New("seed class name not implements commands.SeedInterface")
ErrBadContextValue = errors.New("context value is empty or has wrong type")
ErrBadContextValue = errors.New("context value is empty (not passed as pointer?) or don't implement interface")
)
27 changes: 23 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
module github.com/spacetab-io/commands-go

go 1.16
go 1.17

require (
github.com/jackc/pgx/v4 v4.13.0
github.com/pressly/goose v2.7.0+incompatible
github.com/spacetab-io/configuration-go v1.2.0
github.com/spacetab-io/configuration-structs-go v0.1.1
github.com/spacetab-io/logs-go/v2 v2.1.0
github.com/spacetab-io/configuration-structs-go/v2 v2.0.0-alpha2
github.com/spacetab-io/logs-go/v3 v3.0.0-alpha2
github.com/spf13/cobra v1.2.1
go.uber.org/zap v1.21.0
)

//replace github.com/spacetab-io/configuration-structs-go => ../configuration-structs-go
require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/getsentry/sentry-go v0.13.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
golang.org/x/text v0.3.7 // indirect
)
85 changes: 51 additions & 34 deletions go.sum

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions log/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package log

import (
"os"

cfgstructs "github.com/spacetab-io/configuration-structs-go/v2"
"github.com/spacetab-io/configuration-structs-go/v2/contracts"
log "github.com/spacetab-io/logs-go/v3"
"go.uber.org/zap"
)

var Logger, _ = log.Init(
&cfgstructs.Logs{Level: "debug", Format: "text", Colored: true, Caller: cfgstructs.CallerConfig{Show: true, SkipFrames: 1}, Sentry: nil},
"unknown", "uptimeMaster", "unknown", os.Stdout)

func Init(cfg contracts.LogsCfgInterface, stage, serviceAlias, serviceVersion string) (err error) {
l, err := log.Init(cfg, stage, serviceAlias, serviceVersion, os.Stdout)
if err != nil {
return err
}

Logger = l

return nil
}

func Debug() *log.Event { return Logger.Debug() }
func Info() *log.Event { return Logger.Info() }
func Warn() *log.Event { return Logger.Warn() }
func Error() *log.Event { return Logger.Error() }
func Fatal() *log.Event { return Logger.Fatal() }
func GetLogger() *zap.Logger {
l := *Logger.Logger
return &l
}
52 changes: 28 additions & 24 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"os"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/log/zerologadapter"
"github.com/jackc/pgx/v4/log/zapadapter"
"github.com/jackc/pgx/v4/stdlib"
"github.com/pressly/goose"
"github.com/spacetab-io/commands-go/log"
"github.com/spacetab-io/configuration-go/stage"
cfgstructs "github.com/spacetab-io/configuration-structs-go"
log "github.com/spacetab-io/logs-go/v2"
"github.com/spacetab-io/configuration-structs-go/v2/contracts"
log2 "github.com/spacetab-io/logs-go/v3"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -64,7 +65,7 @@ func migrate(cmd *cobra.Command, args []string) error {
return fmt.Errorf(CmdErrStrFormat, method, "getConfig", err)
}

if err := log.Init(appStage.String(), logCfg, appInfo.GetAlias(), appInfo.GetVersion(), os.Stdout); err != nil {
if err := log.Init(logCfg, appStage.String(), appInfo.GetAlias(), appInfo.GetVersion()); err != nil {
log.Error().Err(err).Send()

return fmt.Errorf(CmdErrStrFormat, method, "log.Init", err)
Expand All @@ -76,19 +77,20 @@ func migrate(cmd *cobra.Command, args []string) error {

log.Debug().Str("command", command).Strs("command args", args[0:]).Msg("run migrate command")

cfg, err := pgx.ParseConfig(dbCfg.GetDSN())
pgxConfig, err := pgx.ParseConfig(dbCfg.GetDSN())
if err != nil {
log.Error().Err(err).Str("dsn", dbCfg.GetMigrationDSN()).Msg("fail to parse config")

return fmt.Errorf(CmdErrStrFormat, method, "ParseConfig", err)
}

cfg.Logger = zerologadapter.NewLogger(log.Logger().With().CallerWithSkipFrameCount(4).Logger()) // nolint:gomnd
cfg.PreferSimpleProtocol = true
pgxConfig.Logger = zapadapter.NewLogger(log.GetLogger())
pgxConfig.RuntimeParams = map[string]string{"standard_conforming_strings": "on"}
pgxConfig.PreferSimpleProtocol = true

stdlib.RegisterConnConfig(cfg)
stdlib.RegisterConnConfig(pgxConfig)

db := stdlib.OpenDB(*cfg)
db := stdlib.OpenDB(*pgxConfig)

if err := goose.SetDialect("postgres"); err != nil {
return fmt.Errorf("migrate SetDialect error: %w", err)
Expand All @@ -97,6 +99,8 @@ func migrate(cmd *cobra.Command, args []string) error {
// set migrations table from cfg
goose.SetTableName(dbCfg.GetMigrationsTableName())

goose.SetLogger(log2.NewGooseLogger(log.Logger))

if err := db.Ping(); err != nil {
log.Error().Err(err).Str("dsn", dbCfg.GetDSN()).Msg("fail to ping database")

Expand Down Expand Up @@ -127,8 +131,8 @@ func migrate(cmd *cobra.Command, args []string) error {
return goose.Run(command, db, dbCfg.GetMigrationsPath(), arguments...)
}

func checkInit(cfg cfgstructs.DatabaseCfgInterface, db *sql.DB) error {
log.Trace().Msg("detect goose table exists")
func checkInit(cfg contracts.DatabaseCfgInterface, db *sql.DB) error {
log.Debug().Msg("detect goose table exists")

var t *string

Expand All @@ -154,47 +158,47 @@ INSERT INTO %s.%s ("version_id", "is_applied", "tstamp") VALUES ('0', 't', NOW()
)

if t == nil {
log.Trace().Msg("goose table doesn't exists. let's create it")
log.Debug().Msg("goose table doesn't exists. let's create it")

if _, err := db.Exec(create); err != nil {
return fmt.Errorf("checkInit db.Exec error: %w", err)
}

log.Trace().Msg("goose table now exists. continue")
log.Debug().Msg("goose table now exists. continue")

return nil
}

log.Trace().Msg("goose table exists. go forward")
log.Debug().Msg("goose table exists. go forward")

return nil
}

func getConfigs(ctx context.Context) (
stage.Interface,
cfgstructs.DatabaseCfgInterface,
log.Config,
cfgstructs.ApplicationInfoCfgInterface,
contracts.DatabaseCfgInterface,
contracts.LogsCfgInterface,
contracts.ApplicationInfoCfgInterface,
error,
) {
appStage, ok := ctx.Value(CommandContextCfgKeyStage).(stage.Interface)
if !ok {
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: stage name (cfg.envStage)", ErrBadContextValue)
return nil, nil, nil, nil, fmt.Errorf("%w: stage name (cfg.envStage)", ErrBadContextValue)
}

dbCfg, ok := ctx.Value(CommandContextCfgKeyDB).(cfgstructs.DatabaseCfgInterface)
logCfg, ok := ctx.Value(CommandContextCfgKeyLog).(contracts.LogsCfgInterface)
if !ok {
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: database config (cfg.db)", ErrBadContextValue)
return nil, nil, nil, nil, fmt.Errorf("%w: log config (cfg.log)", ErrBadContextValue)
}

logCfg, ok := ctx.Value(CommandContextCfgKeyLog).(log.Config)
appInfoCfg, ok := ctx.Value(CommandContextCfgKeyAppInfo).(contracts.ApplicationInfoCfgInterface)
if !ok {
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: log config (cfg.log)", ErrBadContextValue)
return nil, nil, nil, nil, fmt.Errorf("%w: app info config (cfg.appInfo)", ErrBadContextValue)
}

appInfoCfg, ok := ctx.Value(CommandContextCfgKeyAppInfo).(cfgstructs.ApplicationInfoCfgInterface)
dbCfg, ok := ctx.Value(CommandContextCfgKeyDB).(contracts.DatabaseCfgInterface)
if !ok {
return nil, nil, log.Config{}, nil, fmt.Errorf("%w: app info config (cfg.appInfo)", ErrBadContextValue)
return nil, nil, nil, nil, fmt.Errorf("%w: database config (cfg.db)", ErrBadContextValue)
}

return appStage, dbCfg, logCfg, appInfoCfg, nil
Expand Down
6 changes: 3 additions & 3 deletions seed.cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

log "github.com/spacetab-io/logs-go/v2"
"github.com/spacetab-io/commands-go/log"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -69,7 +69,7 @@ func seedRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("seedRun getAppSeeder() error: %w", err)
}

log.Trace().Strs("seeds", args).Msg("Running seeder...")
log.Debug().Strs("seeds", args).Msg("Running seeder...")

// Execute only the given method names
for _, item := range args {
Expand All @@ -93,7 +93,7 @@ func seedRunAll(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("seedRunAll getAppSeeder() error: %w", err)
}

log.Trace().Msg("Running all seeder...")
log.Debug().Msg("Running all seeder...")

// We are looping over the method on a Seeder struct
for _, seed := range s.GetMethods() {
Expand Down
2 changes: 1 addition & 1 deletion seed.interface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package commands

import (
cfgstructs "github.com/spacetab-io/configuration-structs-go"
cfgstructs "github.com/spacetab-io/configuration-structs-go/v2"
)

type SeedInterface interface {
Expand Down
2 changes: 1 addition & 1 deletion seed.seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"reflect"

cfgstructs "github.com/spacetab-io/configuration-structs-go"
cfgstructs "github.com/spacetab-io/configuration-structs-go/v2"
)

type Seeder struct {
Expand Down
4 changes: 2 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package commands
import (
"fmt"

cfgstructs "github.com/spacetab-io/configuration-structs-go"
"github.com/spacetab-io/configuration-structs-go/v2/contracts"
"github.com/spf13/cobra"
)

var VersionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
RunE: func(cmd *cobra.Command, args []string) error {
appInfo, ok := cmd.Context().Value(CommandContextCfgKeyAppInfo).(cfgstructs.ApplicationInfoCfgInterface)
appInfo, ok := cmd.Context().Value(CommandContextCfgKeyAppInfo).(contracts.ApplicationInfoCfgInterface)
if !ok {
return fmt.Errorf("%w: app info config (cfg.appInfo)", ErrBadContextValue)
}
Expand Down

0 comments on commit 96ae01f

Please sign in to comment.