diff --git a/cmd/geth/config.go b/cmd/geth/config.go index c60b09efe..ae5332e30 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -265,6 +265,9 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { if ctx.IsSet(utils.StateDiffDBConnTimeout.Name) { pgConfig.ConnTimeout = time.Duration(ctx.Duration(utils.StateDiffDBConnTimeout.Name).Seconds()) } + if ctx.IsSet(utils.StateDiffLogStatements.Name) { + pgConfig.LogStatements = ctx.Bool(utils.StateDiffLogStatements.Name) + } indexerConfig = pgConfig case shared.DUMP: dumpTypeStr := ctx.String(utils.StateDiffDBDumpDst.Name) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 696b7be26..16c549fed 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -179,6 +179,7 @@ var ( utils.StateDiffWaitForSync, utils.StateDiffWatchedAddressesFilePath, utils.StateDiffUpsert, + utils.StateDiffLogStatements, configFileFlag, }, utils.NetworkFlags, utils.DatabasePathFlags) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 070c00409..2bdb3f8e1 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1078,6 +1078,11 @@ var ( Usage: "Should the statediff service overwrite data existing in the database?", Value: false, } + StateDiffLogStatements = &cli.BoolFlag{ + Name: "statediff.db.logstatements", + Usage: "Should the statediff service log all database statements?", + Value: false, + } StateDiffWritingFlag = &cli.BoolFlag{ Name: "statediff.writing", Usage: "Activates progressive writing of state diffs to database as new block are synced", diff --git a/statediff/indexer/database/sql/postgres/config.go b/statediff/indexer/database/sql/postgres/config.go index 07c426811..b5cdc02ab 100644 --- a/statediff/indexer/database/sql/postgres/config.go +++ b/statediff/indexer/database/sql/postgres/config.go @@ -70,6 +70,7 @@ type Config struct { MaxConnIdleTime time.Duration MaxConnLifetime time.Duration ConnTimeout time.Duration + LogStatements bool // node info params ID string diff --git a/statediff/indexer/database/sql/postgres/log_adapter.go b/statediff/indexer/database/sql/postgres/log_adapter.go new file mode 100644 index 000000000..c3ceead46 --- /dev/null +++ b/statediff/indexer/database/sql/postgres/log_adapter.go @@ -0,0 +1,61 @@ +// Copyright © 2023 Cerc + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package postgres + +import ( + "context" + + "github.com/ethereum/go-ethereum/log" + "github.com/jackc/pgx/v4" +) + +type LogAdapter struct { + l log.Logger +} + +func NewLogAdapter(l log.Logger) *LogAdapter { + return &LogAdapter{l: l} +} + +func (l *LogAdapter) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) { + var logger log.Logger + if data != nil { + var args = make([]interface{}, 0) + for key, value := range data { + if value != nil { + args = append(args, key, value) + } + } + logger = l.l.New(args...) + } else { + logger = l.l + } + + switch level { + case pgx.LogLevelTrace: + logger.Trace(msg) + case pgx.LogLevelDebug: + logger.Debug(msg) + case pgx.LogLevelInfo: + logger.Info(msg) + case pgx.LogLevelWarn: + logger.Warn(msg) + case pgx.LogLevelError: + logger.Error(msg) + default: + logger.New("INVALID_PGX_LOG_LEVEL", level).Error(msg) + } +} diff --git a/statediff/indexer/database/sql/postgres/pgx.go b/statediff/indexer/database/sql/postgres/pgx.go index 936a3765d..9f1c4d571 100644 --- a/statediff/indexer/database/sql/postgres/pgx.go +++ b/statediff/indexer/database/sql/postgres/pgx.go @@ -20,6 +20,8 @@ import ( "context" "time" + "github.com/ethereum/go-ethereum/log" + "github.com/georgysavva/scany/pgxscan" "github.com/jackc/pgconn" "github.com/jackc/pgx/v4" @@ -85,6 +87,11 @@ func MakeConfig(config Config) (*pgxpool.Config, error) { if config.MaxConnIdleTime != 0 { conf.MaxConnIdleTime = config.MaxConnIdleTime } + + if config.LogStatements { + conf.ConnConfig.Logger = NewLogAdapter(log.New()) + } + return conf, nil }