简体   繁体   中英

How do we log to stdout with gofiber golang framework?

I am looking for a way to log some data to stdout with gofiber golang framework as the framework is very quiet and does not log errors mostly

I was thinking i could just use fmf.Print but even that does not get picked up at all

How do you guys troubleshoot with the gofiber framework? How i can detect issues within the code when the framework rarely spits anything out.

Here is what i am trying to do for example but getting nothing still in stdout

func GetUserEmail(c *fiber.Ctx) (string, error) {
    cookie := c.Cookies("access_token")

    token, err := jwt.ParseWithClaims(cookie, &ClaimsWithScope{}, func(token *jwt.Token) (interface{}, error) {
        return []byte(SecretKey), nil
    })

    if err != nil {
        return "issues found", err
    }


    payload := token.Claims.(*ClaimsWithScope)

    email := payload.Subject
    fmt.Print("email is", email, "\n")
    return email, nil
}

from above i have this line fmt.Print("email is", email, "\n") to print the value of email to stdout so i can troubleshoot but nothing gets printed at all

Need to make gofiber verbose to troubleshoot issues

To parse a JWT with claims or custom claims you should check the status.

// pass your custom claims to the parser function and data type of cookie parameter should be string
    token, err := jwt.ParseWithClaims(cookie, &ClaimsWithScope{}, func(token *jwt.Token) (interface{}, error) {
        if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
            return nil, errors.New("unexpected signing method")
        }
        return []byte(/* your JWT secret*/), nil
    })

    // type-assert `Claims` into a variable of the appropriate type
    payload := token.Claims.(*ClaimsWithScope)

If you didn't see any output with fmt.Print() then you have to use fmt.Println() .

Print :- "Print" This cannot format anything, it simply takes a string and print it

Println :- "Print Line" same thing as Printf() however it will append a newline character \n

fmt.Println("email is", email, "\n")

does work, issue was code was not getting to that point; finally moved it higher up in the code and can now see prints in stdout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM