2023-06-14 12:43:34 +00:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4"
|
2023-06-23 12:42:55 +00:00
|
|
|
|
|
|
|
"github.com/cerc-io/plugeth-statediff/utils/log"
|
2023-06-14 12:43:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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{}) {
|
2023-06-23 12:42:55 +00:00
|
|
|
args := make([]interface{}, 0)
|
|
|
|
for key, value := range data {
|
|
|
|
if value != nil {
|
|
|
|
args = append(args, key, value)
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 12:42:55 +00:00
|
|
|
logger := l.l
|
2023-06-14 12:43:34 +00:00
|
|
|
switch level {
|
|
|
|
case pgx.LogLevelTrace:
|
2023-06-23 12:42:55 +00:00
|
|
|
logger.Trace(msg, args...)
|
2023-06-14 12:43:34 +00:00
|
|
|
case pgx.LogLevelDebug:
|
2023-06-23 12:42:55 +00:00
|
|
|
logger.Debug(msg, args...)
|
2023-06-14 12:43:34 +00:00
|
|
|
case pgx.LogLevelInfo:
|
2023-06-23 12:42:55 +00:00
|
|
|
logger.Info(msg, args...)
|
2023-06-14 12:43:34 +00:00
|
|
|
case pgx.LogLevelWarn:
|
2023-06-23 12:42:55 +00:00
|
|
|
logger.Warn(msg, args...)
|
2023-06-14 12:43:34 +00:00
|
|
|
case pgx.LogLevelError:
|
2023-06-23 12:42:55 +00:00
|
|
|
logger.Error(msg, args...)
|
2023-06-14 12:43:34 +00:00
|
|
|
default:
|
2023-06-23 12:42:55 +00:00
|
|
|
logger.Error(msg, "INVALID_PGX_LOG_LEVEL", level)
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
}
|