diff --git a/library/think/log.php b/library/think/log.php index 15577e20..ecf44dde 100644 --- a/library/think/log.php +++ b/library/think/log.php @@ -26,6 +26,8 @@ class Log protected static $type = ['log', 'error', 'info', 'sql', 'warn', 'alert']; // 日志写入驱动 protected static $driver = null; + // 通知发送驱动 + protected static $alarm = null; // 日志初始化 public static function init($config = []) @@ -36,6 +38,15 @@ class Log self::$driver = new $class($config); } + // 通知初始化 + public static function alarm($config = []) + { + $type = isset($config['type']) ? $config['type'] : 'Email'; + $class = '\\think\\log\\alarm\\' . ucwords($config['type']); + unset($config['type']); + self::$alarm = new $class($config['alarm']); + } + /** * 获取全部日志信息 * @return array @@ -66,11 +77,17 @@ class Log } /** - * 写入调试信息 + * 实时写入日志信息 并支持异常和错误预警通知 + * @param string $msg 调试信息 + * @param string $type 信息类型 * @return void */ - public static function write($msg, $type, $destination) + public static function write($msg, $type) { + if ('error' == $type) { + // 预留预警通知接口 + self::$alarm && self::$alarm->send($msg); + } $log[] = ['type' => $type, 'msg' => $msg]; self::$driver && self::$driver->save($log); } diff --git a/library/think/log/alarm/email.php b/library/think/log/alarm/email.php new file mode 100644 index 00000000..0cc58332 --- /dev/null +++ b/library/think/log/alarm/email.php @@ -0,0 +1,40 @@ + +// +---------------------------------------------------------------------- +namespace think\log\alarm; + +/** + * 邮件通知驱动 + */ +class Email +{ + + protected $config = [ + 'address' => '', + ]; + + // 实例化并传入参数 + public function __construct($config = []) + { + $this->config = array_merge($this->config, $config); + } + + /** + * 通知发送接口 + * @access public + * @param string $log 日志信息 + * @return void + */ + public function send($msg = '') + { + error_log($msg, 1, $this->config['address']); + } + +}