Search code examples
gofmt

Why don't I see fmt logs in my terminal when running go app locally?


I'm trying to debug my golang app. Currently, I have an API request that isn't working which has this line of code: fmt.Errorf("Object(%q).CopierFrom(%q).Run: %v", dstName, object, err)

How can I view the output of this error log? And if it's not possible what are some other ways to debug in go? (runtime invocation would be nice)


Solution

  • fmt.Errorf() creates an error object. but not print.doc

    If you're just trying to print the message to stdout: run

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        const name, id = "bueller", 17
        err := fmt.Errorf("user %q (id %d) not found", name, id)
        fmt.Println(err.Error())
    
    }
    

    out:

    user "bueller" (id 17) not found
    

    if you want debug golang code, I recommend use log packages for example: zerolog

    
    package main
    
    import (
        "errors"
    
        "github.com/rs/zerolog"
        "github.com/rs/zerolog/log"
    )
    
    func main() {
        // UNIX Time is faster and smaller than most timestamps
        zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
    
        err := errors.New("seems we have an error here")
        log.Error().Err(err).Msg("this is an error")
    }
    
    

    out:

    {"level":"error","error":"seems we have an error here","time":1640795128,"message":"this is an error"}