英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论