mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
增加异常日志写入的通知驱动
This commit is contained in:
@@ -26,6 +26,8 @@ class Log
|
|||||||
protected static $type = ['log', 'error', 'info', 'sql', 'warn', 'alert'];
|
protected static $type = ['log', 'error', 'info', 'sql', 'warn', 'alert'];
|
||||||
// 日志写入驱动
|
// 日志写入驱动
|
||||||
protected static $driver = null;
|
protected static $driver = null;
|
||||||
|
// 通知发送驱动
|
||||||
|
protected static $alarm = null;
|
||||||
|
|
||||||
// 日志初始化
|
// 日志初始化
|
||||||
public static function init($config = [])
|
public static function init($config = [])
|
||||||
@@ -36,6 +38,15 @@ class Log
|
|||||||
self::$driver = new $class($config);
|
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
|
* @return array
|
||||||
@@ -66,11 +77,17 @@ class Log
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 写入调试信息
|
* 实时写入日志信息 并支持异常和错误预警通知
|
||||||
|
* @param string $msg 调试信息
|
||||||
|
* @param string $type 信息类型
|
||||||
* @return void
|
* @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];
|
$log[] = ['type' => $type, 'msg' => $msg];
|
||||||
self::$driver && self::$driver->save($log);
|
self::$driver && self::$driver->save($log);
|
||||||
}
|
}
|
||||||
|
|||||||
40
library/think/log/alarm/email.php
Normal file
40
library/think/log/alarm/email.php
Normal 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']);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user