Skip to content

Commit ec02362

Browse files
authored
fix: mod grpc/status default logger output (#120)
2 parents d090424 + 09a66a5 commit ec02362

File tree

5 files changed

+50
-4
lines changed

5 files changed

+50
-4
lines changed

database/sql/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func OpenContext(ctx context.Context, driverName string, dataSourceName string)
2222
func MustOpenContext(ctx context.Context, driverName string, dataSourceName string) *sql.DB {
2323
db, err := OpenContext(ctx, driverName, dataSourceName)
2424
if err != nil {
25+
err = fmt.Errorf("OpenContext: %w", err)
2526
panic(err)
2627
}
2728

database/sql/tx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package sqlz
33
import (
44
"context"
55
"database/sql"
6+
"fmt"
67
)
78

89
func MustBeginTx(ctx context.Context, db sqlTxBeginner, opts *sql.TxOptions) *sql.Tx {
910
tx, err := db.BeginTx(ctx, opts)
1011
if err != nil {
12+
err = fmt.Errorf("BeginTx: %w", err)
1113
panic(err)
1214
}
1315
return tx

grpc/status/status.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"runtime"
78

89
"google.golang.org/grpc/codes"
910
"google.golang.org/grpc/status"
1011
"google.golang.org/protobuf/runtime/protoiface"
1112

1213
errorz "github.com/kunitsucom/util.go/errors"
14+
filepathz "github.com/kunitsucom/util.go/path/filepath"
1315
)
1416

1517
type Level string
@@ -58,16 +60,30 @@ func WithDetails(details ...protoiface.MessageV1) ErrorOption { //nolint:ireturn
5860
return &newOptionDetails{details: details}
5961
}
6062

63+
// DiscardLogger is a logger to discard logs. If you want to disable logging, assign DiscardLogger to DefaultLogger.
64+
func DiscardLogger(_ context.Context, _ Level, _ *status.Status, _ error) {}
65+
6166
//nolint:gochecknoglobals
6267
var (
63-
DiscardLogger Logger = func(_ context.Context, _ Level, _ *status.Status, _ error) {}
68+
// DefaultLogger is a logger to record the location of statz.New() calls.
6469
DefaultLogger Logger = func(_ context.Context, level Level, stat *status.Status, err error) {
65-
log.Printf("level=%s code=%s message=%q details=%s error=%q stacktrace=%q", level, stat.Code(), stat.Message(), stat.Details(), fmt.Sprintf("%v", err), fmt.Sprintf("%+v", err))
70+
_, file, line, _ := runtime.Caller(2)
71+
log.Printf("level=%s caller=%s:%d code=%s message=%q details=%s error=%q stacktrace=%q", level, filepathz.Short(file), line, stat.Code(), stat.Message(), stat.Details(), fmt.Sprintf("%v", err), fmt.Sprintf("%+v", err))
6672
}
67-
_ interface{ GRPCStatus() *status.Status } = (*statusError)(nil)
68-
errorf = errorz.NewErrorf(errorz.WithCallerSkip(1))
73+
74+
errorf = errorz.NewErrorf(errorz.WithCallerSkip(1))
75+
)
76+
77+
var (
78+
_ error = (*statusError)(nil)
79+
_ fmt.Formatter = (*statusError)(nil)
80+
_ interface{ Unwrap() error } = (*statusError)(nil)
81+
_ interface{ GRPCStatus() *status.Status } = (*statusError)(nil)
6982
)
7083

84+
// New is a function like errors.New() for gRPC status.Status.
85+
//
86+
// The location where statz.New() is called is logged by DefaultLogger.
7187
func New(ctx context.Context, level Level, code codes.Code, msg string, err error, opts ...ErrorOption) error {
7288
e := &statusError{
7389
error: errorf("statz.New: level=%s code=%s message=%s: %w", level, code, msg, err),

path/filepath/short.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package filepathz
2+
3+
import "path/filepath"
4+
5+
func Short(path string) string {
6+
dirname := filepath.Base(filepath.Dir(path))
7+
basename := filepath.Base(path)
8+
9+
return filepath.Join(dirname, basename)
10+
}

path/filepath/short_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package filepathz_test
2+
3+
import (
4+
"testing"
5+
6+
filepathz "github.com/kunitsucom/util.go/path/filepath"
7+
)
8+
9+
func TestShort(t *testing.T) {
10+
t.Parallel()
11+
t.Run("success", func(t *testing.T) {
12+
t.Parallel()
13+
if expect, actual := "to/file", filepathz.Short("/path/to/file"); expect != actual {
14+
t.Errorf("expect(%v) != actual(%v)", expect, actual)
15+
}
16+
})
17+
}

0 commit comments

Comments
 (0)