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

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

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的文档:

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

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

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

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

logger := log.New(os.Stderr, "my-app", 0)
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:

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

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

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:

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

答案2

得分: 2

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

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())
}

输出结果为:

user "bueller" (id 17) not found

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

package main

import (
	"errors"

	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
)

func main() {
	// UNIX 时间比大多数时间戳更快且更小
	zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

	err := errors.New("seems we have an error here")
	log.Error().Err(err).Msg("this is an error")
}

输出结果为:

{"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

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"}

答案3

得分: 1

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

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

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:

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

答案4

得分: 0

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

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

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

// Errorf formats according to a format specifier and returns the string as a
// value that satisfies error.
//
// If the format specifier includes a %w verb with an error operand,
// the returned error will implement an Unwrap method returning the operand. It is
// invalid to include more than one %w verb or to supply it with an operand
// that does not implement the error interface. The %w verb is otherwise
// a synonym for %v.
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:

确定