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 f...

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.

如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
目录
相关文章
|
2月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
232 1
|
3月前
|
PHP 开发者
PHP计算过去一定时间段内日期范围函数
这个函数为开发者提供了一个快速而简单的方法来获取与当前日期相关的过去时间范围,代码简洁易懂,可复用性高。
145 15
|
2月前
|
存储 监控 算法
基于 PHP 布隆过滤器的局域网监控管理工具异常行为检测算法研究
布隆过滤器以其高效的空间利用率和毫秒级查询性能,为局域网监控管理工具提供轻量化异常设备检测方案。相比传统数据库,显著降低延迟与资源消耗,适配边缘设备部署需求,提升网络安全实时防护能力。(238字)
170 0
|
5月前
|
PHP 开发者 索引
探究PHP中常见数组操作函数
在编码实践中,合理利用这些数组操作函数可以简化编程工作,提升代码的效率和可读性。为达到最佳实践,开发者应该通过阅读官方文档来深入理解每个函数的工作原理以及如何在不同的场景下运用它们。
223 8
|
9月前
|
数据库连接 PHP 数据库
【YashanDB知识库】PHP使用ODBC使用数据库绑定参数功能异常
【YashanDB知识库】PHP使用ODBC使用数据库绑定参数功能异常
|
9月前
|
PHP 数据库
【YashanDB知识库】PHP使用OCI接口使用数据库绑定参数功能异常
【YashanDB知识库】PHP使用OCI接口使用数据库绑定参数功能异常
|
关系型数据库 MySQL 数据库连接
PHP内置函数
PHP内置函数
151 5
|
Java Python
gc模块的set_threshold函数
gc模块的set_threshold函数
375 1
|
PHP 数据库 开发者
PHP中的异常处理和自定义异常
【10月更文挑战第3天】在PHP编程中,异常处理是一个重要的话题。它允许开发者优雅地处理错误,提高代码的可读性和可维护性。本文将介绍如何在PHP中进行异常处理,包括基本的try-catch结构,以及如何创建和使用自定义异常类来处理特定的错误情况。通过实际示例,我们将看到如何使用异常处理机制来增强应用程序的健壮性和灵活性。
144 1
|
Unix PHP 数据库
PHP日期和时间Date()函数获取当前时间
通过灵活运用 `date()`函数及其丰富的格式选项,PHP开发者可以轻松地在应用程序中处理和展示日期及时间信息。无论是需要精确到秒的完整时间戳,还是仅仅展示日期或时间的某一部分,`date()`函数都能胜任。理解并熟练应用这些格式化技巧,对于提升代码的可读性和维护性至关重要。
276 1