-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace logrus with zerolog, add linter (#115)
* Replace logrus with zerolog, add linter * Add default context logger * bugfix golangci.yaml * bump dependencies
- Loading branch information
Showing
14 changed files
with
133 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
run: | ||
timeout: 30m | ||
|
||
linters: | ||
enable: | ||
- errcheck | ||
- gosimple | ||
- govet | ||
- ineffassign | ||
- staticcheck | ||
- unused | ||
- zerologlint | ||
|
||
issues: | ||
max-same-issues: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/equinor/radix-cost-allocation/config" | ||
"github.com/equinor/radix-cost-allocation/run" | ||
_ "github.com/microsoft/go-mssqldb" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/vrischmann/envconfig" | ||
) | ||
|
||
var appConfig config.AppConfig | ||
|
||
func main() { | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGTERM) | ||
defer cancel() | ||
|
||
if err := envconfig.Init(&appConfig); err != nil { | ||
log.Fatal(err) | ||
log.Fatal().Err(err).Msg("Failed to initialize config") | ||
} | ||
|
||
logLevel, err := log.ParseLevel(appConfig.LogLevel) | ||
ctx, err := setupLogger(ctx, appConfig.LogLevel, appConfig.PrettyPrint) | ||
if err != nil { | ||
log.Warnf("Log level '%s' is not valid. Using 'info' level", appConfig.LogLevel) | ||
logLevel = log.InfoLevel | ||
log.Fatal().Err(err).Msg("Failed to initialize logger") | ||
} | ||
log.SetLevel(logLevel) | ||
|
||
stopCh := make(chan struct{}) | ||
|
||
go func() { | ||
if err := run.InitAndStartCollector(appConfig.SQL, appConfig.Schedule, appConfig.AppNameExcludeList, stopCh); err != nil { | ||
log.Fatal(err) | ||
if err := run.InitAndStartCollector(ctx, appConfig.SQL, appConfig.Schedule, appConfig.AppNameExcludeList); err != nil { | ||
log.Fatal().Msg(err.Error()) | ||
} | ||
}() | ||
|
||
sigTerm := make(chan os.Signal, 1) | ||
go func() { | ||
signal.Notify(sigTerm, syscall.SIGTERM) | ||
signal.Notify(sigTerm, syscall.SIGINT) | ||
}() | ||
<-ctx.Done() | ||
} | ||
|
||
<-sigTerm | ||
close(stopCh) | ||
func setupLogger(ctx context.Context, logLevel string, prettyPrint bool) (context.Context, error) { | ||
zerolog.DurationFieldUnit = time.Millisecond | ||
level, err := zerolog.ParseLevel(logLevel) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
zerolog.SetGlobalLevel(level) | ||
if prettyPrint { | ||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.TimeOnly}) | ||
} | ||
zerolog.DefaultContextLogger = &log.Logger | ||
ctx = log.Logger.WithContext(ctx) | ||
return ctx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package sync | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Syncer defines an interface with a Sync method | ||
type Syncer interface { | ||
Sync() error | ||
} | ||
|
||
var ErrSyncAlreadyRunning = errors.New("sync is already running") | ||
|
||
func NewSyncAlreadyRunningError(name string) error { | ||
return fmt.Errorf("%w: %s", ErrSyncAlreadyRunning, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cronlogger | ||
|
||
import ( | ||
"github.com/robfig/cron/v3" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
type CronLogger struct { | ||
logger *zerolog.Logger | ||
} | ||
|
||
var _ cron.Logger = CronLogger{} | ||
|
||
func New(logger *zerolog.Logger) CronLogger { | ||
return CronLogger{logger: logger} | ||
} | ||
|
||
func (c CronLogger) Info(msg string, v ...interface{}) { | ||
c.logger.Info().Msgf(msg, v...) | ||
} | ||
|
||
func (c CronLogger) Error(err error, msg string, v ...interface{}) { | ||
c.logger.Error().Err(err).Msgf(msg, v...) | ||
} |
Oops, something went wrong.