当前位置:Gxlcms > PHP教程 > 如何将php的错误「行数」或完整的错误信息 写进档案

如何将php的错误「行数」或完整的错误信息 写进档案

时间:2021-07-01 10:21:17 帮助过:26人阅读

目前能作到的 只是将sql错误的行数写进 文本文件

我要如何将错误的行数 也写进去。更或将php 错误的讯息整个写进去档案呢

解大大给个方向

回复内容:

目前能作到的 只是将sql错误的行数写进 文本文件

我要如何将错误的行数 也写进去。更或将php 错误的讯息整个写进去档案呢

解大大给个方向

  1. Exception对象承载错误,往外抛
  2. 最外层用try catchset_exception_handler捕获,输出友好错误的同时记录Exception的信息(包含完整的callstack,里面有所有的参数,可能很大需要精简)
  3. error可以通过转换为ErrorException走一样的处理

打比方我厂通过mongodb来记录日志,这是某天的某个错误

    "54d1d4e39dce4f834949f0e9": {
        "_id": {
            "$id": "54d1d4e39dce4f834949f0e9"
        },
        "message": "site.error",
        "context": {
            "uid": null,
            "uri": "\/login\/wechat\/get-userinfo",
            "exception": {
                "class": "ErrorException",
                "message": "Undefined index: openid",
                "file": "\/data\/deploy\/site\/20150130-150624\/src\/Controller\/Passport\/Oauth\/WechatOauth.php:102",
                "trace": [
                    "\/data\/deploy\/site\/20150130-150624\/src\/Controller\/Passport\/Oauth\/WechatOauth.php:102",
                    "{\"function\":\"getUserInfo\",\"class\":\"XXXX\\\\Site\\\\Controller\\\\Passport\\\\Oauth\\\\WechatOauth\",\"type\":\"::\",\"args\":[]}",
                    "\/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Route.php:462",
                    "\/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Slim.php:1326",
                    "\/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Middleware\/Flash.php:85",
                    "\/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Middleware\/MethodOverride.php:92",
                    "\/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Slim.php:1271",
                    "\/data\/deploy\/site\/20150130-150624\/public\/index.php:11"
                ]
            },
            "_trace": [
                "#0 XXXX\\Log\\Log->log() @ \/data\/deploy\/site\/20150130-150624\/vendor\/psr\/log\/Psr\/Log\/AbstractLogger.php:80",
                "#1 Psr\\Log\\AbstractLogger->warning() @ \/data\/deploy\/site\/20150130-150624\/src\/Router.php:194",
                "#2 XXXX\\Site\\Router::XXXX\\Site\\{closure}() @ :",
                "#3 call_user_func_array() @ \/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Slim.php:656",
                "#4 Slim\\Slim->callErrorHandler() @ \/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Slim.php:638",
                "#5 Slim\\Slim->error() @ \/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Slim.php:1347",
                "#6 Slim\\Slim->call() @ \/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Middleware\/Flash.php:85",
                "#7 Slim\\Middleware\\Flash->call() @ \/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Middleware\/MethodOverride.php:92",
                "#8 Slim\\Middleware\\MethodOverride->call() @ \/data\/deploy\/site\/20150130-150624\/vendor\/slim\/slim\/Slim\/Slim.php:1271",
                "#9 Slim\\Slim->run() @ \/data\/deploy\/site\/20150130-150624\/public\/index.php:11",
                "#10 {main}"
            ]
        },
        "level": 300,
        "level_name": "WARNING",
        "channel": "main",
        "datetime": {
            "sec": 1423037667,
            "usec": 0
        },
        "extra": []
    },

微信登陆判断不准确,信任返回结果有openid导致的notice,不看代码都能看出来吧

error_log(),可以吗?

人气教程排行