~~~
/**
* 日志管理類
* @package think
* @mixin Channel
*/
class Log extends Manager implements LoggerInterface
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
const SQL = 'sql';
protected $namespace = '\\think\\log\\driver\\';
/**
* 默認驅動
* @return string|null
*/
public function getDefaultDriver()
{
return $this->getConfig('default');
}
/**
* 獲取日志配置
* @access public
* @param null|string $name 名稱
* @param mixed $default 默認值
* @return mixed
*/
public function getConfig(string $name = null, $default = null)
{
if (!is_null($name)) {
return $this->app->config->get('log.' . $name, $default);
}
return $this->app->config->get('log');
}
/**
* 獲取渠道配置
* @param string $channel
* @param null $name
* @param null $default
* @return array
*/
public function getChannelConfig($channel, $name = null, $default = null)
{
if ($config = $this->getConfig("channels.{$channel}")) {
return Arr::get($config, $name, $default);
}
throw new InvalidArgumentException("Channel [$channel] not found.");
}
/**
* driver()的別名
* @param string|array $name 渠道名
* @return Channel|ChannelSet
*/
public function channel($name = null)
{
if (is_array($name)) {
return new ChannelSet($this, $name);
}
return $this->driver($name);
}
protected function resolveType(string $name)
{
return $this->getChannelConfig($name, 'type', 'file');
}
public function createDriver(string $name)
{
$driver = parent::createDriver($name);
$lazy = !$this->getChannelConfig($name, "realtime_write", false) && !$this->app->runningInConsole();
$allow = array_merge($this->getConfig("level", []), $this->getChannelConfig($name, "level", []));
return new Channel($name, $driver, $allow, $lazy, $this->app->event);
}
protected function resolveConfig(string $name)
{
return $this->getChannelConfig($name);
}
/**
* 清空日志信息
* @access public
* @param string|array $channel 日志通道名
* @return $this
*/
public function clear($channel = '*')
{
if ('*' == $channel) {
$channel = array_keys($this->drivers);
}
$this->channel($channel)->clear();
return $this;
}
/**
* 關閉本次請求日志寫入
* @access public
* @param string|array $channel 日志通道名
* @return $this
*/
public function close($channel = '*')
{
if ('*' == $channel) {
$channel = array_keys($this->drivers);
}
$this->channel($channel)->close();
return $this;
}
/**
* 獲取日志信息
* @access public
* @param string $channel 日志通道名
* @return array
*/
public function getLog(string $channel = null): array
{
return $this->channel($channel)->getLog();
}
/**
* 保存日志信息
* @access public
* @return bool
*/
public function save(): bool
{
/** @var Channel $channel */
foreach ($this->drivers as $channel) {
$channel->save();
}
return true;
}
/**
* 記錄日志信息
* @access public
* @param mixed $msg 日志信息
* @param string $type 日志級別
* @param array $context 替換內容
* @param bool $lazy
* @return $this
*/
public function record($msg, string $type = 'info', array $context = [], bool $lazy = true)
{
$channel = $this->getConfig('type_channel.' . $type);
$this->channel($channel)->record($msg, $type, $context, $lazy);
return $this;
}
/**
* 實時寫入日志信息
* @access public
* @param mixed $msg 調試信息
* @param string $type 日志級別
* @param array $context 替換內容
* @return $this
*/
public function write($msg, string $type = 'info', array $context = [])
{
return $this->record($msg, $type, $context, false);
}
/**
* 注冊日志寫入事件監聽
* @param $listener
* @return Event
*/
public function listen($listener)
{
return $this->app->event->listen(LogWrite::class, $listener);
}
/**
* 記錄日志信息
* @access public
* @param string $level 日志級別
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function log($level, $message, array $context = []): void
{
$this->record($message, $level, $context);
}
/**
* 記錄emergency信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function emergency($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄警報信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function alert($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄緊急情況
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function critical($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄錯誤信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function error($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄warning信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function warning($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄notice信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function notice($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄一般信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function info($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄調試信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function debug($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
/**
* 記錄sql信息
* @access public
* @param mixed $message 日志信息
* @param array $context 替換內容
* @return void
*/
public function sql($message, array $context = []): void
{
$this->log(__FUNCTION__, $message, $context);
}
public function __call($method, $parameters)
{
$this->log($method, ...$parameters);
}
~~~
- 序言
- 為什么要編碼規范?
- 如何進行編碼規范?
- 編碼規范宏觀微觀細節
- PHP編碼規范
- 基礎規范
- 1 語法規范
- 2 變量命名規范
- 3 常量命名規范
- 4 類命名規范
- 5 函數命名規范
- 6 方法命名規范
- PSR-規范
- 基本代碼規范
- Tp項目規范
- TP命名規范
- Tp目錄規范
- Tp基礎目錄構架
- Tp項目開發思考
- Tp控制器規范
- 控制器構架
- Tp模型規范
- 模型性能優化
- Tp業務規范
- 返回結構規范
- 業務異常規范
- Tp輸出規范
- Restful API
- 模板渲染輸出
- Tp異常規范
- 異常碼狀態碼
- 異常輸出方式
- Tp驗證規范
- Tp路由規范
- Tp加密規范
- Password Hashing
- Tp緩存規范
- 常見數據緩存
- 緩存設計思考
- Tp日志規范
- 日志信息
- 日志分析
- Tp日志接管分析
- Tp性能優化
- vendor包規范
- 項目自動化思考
- 項目檢測告警思考
- 項目注釋規范
- Mysql設計規范
- 序言
- 命名規范
- 表及字段規范
- 索引規范
- 索引原理
- Sql規范
- 事務規范
- 讀寫分離
- 樂觀鎖悲觀鎖
- 數據庫審計
- 性能優化
- 查詢優化神器
- 慢查詢優化步驟
- 分庫分表、分區表
- 根據sql日志篩選數據
- 設計原則
- MongoDB規范
- MongoDB基礎
- MongoDB設計
- MongoDB安全性
- MongoDB備份
- 操作手冊規范
- API文檔說明規范
- 管理端操作手冊
- 用戶使用說明書
- 溯源項目構想