增加异常日志写入的通知驱动

This commit is contained in:
thinkphp
2015-12-20 18:40:51 +08:00
parent e654fcae5c
commit 3b9a7acca5
2 changed files with 59 additions and 2 deletions

View File

@@ -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);
}

View File

@@ -0,0 +1,40 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
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']);
}
}