123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace think;
- use InvalidArgumentException;
- use Psr\Log\LoggerInterface;
- use think\event\LogWrite;
- use think\helper\Arr;
- use think\log\Channel;
- use think\log\ChannelSet;
- /**
- * 日志管理类
- * @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);
- }
- }
|