柳暗花明笔记

    php catch error use register_shutdown_function and log it class

    所属分类:[ php&web ] 猫哲学家 发表于:2015-07-22 17:03 浏览:590

    <?php
    /*
    * @anthor Lanmiz Li 20150722 version 1.0
    */

    class errorLog
    {
    public static $cfg;

    public function __construct ($cfg = array())
    {
    self::$cfg = $cfg;
    }

    public static function log($mess, $log_dir = null)
    {
    if (empty(self::$cfg['log_dir']) and empty($log_dir))
    {
    exit('Log dir is Must.');
    }

    $log_dir = empty($log_dir) ? self::$cfg['log_dir'] : $log_dir;

    if (!is_writable($log_dir))
    {
    exit("Log dir: '{$log_dir}' is not writable.");
    }

    $log_file = $log_dir . date('Ym') . '.log';

    error_log(date('[Y-m-d H:i:s] ') . print_r($mess, 1) . PHP_EOL, 3, $log_file);
    }

    public static function my_shutdown_function()
    {
    if ($error = error_get_last())
    {
    switch ($error['type'])
    {
    case 1: $error_type = 'E_ERROR'; break;
    case 2: $error_type = 'E_WARNING'; break;
    case 4: $error_type = 'E_PARSE'; break;
    case 16: $error_type = 'E_CORE_ERROR'; break;
    case 32: $error_type = 'E_CORE_WARNING'; break;
    case 64: $error_type = 'E_COMPILE_ERROR'; break;
    case 128: $error_type = 'E_COMPILE_WARNING'; break;
    case 256: $error_type = 'E_USER_ERROR'; break;
    case 512: $error_type = 'E_USER_WARNING'; break;
    case 4096: $error_type = 'E_RECOVERABLE_ERROR'; break;
    default: $error_type = 'error 0'; break;
    }

    self::log("{$error_type}|{$error['message']} in {$error['file']} on line {$error['line']}");
    }
    }
    }

    // $error_log = new errorLog($cfg = array('log_dir' => './log'));

    // register_shutdown_function(array($error_log, 'my_shutdown_function'));

    标签: php 
    上一编文章 DB class v2.0 use mysqli 下一编文章 mysql常格式化时间函数
    最近文章
    发表评论