gpt4 book ai didi

go - 如何在不退出golang程序的情况下记录错误?

转载 作者:行者123 更新时间:2023-12-01 22:44:32 26 4
gpt4 key购买 nike

我每 5 分钟运行一次任务,我想在不退出程序的情况下记录错误(如果遇到错误),但问题是 log.Fatal() 正在退出程序并且log.Panic() 将调用再次退出程序的 panic()

如何在不退出程序的情况下记录错误?

最佳答案

您可以使用不同的日志记录库,例如 logrus或者像这样修改你的标准日志库:

日志包:

// Info writes logs in the color blue with "INFO: " as prefix
var Info = log.New(os.Stdout, "\u001b[34mINFO: \u001B[0m", log.LstdFlags|log.Lshortfile)

// Warning writes logs in the color yellow with "WARNING: " as prefix
var Warning = log.New(os.Stdout, "\u001b[33mWARNING: \u001B[0m", log.LstdFlags|log.Lshortfile)

// Error writes logs in the color red with "ERROR: " as prefix
var Error = log.New(os.Stdout, "\u001b[31mERROR: \u001b[0m", log.LstdFlags|log.Lshortfile)

// Debug writes logs in the color cyan with "DEBUG: " as prefix
var Debug = log.New(os.Stdout, "\u001b[36mDEBUG: \u001B[0m", log.LstdFlags|log.Lshortfile)

用法:

Error.Println("this is an error")

结果是:

enter image description here

关于go - 如何在不退出golang程序的情况下记录错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60721367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com