Skip to content

Commit

Permalink
Merge pull request #21 from Layr-Labs/use-slogger
Browse files Browse the repository at this point in the history
Use eigensdk Slogger and add default env vars
  • Loading branch information
ian-shim authored Mar 19, 2024
2 parents 98723bb + 4208b57 commit 32b567f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 11 deletions.
12 changes: 11 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Mandatory
AVS_SYNC_PRIVATE_KEY=
AVS_SYNC_REGISTRY_COORDINATOR_ADDR=0xd19d750531c5e95CcC5C9610bF119dDe3008A16D
AVS_SYNC_OPERATOR_STATE_RETRIEVER_ADDR=0x1b41CA79b86295e77Dd49f28DbB000286c022dfd
AVS_SYNC_ETH_HTTP_URL=http://localhost:8545
AVS_SYNC_SYNC_INTERVAL=24h

# Optional
AVS_SYNC_FIRST_SYNC_TIME=00:00:00 # this will make it run at midnight
AVS_SYNC_LOG_LEVEL=DEBUG
AVS_SYNC_LOG_FORMAT=text

# Either AVS_SYNC_ECDSA_PRIVATE_KEY or Fireblocks credentials are required
# If AVS_SYNC_ECDSA_PRIVATE_KEY is specified, the Fireblocks credentials are not required and will be ignored
# If AVS_SYNC_ECDSA_PRIVATE_KEY is not specified, all flags prefixed with AVS_SYNC_FIREBLOCKS are required
AVS_SYNC_ECDSA_PRIVATE_KEY=
AVS_SYNC_FIREBLOCKS_API_KEY=
AVS_SYNC_FIREBLOCKS_API_SECRET_PATH=
AVS_SYNC_FIREBLOCKS_API_URL=
AVS_SYNC_FIREBLOCKS_VAULT_ACCOUNT_NAME=
1 change: 1 addition & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var OptionalFlags = []cli.Flag{

func init() {
Flags = append(RequiredFlags, OptionalFlags...)
Flags = append(Flags, loggerFlags...)
}

// Flags contains the list of configuration options available to the binary.
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg6
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/Layr-Labs/eigensdk-go v0.1.2 h1:0CbdLMr9IPWFW3RPfPORFWQYgXMAa/K4tA8rncBQ/YY=
github.com/Layr-Labs/eigensdk-go v0.1.2/go.mod h1:J+d9zxN4VyMtchmsPzGASFcCjpnh1eT4aE2ggiqOz/g=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240314000707-2e4582065143 h1:rE3u3EFI3G/VVjxiXL5YbDBqyzZSNGnZcppwjaef0Dk=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240314000707-2e4582065143/go.mod h1:J+d9zxN4VyMtchmsPzGASFcCjpnh1eT4aE2ggiqOz/g=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240316050204-63be00407527 h1:jxEWXjCfmHT+yUG8VfEKlBjIgOQneBGGbtqwmwPpxtM=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240316050204-63be00407527/go.mod h1:J+d9zxN4VyMtchmsPzGASFcCjpnh1eT4aE2ggiqOz/g=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240316185645-e035f359ddfa h1:c19dWUGTbIhQxbVZko9CCikC3M151OeWNZOOcpjZxOs=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240316185645-e035f359ddfa/go.mod h1:J+d9zxN4VyMtchmsPzGASFcCjpnh1eT4aE2ggiqOz/g=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240318050546-8d038f135826 h1:KENAtjyjJu/wBkVihG2cVdQbEKl44LuPqrj8IVqUFeI=
github.com/Layr-Labs/eigensdk-go v0.1.3-0.20240318050546-8d038f135826/go.mod h1:J+d9zxN4VyMtchmsPzGASFcCjpnh1eT4aE2ggiqOz/g=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
Expand Down
102 changes: 102 additions & 0 deletions logger_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package main

import (
"fmt"
"io"
"log/slog"
"os"

"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/urfave/cli"
)

const (
pathFlagName = "log.path"
levelFlagName = "log.level"
formatFlagName = "log.format"
)

type LogFormat string

const (
JSONLogFormat LogFormat = "json"
TextLogFormat LogFormat = "text"
)

type LoggerConfig struct {
Format LogFormat
OutputWriter io.Writer
HandlerOpts slog.HandlerOptions
}

var loggerFlags = []cli.Flag{
cli.StringFlag{
Name: levelFlagName,
Usage: `The lowest log level that will be output. Accepted options are "debug", "info", "warn", "error"`,
Value: "info",
EnvVar: envVarPrefix + "LOG_LEVEL",
},
cli.StringFlag{
Name: pathFlagName,
Usage: "Path to file where logs will be written",
Value: "",
EnvVar: envVarPrefix + "LOG_PATH",
},
cli.StringFlag{
Name: formatFlagName,
Usage: "The format of the log file. Accepted options are 'json' and 'text'",
Value: "json",
EnvVar: envVarPrefix + "LOG_FORMAT",
},
}

func DefaultLoggerConfig() LoggerConfig {
return LoggerConfig{
Format: JSONLogFormat,
OutputWriter: os.Stdout,
HandlerOpts: slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
},
}
}

func ReadLoggerCLIConfig(ctx *cli.Context) (*LoggerConfig, error) {
cfg := DefaultLoggerConfig()
format := ctx.GlobalString(formatFlagName)
if format == "json" {
cfg.Format = JSONLogFormat
} else if format == "text" {
cfg.Format = TextLogFormat
} else {
return nil, fmt.Errorf("invalid log file format %s", format)
}

path := ctx.GlobalString(pathFlagName)
if path != "" {
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
cfg.OutputWriter = io.MultiWriter(os.Stdout, f)
}
logLevel := ctx.GlobalString(levelFlagName)
var level slog.Level
err := level.UnmarshalText([]byte(logLevel))
if err != nil {
panic("failed to parse log level " + logLevel)
}
cfg.HandlerOpts.Level = level

return &cfg, nil
}

func NewLogger(cfg LoggerConfig) (logging.Logger, error) {
if cfg.Format == JSONLogFormat {
return logging.NewSlogJsonLogger(cfg.OutputWriter, &cfg.HandlerOpts), nil
}
if cfg.Format == TextLogFormat {
return logging.NewSlogTextLogger(cfg.OutputWriter, &cfg.HandlerOpts), nil
}
return nil, fmt.Errorf("unknown log format: %s", cfg.Format)
}
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/Layr-Labs/eigensdk-go/chainio/clients/fireblocks"
walletsdk "github.com/Layr-Labs/eigensdk-go/chainio/clients/wallet"
"github.com/Layr-Labs/eigensdk-go/chainio/txmgr"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/signerv2"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -37,7 +36,11 @@ func main() {

func avsSyncMain(cliCtx *cli.Context) error {
log.Println("Registering Node")
logger, err := logging.NewZapLogger(logging.Development)
loggerConfig, err := ReadLoggerCLIConfig(cliCtx)
if err != nil {
return err
}
logger, err := NewLogger(*loggerConfig)
if err != nil {
return err
}
Expand Down

0 comments on commit 32b567f

Please sign in to comment.