Skip to content

Commit 71f5ef2

Browse files
author
Abhishek Srivastava
authored
Merge pull request #10 from tokopedia/gqlserver_graphqlgo_config_interface
Config propogation from gqlserver to graphql-go
2 parents 5719637 + 9ca2ea2 commit 71f5ef2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

graphql.go

+14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type Schema struct {
6868
logger log.Logger
6969
useStringDescriptions bool
7070
disableIntrospection bool
71+
isPanicLogEnabled bool
7172
}
7273

7374
// SchemaOpt is an option to pass to ParseSchema or MustParseSchema.
@@ -150,6 +151,19 @@ func (s *Schema) Validate(queryString string, variables map[string]interface{})
150151
return false, validation.Validate(s.schema, doc, variables, s.maxDepth)
151152
}
152153

154+
//EnablePanicLogging enables query info logging if panic occurs
155+
func (s *Schema) EnablePanicLogging() {
156+
s.isPanicLogEnabled = true
157+
s.logger = getLogger(s)
158+
}
159+
160+
func getLogger(s *Schema) log.Logger {
161+
if s != nil && s.isPanicLogEnabled {
162+
return &log.CustomLogger{true}
163+
}
164+
return &log.DefaultLogger{}
165+
}
166+
153167
// Exec executes the given query with the schema's resolver. It panics if the schema was created
154168
// without a resolver. If the context get cancelled, no further resolvers will be called and a
155169
// the context error will be returned as soon as possible (not immediately).

log/log.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package log
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"runtime"
78
)
@@ -19,5 +20,24 @@ func (l *DefaultLogger) LogPanic(_ context.Context, value interface{}, info stri
1920
const size = 64 << 10
2021
buf := make([]byte, size)
2122
buf = buf[:runtime.Stack(buf, false)]
22-
log.Printf("graphql: panic occurred: %v\n%s\n\n%s", value, buf, info)
23+
log.Printf("graphql: panic occurred: %v\n%s", value, buf)
2324
}
25+
26+
// CustomLogger is the custom logger for use with gqlserver custome config to log panics that occur during query execution
27+
type CustomLogger struct{
28+
VerbosePanicLog bool
29+
}
30+
31+
// LogPanic is used to log recovered panic values that occur during query execution
32+
func (l *CustomLogger) LogPanic(_ context.Context, value interface{}, info string) {
33+
const size = 64 << 10
34+
buf := make([]byte, size)
35+
buf = buf[:runtime.Stack(buf, false)]
36+
msg := fmt.Sprintf("graphql: panic occurred: %v\n%s", value, buf)
37+
if l.VerbosePanicLog {
38+
msg = fmt.Sprintf("%s\n\n%s", msg, info)
39+
}
40+
41+
log.Printf(msg)
42+
}
43+

0 commit comments

Comments
 (0)