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 设置异常处理函数的更多相关文章

  1. PHP异常处理函数set_exception_handler()的用法

    定义和用法 set_exception_handler() 函数设置用户自定义的异常处理函数. 该函数用于创建运行时期间的用户自己的异常处理方法. 该函数会返回旧的异常处理程序,若失败,则返回 nul ...

  2. set_exception_handler 和 set_error_handler 函数

    定义和用法 set_exception_handler() 函数设置用户自定义的异常处理函数. 该函数用于创建运行时期间的用户自己的异常处理方法. 该函数会返回旧的异常处理程序,若失败,则返回 nul ...

  3. _set_invalid_parameter_handler异常处理函数

    VS2005之后的版本,微软增加了一些新的异常机制,新机制在出现错误时默认不通知应用程序,这时程序就崩溃了.所以这种情况下,必须调用_set_invalid_parameter_handler._se ...

  4. set_exception_handler 自定义异常处理

    该函数用于创建运行时期间的用户自己的异常处理方法. set_exception_handler(error_function) 参数 必需.规定未捕获的异常发生时调用的函数. 该函数必须在调用 set ...

  5. 内存保护机制及绕过方案——通过覆盖SEH异常处理函数绕过/GS机制

    通过SEH链绕过GS保护机制 ⑴.  原理分析: i.异常处理结构(SEH)处理流程如下: SEH是基于线程的,每一个线程都有一个独立的SEH处理结果,在线程信息块中的第一个结构指向线程的异常列表,F ...

  6. CKFinder 弹出窗口操作并设置回调函数

    CKFinder 弹出窗口操作并设置回调函数 官方例子参考CKFinderJava-2.4.1/ckfinder/_samples/popup.html 写一个与EXT集成的小例子 Ext.defin ...

  7. JQuery EasyUI 扩展方法 日期控件 设置时间段函数

    /** Jquery扩展方法--by hgx 2018年1月8日-- * 设置时间段函数,开始时间(1号)与结束时间(当前日期) * 传入参数:--spaceMonth:查询间隔月,1为间隔查询一个月 ...

  8. c++异常处理函数

    注意: throw 抛出异常,catch 捕获异常,try 尝试捕获异常 catch 中的参数类型要和throw 抛出的数据类型一致 try{    //可能抛出异常的语句}catch (异常类型1) ...

  9. python 异常处理函数--raise

    Python 异常处理--raise函数用法 在Python中,要想引发异常,最简单的形式就是输入关键字raise,后跟要引发的异常的名称.异常名称标识出具体的类: Python异常处理是那些类的对象 ...

随机推荐

  1. Cocoa 框架为什么采用两阶段的方式来创建对象?

    对于之前一直使用C#语言的我来说,刚开始接触Objective-c来创建对象时很迷惑,为何创建对象一般情况下需要通过发送两个消息(调用两个方法)才能创建一个类实例对象(例如[[UIButton all ...

  2. npm install --save 与 npm install --save-dev 的区别

    以npm安装msbuild为例: npm install msbuild: 会把msbuild包安装到node_modules目录中 不会修改package.json 之后运行npm install命 ...

  3. iOS进阶——App生命周期

    State Description Not running The app has not been launched or was running but was terminated by the ...

  4. GDI+绘制文本

    这是在论坛中有人提出的一个问题,原贴见:Graphics DrawString参数无效.这里给出方法,读者可以自行修改以适应自己的项目需求. 先上代码: if (!Page.IsPostBack) { ...

  5. 热键HotKeys

    一:新建类HotKeys命名空间: using System.Runtime.InteropServices; 二:注册热键API [DllImport("user32")] pu ...

  6. select绑定json数组对象 asp.net

    ashx处理页 string JsonList = "["; IList<Models.Channel> ilist = BLL.ChannelManager.GetA ...

  7. Android 控件收集

    SwipeMenuExpandableListView   https://github.com/tycallen/SwipeMenu-Expandable-ListView

  8. soinn

    Growing Cell Structures A Self-Organizing Network for Unsupervised and Supervised Learning Here, and ...

  9. js获取url参数值的两种方式

    js获取url参数值的方法有很多,下面也为大家介绍两种.  方法一:正则分析法  function getQueryString(name) {  var reg = new RegExp(" ...

  10. web一次请求的流程

    1.客户端(浏览器输入网址)请求 2.发送http协议到web服务器(nginx),检测请求类别,如果时纯静态页面,则返响应返回给客户端. 3.如果有动态脚本(php语法)启动fastcgi进程,用解 ...