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

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

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


智慧大石
浏览 179回答 1
1回答

月关宝盒

您可以使用不同的日志库,如logrus或修改标准日志库,如下所示:日志包:// Info writes logs in the color blue with "INFO: " as prefixvar Info = log.New(os.Stdout, "\u001b[34mINFO: \u001B[0m", log.LstdFlags|log.Lshortfile)// Warning writes logs in the color yellow with "WARNING: " as prefixvar Warning = log.New(os.Stdout, "\u001b[33mWARNING: \u001B[0m", log.LstdFlags|log.Lshortfile)// Error writes logs in the color red with "ERROR: " as prefixvar Error = log.New(os.Stdout, "\u001b[31mERROR: \u001b[0m", log.LstdFlags|log.Lshortfile)// Debug writes logs in the color cyan with "DEBUG: " as prefixvar Debug = log.New(os.Stdout, "\u001b[36mDEBUG: \u001B[0m", log.LstdFlags|log.Lshortfile)用法:Error.Println("this is an error")结果将是:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go