保持相同的格式并记录 PHP 中的所有错误
我有多个类,我想做的是对错误有一致的风格。例如 mysql 错误、模板类错误等等。
除了 die(""); 之外,还有什么更好的退出错误的方法呢?
我是否应该定义自己的函数,然后将 die 放入其中,并使用该函数以及 die(); 的样式消息;
另外,如果我想记录错误,例如
Error | line number | script name | if a class then where was it called that caused error | memory usage | cpu usage
谢谢......
I have multiple classes and what I wanna do is that have a consitent style for errors. for example for mysql errors, template class errors, and stuff.
Also what could be a better way of exiting with an error other that die("");
Should I define my own function and then put die in there and the style message with that function as well as die();
Also, if I want to log errors such as
Error | line number | script name | if a class then where was it called that caused error | memory usage | cpu usage
thanks....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
set_error_handler()
来满足所有特定的错误格式化需求。您需要定义一个自定义函数,以便以您想要的格式漂亮地打印:对于您想要的额外信息,您必须使用
debug_backtrace()
了解详细信息,或memory_get_usage
。如果您想将内容写入文件,只需使用
file_put_contents("log.txt", $string, FILE_APPEND|LOCK_EX);
而不是直接print
。You can use
set_error_handler()
for all your specific error formatting needs. You will need to define a custom function to pretty print in your desired format:For that extra information that you want, you will have to use
debug_backtrace()
to find out details, ormemory_get_usage
.If you want to write the stuff to a file, then just use
file_put_contents("log.txt", $string, FILE_APPEND|LOCK_EX);
instead of the directprint
.