PHP set_exception_handler 设置异常处理函数
If you're handling sensitive data and you don't want exceptions logging details such as variable contents when you throw them, you may find yourself frustratedly looking for the bits and pieces that make up a normal stack trace output, so you can retain its legibility but just alter a few things. In that case, this may help you:
<?php
function exceptionHandler($exception) {
// these are our templates
$traceline = "#%s %s(%s): %s(%s)";
$msg = "PHP Fatal error: Uncaught exception '%s' with message '%s' in %s:%s Stack trace: %s thrown in %s on line %s";
// alter your trace as you please, here
$trace = $exception->getTrace();
foreach ($trace as $key => $stackPoint) {
// I'm converting arguments to their type
// (prevents passwords from ever getting logged as anything other than 'string')
$trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);
}
// build your tracelines
$result = array();
foreach ($trace as $key => $stackPoint) {
$result[] = sprintf(
$traceline,
$key,
$stackPoint['file'],
$stackPoint['line'],
$stackPoint['function'],
implode(', ', $stackPoint['args'])
);
}
// trace always ends with {main}
$result[] = '#' . ++$key . ' {main}';
// write tracelines into main template
$msg = sprintf(
$msg,
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
implode(" ", $result),
$exception->getFile(),
$exception->getLine()
);
// log or echo as you please
error_log($msg);
}
?>
If you're not a fan of sprintf() or the duplicate $exception->getFile() and $exception->getLine() calls you can of course replace that as you like - consider this a mere compilation of the parts.
PHP set_exception_handler 设置异常处理函数的更多相关文章
- PHP异常处理函数set_exception_handler()的用法
定义和用法 set_exception_handler() 函数设置用户自定义的异常处理函数. 该函数用于创建运行时期间的用户自己的异常处理方法. 该函数会返回旧的异常处理程序,若失败,则返回 nul ...
- set_exception_handler 和 set_error_handler 函数
定义和用法 set_exception_handler() 函数设置用户自定义的异常处理函数. 该函数用于创建运行时期间的用户自己的异常处理方法. 该函数会返回旧的异常处理程序,若失败,则返回 nul ...
- _set_invalid_parameter_handler异常处理函数
VS2005之后的版本,微软增加了一些新的异常机制,新机制在出现错误时默认不通知应用程序,这时程序就崩溃了.所以这种情况下,必须调用_set_invalid_parameter_handler._se ...
- set_exception_handler 自定义异常处理
该函数用于创建运行时期间的用户自己的异常处理方法. set_exception_handler(error_function) 参数 必需.规定未捕获的异常发生时调用的函数. 该函数必须在调用 set ...
- 内存保护机制及绕过方案——通过覆盖SEH异常处理函数绕过/GS机制
通过SEH链绕过GS保护机制 ⑴. 原理分析: i.异常处理结构(SEH)处理流程如下: SEH是基于线程的,每一个线程都有一个独立的SEH处理结果,在线程信息块中的第一个结构指向线程的异常列表,F ...
- CKFinder 弹出窗口操作并设置回调函数
CKFinder 弹出窗口操作并设置回调函数 官方例子参考CKFinderJava-2.4.1/ckfinder/_samples/popup.html 写一个与EXT集成的小例子 Ext.defin ...
- JQuery EasyUI 扩展方法 日期控件 设置时间段函数
/** Jquery扩展方法--by hgx 2018年1月8日-- * 设置时间段函数,开始时间(1号)与结束时间(当前日期) * 传入参数:--spaceMonth:查询间隔月,1为间隔查询一个月 ...
- c++异常处理函数
注意: throw 抛出异常,catch 捕获异常,try 尝试捕获异常 catch 中的参数类型要和throw 抛出的数据类型一致 try{ //可能抛出异常的语句}catch (异常类型1) ...
- python 异常处理函数--raise
Python 异常处理--raise函数用法 在Python中,要想引发异常,最简单的形式就是输入关键字raise,后跟要引发的异常的名称.异常名称标识出具体的类: Python异常处理是那些类的对象 ...
随机推荐
- Linux文件系统结构
准备写个Linux基础知识总结, 第一个想到的就是整理一个常用系统文件夹结构的说明,园子里“Aric小屋”的结构图整理的不错,我就不重复整理了,故借用一下
- SQL之存储过程,仿数组
create procedure update_ERPTreeList(@s1 varchar(),@s2 varchar()) As Begin declare @ss1 varchar(),@ss ...
- ZipArchive 的使用
新建一个项目,首先添加 System.IO.Compression.FileSystem 引用. 解压文件 using System.IO.Compression; namespace cl { st ...
- docker & nodejs
Docker 部署 Node js demo程序 1.准备node js程序,使用express框架. mkdir demo 在demo文件夹下建立package.json { "name& ...
- css笔记——区分css3中的transform transition animation
出处:http://blog.csdn.net/dyllove98/article/details/8957232 CSS3中和动画有关的属性有三个 transform. transition ...
- java匿名内部类练习
interface Inter { void method(); } class Test { //补足代码.通过匿名内部类. /* static class Inner implements Int ...
- lex&yacc8--wehter use in C++
bintree.h:12:1: error: unknown type name ‘using’ using namespace std; ============== bintree.h:28:1: ...
- Linux 配置
零:个性化 主题:Radiance 主题颜色: gtk-color-scheme = "base_color:#CCE8CF\nfg_color:#006400\ntooltip_fg_co ...
- 用一天的时间学习Java EE中的SSH框架
首先说明一下,本人目前主要从事.NET领域的工作,但对于C++.Java.OC等语言也略知一二,周末闲来无事,特花费一天的时间学习了一下Java中的SSH框架,希望把学习过程中的心得体会与园友们进行分 ...
- linux 常用软件安装-目录
nginx apache php mysql oracle tomcat memcached mongodb sqlserver