Skip to content

Commit

Permalink
feat: respect log level flag and allow per logger levels (#34)
Browse files Browse the repository at this point in the history
Removes hard coded log levels so the log-level flag now properly controls the
default log level.

Adds a log-level-named flag which takes a comma delimited list of logger:level pairs
that specify the log levels for specific loggers. Can also be set via the
VISOR_LOG_LEVEL_NAMED environment variable.

Fixes #32
  • Loading branch information
iand authored Sep 25, 2020
1 parent e6687aa commit f0dc231
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 26 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ func main() {
Name: "log-level",
EnvVars: []string{"GOLOG_LOG_LEVEL"},
Value: "debug",
Usage: "Set the default log level for all loggers to `LEVEL`",
},
&cli.StringFlag{
Name: "log-level-named",
EnvVars: []string{"VISOR_LOG_LEVEL_NAMED"},
Value: "",
Usage: "A comma delimited list of named loggers and log levels formatted as name:level, for example 'logger1:debug,logger2:info'",
},
&cli.BoolFlag{
Name: "tracing",
Expand Down
50 changes: 36 additions & 14 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
_ "net/http/pprof"
"strings"

logging "github.com/ipfs/go-log/v2"
_ "github.com/lib/pq"
Expand All @@ -30,13 +31,9 @@ var processCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
ll := cctx.String("log-level")
if err := logging.SetLogLevel("*", ll); err != nil {
return xerrors.Errorf("set log level: %w", err)
}

if err := logging.SetLogLevel("rpc", "error"); err != nil {
return xerrors.Errorf("set rpc log level: %w", err)
err := setupLogging(cctx)
if err != nil {
return xerrors.Errorf("setup logging: %w", err)
}

if cctx.Bool("tracing") {
Expand Down Expand Up @@ -92,13 +89,9 @@ var indexCmd = &cli.Command{
Name: "index",
Usage: "Index the lotus blockchain",
Action: func(cctx *cli.Context) error {
ll := cctx.String("log-level")
if err := logging.SetLogLevel("*", ll); err != nil {
return xerrors.Errorf("set log level: %w", err)
}

if err := logging.SetLogLevel("rpc", "error"); err != nil {
return xerrors.Errorf("set rpc log level: %w", err)
err := setupLogging(cctx)
if err != nil {
return xerrors.Errorf("setup logging: %w", err)
}

if cctx.Bool("tracing") {
Expand Down Expand Up @@ -212,3 +205,32 @@ func jaegerConfigFromCliContext(cctx *cli.Context) (*jaegerConfig, error) {

return &cfg, nil
}

func setupLogging(cctx *cli.Context) error {
ll := cctx.String("log-level")
if err := logging.SetLogLevel("*", ll); err != nil {
return xerrors.Errorf("set log level: %w", err)
}

if err := logging.SetLogLevel("rpc", "error"); err != nil {
return xerrors.Errorf("set rpc log level: %w", err)
}

llnamed := cctx.String("log-level-named")
if llnamed == "" {
return nil
}

for _, llname := range strings.Split(llnamed, ",") {
parts := strings.Split(llname, ":")
if len(parts) != 2 {
return xerrors.Errorf("invalid named log level format: %q", llname)
}
if err := logging.SetLogLevel(parts[0], parts[1]); err != nil {
return xerrors.Errorf("set named log level %q to %q: %w", parts[0], parts[1], err)
}

}

return nil
}
4 changes: 0 additions & 4 deletions services/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ type Processor struct {
}

func (p *Processor) InitHandler(ctx context.Context, batchSize int) error {
if err := logging.SetLogLevel("*", "debug"); err != nil {
return err
}

p.publisher.Start(ctx)
p.scheduler.Start()

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/common/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("commonactortask")
return next()
})
logging.SetLogLevel("commonactortask", "info")
// log all task
pool.Middleware((*ProcessActorTask).Log)

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("genesistask")
return next()
})
logging.SetLogLevel("genesistask", "info")
// log all task
pool.Middleware((*ProcessGenesisSingletonTask).Log)

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/init/init_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("markettask")
return next()
})
logging.SetLogLevel("markettask", "info")
// log all task
pool.Middleware((*ProcessInitActorTask).Log)

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("markettask")
return next()
})
logging.SetLogLevel("markettask", "info")
// log all task
pool.Middleware((*ProcessMarketTask).Log)

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("messagetask")
return next()
})
logging.SetLogLevel("messagetask", "info")
// log all task
pool.Middleware((*ProcessMessageTask).Log)

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("minertask")
return next()
})
logging.SetLogLevel("minertask", "info")
// log all task
pool.Middleware((*ProcessMinerTask).Log)

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/power/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("minertask")
return next()
})
logging.SetLogLevel("minertask", "info")
// log all task
pool.Middleware((*ProcessPowerTask).Log)

Expand Down
1 change: 0 additions & 1 deletion services/processor/tasks/reward/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func Setup(concurrency uint, taskName, poolName string, redisPool *redis.Pool, n
mt.log = logging.Logger("rewardtask")
return next()
})
logging.SetLogLevel("rewardtask", "debug")
// log all task
pool.Middleware((*ProcessRewardTask).Log)

Expand Down

0 comments on commit f0dc231

Please sign in to comment.