zoukankan      html  css  js  c++  java
  • Log4php 使用心得

    使用log4php 记录系统日志:

    1、自动拦截php报出的错误,写日志

    2、手动打印错误

    set_error_handler('captureNormal',E_ERROR | E_PARSE);
    set_exception_handler('captureException');
    register_shutdown_function('captureShutdown');

    自动拦截错误时,其中拦截captureShutDown中的处理不能写日志,进过调试发现log4php中有自己的错误处理函数,在错误处理函数中将写日志功能关闭了。

    /**
     * Default constructor.
     * @param string $name Appender name
     */
    public function __construct($name = '') {
        $this->name = $name;
        
        // Closes the appender on shutdown. Better than a destructor because
        // it will be called even if a fatal error occurs (destructor won't).
        register_shutdown_function(array($this, 'close'));
        
        if ($this->requiresLayout) {
            $this->layout = $this->getDefaultLayout();
        }
    }

    LoggerAppenderFile 继承与 LoggerAppendder

    其中重写了close方法

    public function close() {
        if($this->closed != true) {
            if($this->fp and $this->layout !== null) {
                if(flock($this->fp, LOCK_EX)) {
                    fwrite($this->fp, $this->layout->getFooter());
                    flock($this->fp, LOCK_UN);
                }
                fclose($this->fp);
            }
            $this->closed = true;
        }
    }

    调试中发现,调用close方法没有堆栈信息,猜想多半是使用了 register_shutdown_function

  • 相关阅读:
    Python(二)
    Python(三)
    Python(一)
    shell(计算机壳层)(一)
    web.xml中 /和/*的区别
    dubbo-admin监控搭建2.6.0版本
    Centos7安装maven
    Dubbo启动时qos-server can not bind localhost:22222错误解决
    Centos7安装zookeeper
    mysql5和mysql8连接数据库的配置
  • 原文地址:https://www.cnblogs.com/ccdc/p/2654540.html
Copyright © 2011-2022 走看看