[go: up one dir, main page]

Skip to content

Commit

Permalink
Merge pull request #38 from rizalgowandy/arwego/bug/logx_pgx
Browse files Browse the repository at this point in the history
Remove Redundant Spacing
  • Loading branch information
rizalgowandy committed May 8, 2022
2 parents 23fbc10 + 7a06fb4 commit 1e330ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
_ "github.com/rizalgowandy/gdk/pkg/jsonx"
_ "github.com/rizalgowandy/gdk/pkg/logx"
_ "github.com/rizalgowandy/gdk/pkg/netx"
_ "github.com/rizalgowandy/gdk/pkg/pagination"
_ "github.com/rizalgowandy/gdk/pkg/password"
_ "github.com/rizalgowandy/gdk/pkg/pprofx"
_ "github.com/rizalgowandy/gdk/pkg/queue/nsqx"
Expand Down
5 changes: 5 additions & 0 deletions pkg/logx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/jackc/pgx/v4"
"github.com/rizalgowandy/gdk/pkg/errorx/v2"
"github.com/rizalgowandy/gdk/pkg/regex"
)

func NewPGX() *PGX {
Expand All @@ -30,6 +31,10 @@ func (p *PGX) Log(
if ok {
query = strings.ReplaceAll(query, "\t", " ")
query = strings.ReplaceAll(query, "\n", " ")
if sanitized, replaceErr := regex.ReplaceAllString(`\s+`, query, " "); replaceErr == nil {
query = sanitized
}
query = strings.TrimSpace(query)
data["sql"] = query
}
}
Expand Down
39 changes: 39 additions & 0 deletions pkg/logx/pgx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package logx

import (
"context"
"testing"

"github.com/jackc/pgx/v4"
)

func TestPGX_Log(t *testing.T) {
type args struct {
ctx context.Context
level pgx.LogLevel
msg string
data map[string]interface{}
}
tests := []struct {
name string
args args
}{
{
name: "Success",
args: args{
ctx: context.Background(),
level: pgx.LogLevelInfo,
msg: "testing",
data: map[string]interface{}{
"sql": " INSERT INTO cronx_histories (\tid,\tcreated_at,\tname,\tstatus,\tstatus_code,\tstarted_at,\tfinished_at,\tlatency,\tmetadata ) VALUES (\t $1,\t $2,\t $3,\t $4,\t $5,\t $6,\t $7,\t $8,\t $9 )\n; ",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &PGX{}
p.Log(tt.args.ctx, tt.args.level, tt.args.msg, tt.args.data)
})
}
}

0 comments on commit 1e330ac

Please sign in to comment.