5

I am trying to debug code by printing/logging from within a gRPC/Go function but the output does not print to the terminal.

fmt.Println() works in my main() method, but not in the server callbacks. How can I print from within these gRPC handlers? I'm open to other methods for debugging if there's a different approach too.

Here's the related code from my main() function. Logging and printing works here:

func main() {
    ...

    lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%s", os.Getenv("APP_PORT")))
    if err != nil {
        logger.Error(fmt.Sprintf("Failed start on port: %s", os.Getenv("APP_PORT")), err)
        log.Fatalf("Failed to start: %v", err)
    }

    opts := []grpc.ServerOption{}
    s := grpc.NewServer(opts...)
    pb.RegisterAuthServiceServer(s, &server{})

    // Register reflection service on gRPC server.
    reflection.Register(s)

    go func() {
        fmt.Println("Starting Server...")
        if err := s.Serve(lis); err != nil {
            log.Fatalf("failed to server: %v", err)
        }
    }()

    // Wait for Control C to exit
    ch := make(chan os.Signal, 1)
    signal.Notify(ch, os.Interrupt)

    // Run until a signal is received
    <-ch
    fmt.Println("")
    fmt.Println("Stopping the server")
    s.Stop()
    fmt.Println("Closing the listener")
    lis.Close()

Then here is one of my handlers, where logging or printing does not output to the terminal where I started the server.

func (*server) AuthUser(ctx context.Context, req *pb.AuthUserRequest) (*pb.AccessToken, error) {
    fmt.Println("AuthUser***")
    ...
Coder1
  • 13,139
  • 15
  • 59
  • 89

0 Answers0