@@ -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
1517type 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
6267var (
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.
7187func 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 ),
0 commit comments