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异常处理是那些类的对象 ...
随机推荐
- python字符串操作2
在python有各种各样的string操作函数.在历史上string类在python中经历了一段轮回的历史.在最开始的时候,python有一个专 门的string的module,要使用string的方 ...
- android apk 逆向中常用工具一览
关于apk 逆向中的一些工具的使用,看了不少文章,也使用过有很长一段时间了,今天对此做一总结: 几种文件之间的转换命令: 1. odex -> smali java -jar baksmali ...
- 快速搭建MongoDB分布式集群
目录Outline 1. prerequisites 2. steps to follow3. configuring the cluster4. a little test to see 1. Pr ...
- 查看Aix系统配置命令
prtconf#topas http://baike.baidu.com/link?url=QruEnlfCqyoqQ565LicyKxIGMQYSkVesj6j9GzHWwzpDOagXtuprhT ...
- HDU1857题解(逆向思维trie)
题目link:http://acm.hdu.edu.cn/showproblem.php?pid=1857 先简述一下题目: 有一个RXC的字母矩形,R,C在500内,要匹配m个单词,m在100000 ...
- opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用
opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用
- Cassandra1.2文档学习(11)—— 删除数据
参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_about_ ...
- CentOS 安装配置memcached (转)
1.先下载memcached 和libevent. libevent 最新的稳定版: wget http://monkey.org/~provos/libevent-1.4.14b-stable.ta ...
- php删除html标签的三种解决方法
分享下PHP删除HTMl标签的三种方法. 方法1:直接取出想要取出的标记 <?php //取出br标记 function strip($str) { $str=str_replace(" ...
- CentOS6.5下 yum安装LAMP
CentOS下yum安装LAMP 1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/ ...