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