我们可以使用go-fiber框架在Go语言中将日志记录到标准输出(stdout)。

huangapple go评论98阅读模式
英文:

How do we log to stdout with gofiber golang framework?

问题

我正在寻找一种在Go语言的gofiber框架中将一些数据记录到stdout的方法,因为该框架非常安静,大多数情况下不会记录错误。

我曾考虑过使用fmt.Print,但连这个都没有被捕捉到。

你们是如何使用gofiber框架进行故障排除的?当框架很少输出任何内容时,我该如何在代码中检测问题。

以下是我尝试的示例代码,但仍然没有在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 "发现问题", err
	}

	payload := token.Claims.(*ClaimsWithScope)

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

在上述代码中,我使用了fmt.Print("email is", email, "\n")来将email的值打印到stdout,以便进行故障排除,但仍然没有任何输出。

我需要使gofiber输出详细信息以便进行故障排除。

英文:

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

答案1

得分: 0

解析带有声明或自定义声明的JWT时,您应该检查状态。

// 将自定义声明传递给解析函数,cookie参数的数据类型应为字符串
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
})

// 将Claims类型断言为适当类型的变量
payload := token.Claims.(*ClaimsWithScope)

如果您没有看到任何fmt.Print()的输出,则必须使用fmt.Println()

PrintPrint不能格式化任何内容,它只是接受一个字符串并打印它

PrintlnPrint LinePrintf()相同,但它会附加一个换行符\n

英文:

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

答案2

得分: 0

fmt.Println("email is", email, "\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

huangapple
  • 本文由 发表于 2022年1月3日 12:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/70561653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定