Commit e2e2b491 authored by protolambda's avatar protolambda

op-service: make global log handler override explicit

parent 29c6f318
...@@ -34,6 +34,7 @@ func Main(version string) func(cliCtx *cli.Context) error { ...@@ -34,6 +34,7 @@ func Main(version string) func(cliCtx *cli.Context) error {
} }
l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.LogConfig) l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.LogConfig)
oplog.SetGlobalLogHandler(l.GetHandler())
endpointMonitor := NewEndpointMonitor(cfg, l) endpointMonitor := NewEndpointMonitor(cfg, l)
l.Info(fmt.Sprintf("starting endpoint monitor with checkInterval=%s checkDuration=%s", cfg.CheckInterval, cfg.CheckDuration)) l.Info(fmt.Sprintf("starting endpoint monitor with checkInterval=%s checkDuration=%s", cfg.CheckInterval, cfg.CheckDuration))
......
...@@ -5,7 +5,7 @@ import ( ...@@ -5,7 +5,7 @@ import (
"github.com/ethereum-optimism/optimism/indexer/api" "github.com/ethereum-optimism/optimism/indexer/api"
"github.com/ethereum-optimism/optimism/indexer/config" "github.com/ethereum-optimism/optimism/indexer/config"
"github.com/ethereum-optimism/optimism/indexer/database" "github.com/ethereum-optimism/optimism/indexer/database"
"github.com/ethereum-optimism/optimism/op-service/log" oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
...@@ -28,7 +28,8 @@ var ( ...@@ -28,7 +28,8 @@ var (
) )
func runIndexer(ctx *cli.Context) error { func runIndexer(ctx *cli.Context) error {
log := log.NewLogger(log.AppOut(ctx), log.ReadCLIConfig(ctx)).New("role", "indexer") log := oplog.NewLogger(oplog.AppOut(ctx), oplog.ReadCLIConfig(ctx)).New("role", "indexer")
oplog.SetGlobalLogHandler(log.GetHandler())
cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name)) cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name))
if err != nil { if err != nil {
log.Error("failed to load config", "err", err) log.Error("failed to load config", "err", err)
...@@ -52,7 +53,8 @@ func runIndexer(ctx *cli.Context) error { ...@@ -52,7 +53,8 @@ func runIndexer(ctx *cli.Context) error {
} }
func runApi(ctx *cli.Context) error { func runApi(ctx *cli.Context) error {
log := log.NewLogger(log.AppOut(ctx), log.ReadCLIConfig(ctx)).New("role", "api") log := oplog.NewLogger(oplog.AppOut(ctx), oplog.ReadCLIConfig(ctx)).New("role", "api")
oplog.SetGlobalLogHandler(log.GetHandler())
cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name)) cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name))
if err != nil { if err != nil {
log.Error("failed to load config", "err", err) log.Error("failed to load config", "err", err)
...@@ -71,7 +73,8 @@ func runApi(ctx *cli.Context) error { ...@@ -71,7 +73,8 @@ func runApi(ctx *cli.Context) error {
} }
func runMigrations(ctx *cli.Context) error { func runMigrations(ctx *cli.Context) error {
log := log.NewLogger(log.AppOut(ctx), log.ReadCLIConfig(ctx)).New("role", "api") log := oplog.NewLogger(oplog.AppOut(ctx), oplog.ReadCLIConfig(ctx)).New("role", "api")
oplog.SetGlobalLogHandler(log.GetHandler())
cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name)) cfg, err := config.LoadConfig(log, ctx.String(ConfigFlag.Name))
migrationsDir := ctx.String(MigrationsFlag.Name) migrationsDir := ctx.String(MigrationsFlag.Name)
if err != nil { if err != nil {
...@@ -91,9 +94,9 @@ func runMigrations(ctx *cli.Context) error { ...@@ -91,9 +94,9 @@ func runMigrations(ctx *cli.Context) error {
func newCli(GitCommit string, GitDate string) *cli.App { func newCli(GitCommit string, GitDate string) *cli.App {
flags := []cli.Flag{ConfigFlag} flags := []cli.Flag{ConfigFlag}
flags = append(flags, log.CLIFlags("INDEXER")...) flags = append(flags, oplog.CLIFlags("INDEXER")...)
migrationFlags := []cli.Flag{MigrationsFlag, ConfigFlag} migrationFlags := []cli.Flag{MigrationsFlag, ConfigFlag}
migrationFlags = append(migrationFlags, log.CLIFlags("INDEXER")...) migrationFlags = append(migrationFlags, oplog.CLIFlags("INDEXER")...)
return &cli.App{ return &cli.App{
Version: params.VersionWithCommit(GitCommit, GitDate), Version: params.VersionWithCommit(GitCommit, GitDate),
Description: "An indexer of all optimism events with a serving api layer", Description: "An indexer of all optimism events with a serving api layer",
......
...@@ -29,6 +29,7 @@ func Main(version string, cliCtx *cli.Context) error { ...@@ -29,6 +29,7 @@ func Main(version string, cliCtx *cli.Context) error {
cfg := NewConfig(cliCtx) cfg := NewConfig(cliCtx)
l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.LogConfig) l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.LogConfig)
oplog.SetGlobalLogHandler(l.GetHandler())
opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, l) opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, l)
m := metrics.NewMetrics("default") m := metrics.NewMetrics("default")
l.Info("Initializing Batch Submitter") l.Info("Initializing Batch Submitter")
......
...@@ -44,6 +44,7 @@ func Main(cliCtx *cli.Context) error { ...@@ -44,6 +44,7 @@ func Main(cliCtx *cli.Context) error {
log.Info("Initializing bootnode") log.Info("Initializing bootnode")
logCfg := oplog.ReadCLIConfig(cliCtx) logCfg := oplog.ReadCLIConfig(cliCtx)
logger := oplog.NewLogger(oplog.AppOut(cliCtx), logCfg) logger := oplog.NewLogger(oplog.AppOut(cliCtx), logCfg)
oplog.SetGlobalLogHandler(logger.GetHandler())
m := metrics.NewMetrics("default") m := metrics.NewMetrics("default")
ctx := context.Background() ctx := context.Background()
......
...@@ -71,5 +71,6 @@ func run(args []string, action ConfigAction) error { ...@@ -71,5 +71,6 @@ func run(args []string, action ConfigAction) error {
func setupLogging(ctx *cli.Context) (log.Logger, error) { func setupLogging(ctx *cli.Context) (log.Logger, error) {
logCfg := oplog.ReadCLIConfig(ctx) logCfg := oplog.ReadCLIConfig(ctx)
logger := oplog.NewLogger(oplog.AppOut(ctx), logCfg) logger := oplog.NewLogger(oplog.AppOut(ctx), logCfg)
oplog.SetGlobalLogHandler(logger.GetHandler())
return logger, nil return logger, nil
} }
...@@ -38,6 +38,7 @@ func Main(version string) func(ctx *cli.Context) error { ...@@ -38,6 +38,7 @@ func Main(version string) func(ctx *cli.Context) error {
} }
l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.Log) l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.Log)
oplog.SetGlobalLogHandler(l.GetHandler())
l.Info("starting heartbeat monitor", "version", version) l.Info("starting heartbeat monitor", "version", version)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
......
...@@ -84,6 +84,7 @@ func RollupNodeMain(ctx *cli.Context) error { ...@@ -84,6 +84,7 @@ func RollupNodeMain(ctx *cli.Context) error {
log.Info("Initializing Rollup Node") log.Info("Initializing Rollup Node")
logCfg := oplog.ReadCLIConfig(ctx) logCfg := oplog.ReadCLIConfig(ctx)
log := oplog.NewLogger(oplog.AppOut(ctx), logCfg) log := oplog.NewLogger(oplog.AppOut(ctx), logCfg)
oplog.SetGlobalLogHandler(log.GetHandler())
opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, log) opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, log)
m := metrics.NewMetrics("default") m := metrics.NewMetrics("default")
......
...@@ -17,5 +17,6 @@ func main() { ...@@ -17,5 +17,6 @@ func main() {
Format: oplog.FormatLogFmt, Format: oplog.FormatLogFmt,
Color: false, Color: false,
}) })
oplog.SetGlobalLogHandler(logger.GetHandler())
client.Main(logger) client.Main(logger)
} }
...@@ -75,5 +75,6 @@ func run(args []string, action ConfigAction) error { ...@@ -75,5 +75,6 @@ func run(args []string, action ConfigAction) error {
func setupLogging(ctx *cli.Context) (log.Logger, error) { func setupLogging(ctx *cli.Context) (log.Logger, error) {
logCfg := oplog.ReadCLIConfig(ctx) logCfg := oplog.ReadCLIConfig(ctx)
logger := oplog.NewLogger(oplog.AppOut(ctx), logCfg) logger := oplog.NewLogger(oplog.AppOut(ctx), logCfg)
oplog.SetGlobalLogHandler(logger.GetHandler())
return logger, nil return logger, nil
} }
...@@ -44,6 +44,7 @@ func Main(version string, cliCtx *cli.Context) error { ...@@ -44,6 +44,7 @@ func Main(version string, cliCtx *cli.Context) error {
} }
l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.LogConfig) l := oplog.NewLogger(oplog.AppOut(cliCtx), cfg.LogConfig)
oplog.SetGlobalLogHandler(l.GetHandler())
opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, l) opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, l)
m := metrics.NewMetrics("default") m := metrics.NewMetrics("default")
l.Info("Initializing L2 Output Submitter") l.Info("Initializing L2 Output Submitter")
......
...@@ -159,6 +159,15 @@ func NewLogger(wr io.Writer, cfg CLIConfig) log.Logger { ...@@ -159,6 +159,15 @@ func NewLogger(wr io.Writer, cfg CLIConfig) log.Logger {
return logger return logger
} }
// SetGlobalLogHandler sets the log handles as the handler of the global default logger.
// The usage of this logger is strongly discouraged,
// as it does makes it difficult to distinguish different services in the same process, e.g. during tests.
// Geth and other components may use the global logger however,
// and it is thus recommended to set the global log handler to catch these logs.
func SetGlobalLogHandler(h log.Handler) {
log.Root().SetHandler(h)
}
// DefaultCLIConfig creates a default log configuration. // DefaultCLIConfig creates a default log configuration.
// Color defaults to true if terminal is detected. // Color defaults to true if terminal is detected.
func DefaultCLIConfig() CLIConfig { func DefaultCLIConfig() CLIConfig {
......
...@@ -399,6 +399,7 @@ var ( ...@@ -399,6 +399,7 @@ var (
Action: EngineAction(func(ctx *cli.Context, client client.RPC) error { Action: EngineAction(func(ctx *cli.Context, client client.RPC) error {
logCfg := oplog.ReadCLIConfig(ctx) logCfg := oplog.ReadCLIConfig(ctx)
l := oplog.NewLogger(oplog.AppOut(ctx), logCfg) l := oplog.NewLogger(oplog.AppOut(ctx), logCfg)
oplog.SetGlobalLogHandler(l.GetHandler())
settings := ParseBuildingArgs(ctx) settings := ParseBuildingArgs(ctx)
// TODO: finalize/safe flag // TODO: finalize/safe flag
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment