Skip to content

Commit

Permalink
Merge pull request #1831 from mesg-foundation/feature/improve-logs
Browse files Browse the repository at this point in the history
Switch to json logger in orchestrator and daemon
  • Loading branch information
antho1404 authored May 21, 2020
2 parents bf1767b + 28a2f36 commit f1945e7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 18 deletions.
14 changes: 1 addition & 13 deletions cmd/mesg-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/lcd"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/cosmos-sdk/x/bank"
bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli"
"github.com/mesg-foundation/engine/app"
Expand Down Expand Up @@ -54,7 +52,7 @@ func main() {
flags.LineBreak,
orchestratorCmd(cdc),
flags.LineBreak,
lcd.ServeCommand(cdc, registerRoutes),
ServeCommand(cdc),
flags.LineBreak,
keys.Commands(),
signCommand(),
Expand Down Expand Up @@ -131,16 +129,6 @@ func txCmd(cdc *amino.Codec) *cobra.Command {
return txCmd
}

// registerRoutes registers the routes from the different modules for the LCD.
// NOTE: details on the routes added for each module are in the module documentation
// NOTE: If making updates here you also need to update the test helper in client/lcd/test_helper.go
func registerRoutes(rs *lcd.RestServer) {
client.RegisterRoutes(rs.CliCtx, rs.Mux)
authrest.RegisterTxRoutes(rs.CliCtx, rs.Mux)
app.ModuleBasics.RegisterRESTRoutes(rs.CliCtx, rs.Mux)
cosmos.RegisterSimulateRoute(rs.CliCtx, rs.Mux)
}

func initConfig(cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(cli.HomeFlag)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mesg-cli/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func startOrchestratorCmd(cdc *codec.Codec) *cobra.Command {
return fmt.Errorf("chain-id is required. use flag --chain-id or config file")
}

logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger := log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout))
client, err := cliCtx.GetNode()
if err != nil {
return err
Expand Down
70 changes: 70 additions & 0 deletions cmd/mesg-cli/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"net"
"os"
"time"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/gorilla/mux"
"github.com/mesg-foundation/engine/app"
"github.com/mesg-foundation/engine/cosmos"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
)

// ServeCommand creates and starts the LCD server
// adapted version of function from https://github.com/cosmos/cosmos-sdk/blob/v0.38.3/client/lcd/root.go#L74-L100
func ServeCommand(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "rest-server",
Short: "Start LCD (light-client daemon), a local REST server",
RunE: func(cmd *cobra.Command, args []string) (err error) {
// new rest server
r := mux.NewRouter()
cliCtx := context.NewCLIContext().WithCodec(cdc)
logger := log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout)).With("module", "rest-server")

// register routes
client.RegisterRoutes(cliCtx, r)
authrest.RegisterTxRoutes(cliCtx, r)
app.ModuleBasics.RegisterRESTRoutes(cliCtx, r)
cosmos.RegisterSimulateRoute(cliCtx, r)

// start
var listener net.Listener
server.TrapSignal(func() {
err := listener.Close()
logger.Error("error closing listener", "err", err)
})

cfg := rpcserver.DefaultConfig()
cfg.MaxOpenConnections = viper.GetInt(flags.FlagMaxOpenConnections)
cfg.ReadTimeout = time.Duration(uint(viper.GetInt(flags.FlagRPCReadTimeout))) * time.Second
cfg.WriteTimeout = time.Duration(uint(viper.GetInt(flags.FlagRPCWriteTimeout))) * time.Second

listener, err = rpcserver.Listen(viper.GetString(flags.FlagListenAddr), cfg)
if err != nil {
return
}
logger.Info(
fmt.Sprintf(
"Starting application REST service (chain-id: %q)...",
viper.GetString(flags.FlagChainID),
),
)

return rpcserver.StartHTTPServer(listener, r, logger, cfg)
},
}

return flags.RegisterRestServerFlags(cmd)
}
29 changes: 25 additions & 4 deletions cmd/mesg-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"io"
"os"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/debug"
Expand All @@ -19,7 +20,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
tmflags "github.com/tendermint/tendermint/libs/cli/flags"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
Expand All @@ -35,12 +38,30 @@ func main() {
// init the config of cosmos
cosmos.InitConfig()

ctx := server.NewDefaultContext()
ctx := server.NewContext(
cfg.DefaultConfig(),
log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout)),
)
cobra.EnableCommandSorting = false
rootCmd := &cobra.Command{
Use: version.ServerName,
Short: "Engine Daemon (server)",
PersistentPreRunE: server.PersistentPreRunEFn(ctx),
Use: version.ServerName,
Short: "Engine Daemon (server)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// adapted version of function from https://github.com/cosmos/cosmos-sdk/blob/v0.38.3/server/util.go#L49-L74
if err := server.PersistentPreRunEFn(ctx)(cmd, args); err != nil {
return err
}
logger, err := tmflags.ParseLogLevel(ctx.Config.LogLevel, log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout)), cfg.DefaultLogLevel())
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
ctx.Logger = logger
return nil
},
}

rootCmd.AddCommand(genutilcli.InitCmd(ctx, cdc, app.ModuleBasics, app.DefaultNodeHome))
Expand Down

0 comments on commit f1945e7

Please sign in to comment.