2019-11-11 12:57:06 +00:00
|
|
|
<?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\driver;
|
|
|
|
|
|
|
|
|
|
use think\App;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 本地化调试输出到文件
|
|
|
|
|
*/
|
|
|
|
|
class File
|
|
|
|
|
{
|
|
|
|
|
protected $config = [
|
2019-11-13 03:54:57 +00:00
|
|
|
'time_format' => ' c ',
|
2019-11-11 12:57:06 +00:00
|
|
|
'single' => false,
|
|
|
|
|
'file_size' => 2097152,
|
|
|
|
|
'path' => '',
|
|
|
|
|
'apart_level' => [],
|
|
|
|
|
'max_files' => 0,
|
|
|
|
|
'json' => false,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $app;
|
|
|
|
|
|
|
|
|
|
// 实例化并传入参数
|
|
|
|
|
public function __construct(App $app, $config = [])
|
|
|
|
|
{
|
|
|
|
|
$this->app = $app;
|
|
|
|
|
|
|
|
|
|
if (is_array($config)) {
|
|
|
|
|
$this->config = array_merge($this->config, $config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($this->config['path'])) {
|
|
|
|
|
$this->config['path'] = $this->app->getRuntimePath() . 'log' . DIRECTORY_SEPARATOR;
|
|
|
|
|
} elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) {
|
|
|
|
|
$this->config['path'] .= DIRECTORY_SEPARATOR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 日志写入接口
|
|
|
|
|
* @access public
|
|
|
|
|
* @param array $log 日志信息
|
|
|
|
|
* @param bool $append 是否追加请求信息
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function save(array $log = [], $append = false)
|
|
|
|
|
{
|
|
|
|
|
$destination = $this->getMasterLogFile();
|
|
|
|
|
|
|
|
|
|
$path = dirname($destination);
|
|
|
|
|
!is_dir($path) && mkdir($path, 0755, true);
|
|
|
|
|
|
|
|
|
|
$info = [];
|
|
|
|
|
|
|
|
|
|
foreach ($log as $type => $val) {
|
|
|
|
|
|
|
|
|
|
foreach ($val as $msg) {
|
|
|
|
|
if (!is_string($msg)) {
|
|
|
|
|
$msg = var_export($msg, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$info[$type][] = $this->config['json'] ? $msg : '[ ' . $type . ' ] ' . $msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->config['json'] && (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level']))) {
|
|
|
|
|
// 独立记录的日志级别
|
|
|
|
|
$filename = $this->getApartLevelFile($path, $type);
|
|
|
|
|
|
|
|
|
|
$this->write($info[$type], $filename, true, $append);
|
|
|
|
|
|
|
|
|
|
unset($info[$type]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($info) {
|
|
|
|
|
return $this->write($info, $destination, false, $append);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 日志写入
|
|
|
|
|
* @access protected
|
|
|
|
|
* @param array $message 日志信息
|
|
|
|
|
* @param string $destination 日志文件
|
|
|
|
|
* @param bool $apart 是否独立文件写入
|
|
|
|
|
* @param bool $append 是否追加请求信息
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function write($message, $destination, $apart = false, $append = false)
|
|
|
|
|
{
|
|
|
|
|
// 检测日志文件大小,超过配置大小则备份日志文件重新生成
|
|
|
|
|
$this->checkLogSize($destination);
|
|
|
|
|
|
|
|
|
|
// 日志信息封装
|
|
|
|
|
$info['timestamp'] = date($this->config['time_format']);
|
|
|
|
|
|
|
|
|
|
foreach ($message as $type => $msg) {
|
2019-11-13 03:54:57 +00:00
|
|
|
$info[$type] = is_array($msg) ? implode("\r\n", $msg) : $msg;
|
2019-11-11 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PHP_SAPI == 'cli') {
|
|
|
|
|
$message = $this->parseCliLog($info);
|
|
|
|
|
} else {
|
|
|
|
|
// 添加调试日志
|
|
|
|
|
$this->getDebugLog($info, $append, $apart);
|
|
|
|
|
|
|
|
|
|
$message = $this->parseLog |