为什么我在本地运行Go应用程序时,在终端中看不到fmt日志?

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

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

问题

我正在尝试调试我的 Golang 应用程序。目前,我有一个 API 请求不起作用,其中有这行代码:
fmt.Errorf("Object(%q).CopierFrom(%q).Run: %v", dstName, object, err)

我该如何查看这个错误日志的输出?如果不可能的话,还有其他一些调试 Golang 的方法吗?(最好是运行时调用)

英文:

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)

答案1

得分: 2

fmt.Errorf创建一个error对象;它不会打印输出。

根据fmt.Errorf的文档:

  1. func Errorf(format string, a ...interface{}) error

如果你只是想将消息打印到标准输出:

  1. fmt.Printf("Object(%q).CopierFrom(%q).Run: %v\n", dstName, object, err)

如果你想写入错误日志,我建议查看log包。例如,如果你想写入标准错误:

  1. logger := log.New(os.Stderr, "my-app", 0)
  2. logger.Printf("Object(%q).CopierFrom(%q).Run: %v", dstName, object, err)
英文:

fmt.Errorf creates an error object; it does not print.

From the docs for fmt.Errorf:

  1. func Errorf(format string, a ...interface{}) error

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

  1. fmt.Printf("Object(%q).CopierFrom(%q).Run: %v\n", dstName, object, err)

If you want to write to an error log, I recommend looking at the log package. For example, if you were looking to write to stderr:

  1. logger := log.New(os.Stderr, "my-app", 0)
  2. logger.Printf("Object(%q).CopierFrom(%q).Run: %v", dstName, object, err)

答案2

得分: 2

fmt.Errorf()创建一个错误对象,但不会打印。如果你只是想将消息打印到标准输出,可以使用以下代码:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. const name, id = "bueller", 17
  7. err := fmt.Errorf("user %q (id %d) not found", name, id)
  8. fmt.Println(err.Error())
  9. }

输出结果为:

  1. user "bueller" (id 17) not found

如果你想调试 Golang 代码,我推荐使用日志包,例如 zerolog

  1. package main
  2. import (
  3. "errors"
  4. "github.com/rs/zerolog"
  5. "github.com/rs/zerolog/log"
  6. )
  7. func main() {
  8. // UNIX 时间比大多数时间戳更快且更小
  9. zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
  10. err := errors.New("seems we have an error here")
  11. log.Error().Err(err).Msg("this is an error")
  12. }

输出结果为:

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

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

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

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. const name, id = "bueller", 17
  7. err := fmt.Errorf("user %q (id %d) not found", name, id)
  8. fmt.Println(err.Error())
  9. }

out:

  1. user "bueller" (id 17) not found

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

  1. package main
  2. import (
  3. "errors"
  4. "github.com/rs/zerolog"
  5. "github.com/rs/zerolog/log"
  6. )
  7. func main() {
  8. // UNIX Time is faster and smaller than most timestamps
  9. zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
  10. err := errors.New("seems we have an error here")
  11. log.Error().Err(err).Msg("this is an error")
  12. }

out:

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

答案3

得分: 1

fmt.Errorf创建一个错误,非常适合用于函数返回,但它不会自动记录日志。

如果你想简单地记录一个错误:

  1. log.Printf("api X: error %v", err)
英文:

fmt.Errorf creates an error - ideal for function returns - but it's not implicitly logged.

If you want to log an error simply:

  1. log.Printf("api X: error %v", err)

答案4

得分: 0

在使用任何函数之前,最好先阅读函数签名和注释。

  1. // Errorf 根据格式说明符进行格式化,并将字符串作为满足 error 接口的值返回。
  2. //
  3. // 如果格式说明符包含带有 error 操作数的 %w 动词,
  4. // 返回的错误将实现一个返回该操作数的 Unwrap 方法。包含多个 %w 动词或者提供不实现 error 接口的操作数都是无效的。否则,%w 动词是 %v 的同义词。
  5. func Errorf(format string, a ...interface{}) error
英文:

It is better to read the function signature and comments before you use any functions.

  1. // Errorf formats according to a format specifier and returns the string as a
  2. // value that satisfies error.
  3. //
  4. // If the format specifier includes a %w verb with an error operand,
  5. // the returned error will implement an Unwrap method returning the operand. It is
  6. // invalid to include more than one %w verb or to supply it with an operand
  7. // that does not implement the error interface. The %w verb is otherwise
  8. // a synonym for %v.
  9. func Errorf(format string, a ...interface{}) error

huangapple
  • 本文由 发表于 2021年12月30日 00:06:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/70521948.html
匿名

发表评论

匿名网友

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

确定