From 3b9a7acca5f68c61b8d8d575e7a59e92c841c1d5 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 20 Dec 2015 18:40:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=86=99=E5=85=A5=E7=9A=84=E9=80=9A=E7=9F=A5=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/log.php | 21 ++++++++++++++-- library/think/log/alarm/email.php | 40 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 library/think/log/alarm/email.php 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']); + } + +}