How to use WithLogger method of pb Package

Best K6 code snippet using pb.WithLogger

grpc_client_test.go

Source:grpc_client_test.go Github

copy

Full Screen

...19 memConfig.Set("debug", true)20 memConfig.Set("grpc_client_debug", true)21 clientOptional := ClientOptionals{}22 clientOptions := NewClientOptions(23 clientOptional.WithLogger(logger),24 clientOptional.WithConf(memConfig),25 )26 ctx := context.Background()27 client := NewClient(clientOptions)28 conn := client.DialContext(ctx, tcpAddr.String())29 defer conn.Close()30 c := pb.NewGreeterClient(conn)31 ctx, cancel := context.WithTimeout(context.Background(), time.Second)32 defer cancel()33 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: esim})34 if err != nil {35 logger.Errorf(err.Error())36 } else {37 assert.NotEmpty(t, r.Message)38 }39}40func TestSlowClient(t *testing.T) {41 memConfig := config.NewMemConfig()42 memConfig.Set("debug", true)43 memConfig.Set("grpc_client_debug", true)44 memConfig.Set("grpc_client_check_slow", true)45 memConfig.Set("grpc_client_slow_time", 10)46 clientOptional := ClientOptionals{}47 clientOptions := NewClientOptions(48 clientOptional.WithLogger(logger),49 clientOptional.WithConf(memConfig),50 clientOptional.WithDialOptions(51 grpc.WithBlock(),52 grpc.WithChainUnaryInterceptor(slowRequest),53 ),54 )55 ctx := context.Background()56 client := NewClient(clientOptions)57 conn := client.DialContext(ctx, tcpAddr.String())58 defer conn.Close()59 c := pb.NewGreeterClient(conn)60 ctx, cancel := context.WithTimeout(context.Background(), time.Second)61 defer cancel()62 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: esim})63 if err != nil {64 logger.Errorf(err.Error())65 } else {66 assert.NotEmpty(t, r.Message)67 }68}69func TestServerPanic(t *testing.T) {70 memConfig := config.NewMemConfig()71 memConfig.Set("debug", true)72 memConfig.Set("grpc_client_debug", true)73 clientOptional := ClientOptionals{}74 clientOptions := NewClientOptions(75 clientOptional.WithLogger(logger),76 clientOptional.WithConf(memConfig),77 )78 ctx := context.Background()79 client := NewClient(clientOptions)80 conn := client.DialContext(ctx, tcpAddr.String())81 defer conn.Close()82 c := pb.NewGreeterClient(conn)83 ctx, cancel := context.WithTimeout(context.Background(), time.Second)84 defer cancel()85 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: callPanic})86 assert.Error(t, err)87 assert.Nil(t, r)88}89func TestServerPanicArr(t *testing.T) {90 memConfig := config.NewMemConfig()91 memConfig.Set("debug", true)92 memConfig.Set("grpc_client_debug", true)93 clientOptional := ClientOptionals{}94 clientOptions := NewClientOptions(95 clientOptional.WithLogger(logger),96 clientOptional.WithConf(memConfig),97 )98 ctx := context.Background()99 client := NewClient(clientOptions)100 conn := client.DialContext(ctx, tcpAddr.String())101 defer conn.Close()102 c := pb.NewGreeterClient(conn)103 ctx, cancel := context.WithTimeout(context.Background(), time.Second)104 defer cancel()105 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: callPanicArr})106 assert.Error(t, err)107 assert.Nil(t, r)108}109func TestSubsReply(t *testing.T) {110 memConfig := config.NewMemConfig()111 memConfig.Set("debug", true)112 memConfig.Set("grpc_client_debug", true)113 clientOptional := ClientOptionals{}114 clientOptions := NewClientOptions(115 clientOptional.WithLogger(logger),116 clientOptional.WithConf(memConfig),117 clientOptional.WithDialOptions(118 grpc.WithChainUnaryInterceptor(ClientStubs(func(ctx context.Context,119 method string, req, reply interface{}, cc *grpc.ClientConn,120 invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {121 if method == "/helloworld.Greeter/SayHello" {122 reply.(*pb.HelloReply).Message = esim123 }124 return nil125 })),126 ),127 )128 ctx := context.Background()129 client := NewClient(clientOptions)130 conn := client.DialContext(ctx, tcpAddr.String())131 defer conn.Close()132 c := pb.NewGreeterClient(conn)133 ctx, cancel := context.WithTimeout(context.Background(), time.Second)134 defer cancel()135 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: esim})136 if err != nil {137 logger.Errorf(err.Error())138 } else {139 assert.Equal(t, esim, r.Message)140 }141}142func TestGlobalSubs(t *testing.T) {143 memConfig := config.NewMemConfig()144 memConfig.Set("debug", true)145 memConfig.Set("grpc_client_debug", true)146 clientOptional := ClientOptionals{}147 GlobalStub = func(ctx context.Context,148 method string, req, reply interface{}, cc *grpc.ClientConn,149 invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {150 if method == "/helloworld.Greeter/SayHello" {151 reply.(*pb.HelloReply).Message = esim152 }153 return nil154 }155 ctx := context.Background()156 client := NewClientWithOptionals(157 clientOptional.WithLogger(logger),158 clientOptional.WithConf(memConfig),159 )160 conn := client.DialContext(ctx, tcpAddr.String())161 defer conn.Close()162 c := pb.NewGreeterClient(conn)163 ctx, cancel := context.WithTimeout(context.Background(), time.Second)164 defer cancel()165 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: esim})166 if err != nil {167 logger.Errorf(err.Error())168 } else {169 assert.Equal(t, esim, r.Message)170 }171}...

Full Screen

Full Screen

server_test.go

Source:server_test.go Github

copy

Full Screen

...16 "google.golang.org/protobuf/encoding/protojson"17 "google.golang.org/protobuf/proto"18)19func TestHTTP(t *testing.T) {20 withLogger := serve.WithLogger(log.DiscardLogger)21 ts := serve.NewTestServer(serve.JsonnetEvaluator(), os.DirFS("testdata/greet"), withLogger)22 defer ts.Stop()23 h := NewServer(ts.Files, ts.UnknownHandler, log.DiscardLogger, nil)24 ts.SetHTTPHandler(h)25 body := `{"first_name": "Stranger"}`26 url := fmt.Sprintf("http://%s/api/greet/hello", ts.Addr())27 t.Run("accept JSON response", func(t *testing.T) {28 resp, err := http.Post(url, "application/json; charset=utf-8", strings.NewReader(body))29 require.NoError(t, err)30 respPb := &greet.HelloResponse{}31 raw, err := ioutil.ReadAll(resp.Body)32 require.NoError(t, err)33 defer resp.Body.Close()34 require.NoError(t, protojson.Unmarshal(raw, respPb))35 expected := &greet.HelloResponse{Greeting: "💃 jig [unary]: Hello Stranger"}36 require.Truef(t, proto.Equal(expected, respPb), "expected: %s, \nactual: %s", expected, respPb)37 })38 t.Run("accept binary response", func(t *testing.T) {39 req, err := http.NewRequest("POST", url, strings.NewReader(body))40 require.NoError(t, err)41 req.Header.Set("Content-Type", "application/json; charset=utf-8")42 req.Header.Set("Accept", "application/x-protobuf; charset=utf-8")43 resp, err := http.DefaultClient.Do(req)44 require.NoError(t, err)45 respPb := &greet.HelloResponse{}46 raw, err := ioutil.ReadAll(resp.Body)47 require.NoError(t, err)48 defer resp.Body.Close()49 require.NoError(t, proto.Unmarshal(raw, respPb))50 expected := &greet.HelloResponse{Greeting: "💃 jig [unary]: Hello Stranger"}51 require.Truef(t, proto.Equal(expected, respPb), "expected: %s, \nactual: %s", expected, respPb)52 })53 t.Run("converts error responses to HTTP", func(t *testing.T) {54 badRequestBody := `{"first_name": "Bart"}`55 req, err := http.NewRequest("POST", url, strings.NewReader(badRequestBody))56 require.NoError(t, err)57 req.Header.Set("Accept", "application/json; charset=utf-8")58 resp, err := http.DefaultClient.Do(req)59 require.NoError(t, err)60 require.Equal(t, http.StatusBadRequest, resp.StatusCode)61 respPb := &statuspb.Status{}62 raw, err := ioutil.ReadAll(resp.Body)63 require.NoError(t, err)64 defer resp.Body.Close()65 require.NoError(t, protojson.Unmarshal(raw, respPb))66 respPb.Details = nil67 expected := &statuspb.Status{Code: 3, Message: "💃 jig [unary]: eat my shorts"}68 require.Truef(t, proto.Equal(expected, respPb), "expected: %s, \nactual: %s", expected, respPb)69 })70 t.Run("return 404 for invalid path", func(t *testing.T) {71 req, err := http.NewRequest("GET", url, nil)72 require.NoError(t, err)73 req.Header.Set("Accept", "application/json; charset=utf-8")74 resp, err := http.DefaultClient.Do(req)75 require.NoError(t, err)76 require.Equal(t, http.StatusNotFound, resp.StatusCode)77 })78}79func TestHTTPRuleInterpolation(t *testing.T) {80 logger := log.NewLogger(io.Discard, log.LogLevelError)81 withLogger := serve.WithLogger(logger)82 ts := serve.NewTestServer(serve.JsonnetEvaluator(), os.DirFS("testdata/httpgreet"), withLogger)83 defer ts.Stop()84 tmpl := []*annotations.HttpRule{85 {Pattern: &annotations.HttpRule_Post{Post: "/post/{package}.{service}/{method}"}, Body: "*"},86 {Pattern: &annotations.HttpRule_Get{Get: "/get/{method}"}},87 }88 h := NewServer(ts.Files, ts.UnknownHandler, logger, tmpl)89 ts.SetHTTPHandler(h)90 u := "http://" + ts.Addr() + "/get/SimpleHello"91 req, err := http.NewRequest("GET", u, nil)92 require.NoError(t, err)93 req.Header.Set("Accept", "application/json; charset=utf-8")94 resp, err := http.DefaultClient.Do(req)95 require.NoError(t, err)...

Full Screen

Full Screen

main_test.go

Source:main_test.go Github

copy

Full Screen

...46 dt.RunReids()47 dt.RunMysql()48 clientOptions := mysql.ClientOptions{}49 mysqlClient = mysql.NewClient(50 clientOptions.WithLogger(logger),51 clientOptions.WithConf(conf),52 clientOptions.WithGormConfig(&gorm.Config{53 Logger: glog,54 }),55 clientOptions.WithDbConfig(56 []mysql.DbConfig{57 mysql.DbConfig{58 Db: "sagas",59 Dsn: "root:123456@tcp(localhost:3306)/sagas?charset=utf8&parseTime=True&loc=Local",60 },61 mysql.DbConfig{62 Db: "sagas_slave",63 Dsn: "root:123456@tcp(localhost:3306)/sagas?charset=utf8&parseTime=True&loc=Local",64 },65 },66 ),67 clientOptions.WithProxy(68 func() interface{} {69 monitorProxyOptions := mysql.MonitorProxyOptions{}70 return mysql.NewMonitorProxy(71 monitorProxyOptions.WithLogger(logger),72 monitorProxyOptions.WithConf(conf),73 )74 },75 ),76 )77 redisClientOptions := redis.ClientOptions{}78 redisClient = redis.NewClient(79 redisClientOptions.WithLogger(logger),80 redisClientOptions.WithConf(conf),81 redisClientOptions.WithProxy(func() interface{} {82 monitorProxyOptions := redis.MonitorProxyOptions{}83 return redis.NewMonitorProxy(84 monitorProxyOptions.WithLogger(logger),85 monitorProxyOptions.WithConf(conf),86 )87 }),88 )89 nxRedis := nx_redis.NewClient(90 nx_redis.WithLogger(logger),91 nx_redis.WithClient(redisClient),92 )93 nl = nxlock.NewNxlock(94 nxlock.WithLogger(logger),95 nxlock.WithSolution(nxRedis),96 )97 httpClientOptions := ehttp.ClientOptions{}98 httpClient = ehttp.NewClient(99 httpClientOptions.WithLogger(logger),100 httpClientOptions.WithProxy(101 func() interface{} {102 monitorProxyOptions := ehttp.MonitorProxyOptions{}103 return ehttp.NewMonitorProxy(104 monitorProxyOptions.WithLogger(logger),105 monitorProxyOptions.WithConf(conf),106 )107 },108 ),109 )110 code := m.Run()111 mysqlClient.Close()112 redisClient.Close()113 dt.Close()114 //mysqlClient.Close()115 // You can't defer this because os.Exit doesn't care for defer116 os.Exit(code)117}118func StartHTTPServer() {...

Full Screen

Full Screen

WithLogger

Using AI Code Generation

copy

Full Screen

1func main() {2 logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)3 client := pb.NewGreeterClient("localhost:50051", grpc.WithLogger(logger))4}5func main() {6 logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)7 client := pb.NewGreeterClient("localhost:50051", grpc.WithLoggerV2(logger))8}9func main() {10 logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)11 client := pb.NewGreeterClient("localhost:50051", grpc.WithLoggerV2(logger, grpclogger.WithLevels(grpclog.Info, grpclog.Info, grpclog.Info)))12}13The grpc.WithLoggerV2() method has an optional second parameter, which is a grpclog.LoggerV2Option interface. This option allows you to specify custom log levels for the different log messages. The default log levels are:

Full Screen

Full Screen

WithLogger

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(time.Now())4}5import (6type Person struct {7}8func main() {9 p := Person{}10 fmt.Println(p)11}12{}13{ Name: Age: 0 }14import (15func main() {16 data, err := ioutil.ReadFile("test.txt")17 if err != nil {18 fmt.Println("File reading error", err)19 }20 fmt.Println("Contents of file:", string(data))21}22import (23func main() {24 fmt.Println(time.Now())25}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful