Commit e0ce58c9 authored by fuyunnan's avatar fuyunnan

增加记录日志方法

parent f8374663
......@@ -3,8 +3,12 @@
use Hyperf\Cache\Listener\DeleteListenerEvent;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Logger\Logger;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\FirePHPHandler;
use Monolog\Handler\StreamHandler;
use Psr\EventDispatcher\EventDispatcherInterface;
/**
......@@ -414,6 +418,38 @@ if (!function_exists('get_week_start_and_end')) {
}
}
if (!function_exists('putLog')) {
/**
* description:记录日志 文件会生成在当前项目 /runtime/dev/当前日期名称
* author: fuyunnan
* @param string|array $output 日志内容
* @param string $dir 目录
* date: 2020/3/18
* @throws
* @return void
*/
function putLog ($output='out-mes', $dir =BASE_PATH.'/runtime/dev/')
{
!is_dir($dir) && !mkdir($dir, 0777, true);
// 创建一个 Channel,参数 log 即为 Channel 的名字
$log = new Logger('');
// 创建两个 Handler,对应变量 $stream 和 $fire
$filename = $dir.date('Y-m-d',time()).'.log';
$stream = new StreamHandler($filename, Logger::WARNING);
$fire = new FirePHPHandler();
if (is_array($output)) {
$output = var_export($output, true);
}
$output = '[ ' . date('H:i:s', time()) . ' ] --- ' . $output;
$formatter = new LineFormatter($output."\r\n");
$stream->setFormatter($formatter);
$log->pushHandler($stream);
$log->pushHandler($fire);
$log->alert('');
}
}
if (!function_exists('empty_string_2_null')) {
/**
* 空字符串转NULL
......@@ -437,3 +473,6 @@ if (!function_exists('empty_string_2_null')) {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment