-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Layr-Labs/use-slogger
Use eigensdk Slogger and add default env vars
- Loading branch information
Showing
5 changed files
with
119 additions
and
11 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
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= |
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,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) | ||
} |
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