Skip to content

Commit

Permalink
Configure workers to log to a file (#59)
Browse files Browse the repository at this point in the history
* Configure workers to log to a file

As a best practice. Inspired by https://github.com/uber-go/zap/blob/845ca51d5b8d9fed9fe14f35ab13b6b160d5762d/FAQ.md#does-zap-support-log-rotation

* Removed redundant error check

* Make log file optional
  • Loading branch information
fkorotkov committed Mar 24, 2023
1 parent 67702ed commit f420268
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ require (
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
25 changes: 24 additions & 1 deletion internal/command/worker/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"github.com/cirruslabs/orchard/pkg/client"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

var ErrBootstrapTokenNotProvided = errors.New("no bootstrap token provided")

var bootstrapTokenRaw string
var logFilePath string

func newRunCommand() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -23,6 +26,8 @@ func newRunCommand() *cobra.Command {
}
cmd.PersistentFlags().StringVar(&bootstrapTokenRaw, "bootstrap-token", "",
"a bootstrap token retrieved via `orchard get bootstrap-token <service-account-name-for-workers>`")
cmd.PersistentFlags().StringVar(&logFilePath, "log-file", "",
"optional path to a file where logs (up to 100 Mb) will be written.")
return cmd
}

Expand All @@ -49,10 +54,11 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {
}

// Initialize the logger
logger, err := zap.NewProduction()
logger, err := createLogger()
if err != nil {
return err
}

defer func() {
if syncErr := logger.Sync(); syncErr != nil && err == nil {
err = syncErr
Expand All @@ -66,3 +72,20 @@ func runWorker(cmd *cobra.Command, args []string) (err error) {

return workerInstance.Run(cmd.Context())
}

func createLogger() (*zap.Logger, error) {
if logFilePath == "" {
return zap.NewProduction()
}

logFileWriter := zapcore.AddSync(&lumberjack.Logger{
Filename: logFilePath,
MaxSize: 100, // megabytes
})
core := zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
logFileWriter,
zap.InfoLevel,
)
return zap.New(core), nil
}

0 comments on commit f420268

Please sign in to comment.