2021-01-18 19:00:32 +00:00
|
|
|
package funcs
|
|
|
|
|
|
|
|
import (
|
2021-02-14 10:58:51 +00:00
|
|
|
"context"
|
2021-11-07 17:51:39 +00:00
|
|
|
"net"
|
2021-01-18 19:00:32 +00:00
|
|
|
|
2021-02-14 10:58:51 +00:00
|
|
|
"github.com/jackc/pgx/v4"
|
2022-11-05 07:29:17 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-01-18 19:00:32 +00:00
|
|
|
)
|
|
|
|
|
2022-11-05 07:29:17 +00:00
|
|
|
// PGXLogger is an struct to intergrate the Logger Interface of pgx
|
2021-02-14 10:58:51 +00:00
|
|
|
type PGXLogger struct{}
|
2021-01-18 19:00:32 +00:00
|
|
|
|
2021-02-14 10:58:51 +00:00
|
|
|
// Log sends the logging data to phuslu/log
|
|
|
|
func (PGXLogger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
|
2022-11-05 07:29:17 +00:00
|
|
|
var entry *zerolog.Event
|
2021-02-14 10:58:51 +00:00
|
|
|
switch level {
|
|
|
|
case pgx.LogLevelDebug:
|
|
|
|
entry = log.Debug()
|
|
|
|
case pgx.LogLevelError:
|
|
|
|
entry = log.Error()
|
|
|
|
case pgx.LogLevelInfo:
|
|
|
|
entry = log.Info()
|
|
|
|
case pgx.LogLevelTrace:
|
|
|
|
entry = log.Trace()
|
|
|
|
case pgx.LogLevelWarn:
|
|
|
|
entry = log.Warn()
|
|
|
|
default:
|
|
|
|
entry = log.Info()
|
|
|
|
}
|
|
|
|
for key, value := range data {
|
|
|
|
entry = entry.Interface(key, value)
|
|
|
|
}
|
|
|
|
entry.Msg(msg)
|
2021-01-18 19:00:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 10:58:51 +00:00
|
|
|
var _ pgx.Logger = PGXLogger{}
|
2021-11-07 17:51:39 +00:00
|
|
|
|
|
|
|
// IsUnix tests if the address is an unix address. It returns false if its an tcp address.
|
|
|
|
func IsUnix(address string) bool {
|
|
|
|
if IsTCP(address) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := net.ResolveUnixAddr("unix", address); err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTCP tests if the address is an tcp address
|
|
|
|
func IsTCP(address string) bool {
|
|
|
|
_, err := net.ResolveTCPAddr("tcp", address)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|