<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## psr-3規范 ``` composer require psr/log ``` (`psr` 見 關于`psr規范解釋`) 這篇的跟`數據庫連接 (多例模式)` 有些類似,同樣是 `多例模式` ## 創建配置文件 config/log.php ``` <?php return [ 'default' => 'file1', 'channels' => [ 'file1' => [ // 文件類型的日志 'driver' => 'stack', 'path' => FRAME_BASE_PATH.'/storage/', 'format' => '[%s][%s] %s', // 格式化類型 分別代表:[日期][日志級別]消息 ], 'file2' => [ 'driver' => 'daily', 'path' => FRAME_BASE_PATH.'/storage/', 'format' => '[%s][%s] %s', // 格式化類型 ] ] ]; ``` ## 創建core/log/driver/StackLogger.php 這個日志是把所有內容都放同一個文件的。 現在可以直接 `new` 這個類,傳遞配置進去就可以了。 但是這樣不完善,如果想用 按日期分類的日志怎么辦? 所以想要一個 `調用者` 來控制,這個 `調用者是多例的`。 ``` <?php namespace core\log\driver; use Illuminate\Contracts\Validation\ValidatesWhenResolved; use Psr\Log\AbstractLogger; use Psr\Log\LoggerInterface; use Psr\Log\LoggerTrait; class StackLogger extends AbstractLogger { protected $config; public function __construct(array $config) { $this->config = $config; } // 來自: https://learnku.com/docs/psr/psr-3-logger-interface/1607 /** * @example 代碼:app('log')->info('{language} is the best language in the world’,['language' => 'php']) 返回: php is the best language in the world * @example 說白了 就是替換而已 * @param $message 原本消息 * @param $context 上下文 要替換的 * @return string */ public function interpolate($message, array $context = array()) { // 構建一個花括號包含的鍵名的替換數組 $replace = array(); foreach ($context as $key => $val) { // 檢查該值是否可以轉換為字符串 if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { $replace['{' . $key . '}'] = $val; } } // 替換記錄信息中的占位符,最后返回修改后的記錄信息。 return strtr($message, $replace); } /** * @inheritDoc */ public function log($level, $message, array $context = array()) { if( is_array($message)) $message = var_export($message,true) . var_export($context,true); // 設置true 不輸出 else if( is_string($message)) // 內容是字符串 并且 $context是數組 替換占位符 $message = $this->interpolate($message,$context); $message = sprintf($this->config['format'],date('y-m-d h:m:s'),$level,$message); // 根據配置文件格式化 /** * error_log函數解釋 * @param $message 日志內容 * @param $message_type 類型 3是寫入到文件 * @param $destination 因為是3 填文件路徑 */ error_log($message.PHP_EOL,3,$this->config['path'].'/php_frame.log'); } } ``` ### 關于AbstractLogger.php的解釋 這個是 `psr-log` 自帶的類,很簡單,只有幾個方法。 可以讓我們少些幾行代碼。 ![](https://img.kancloud.cn/7d/dd/7dddc233966a2d3360753e287b29bee7_774x1005.png) ## 創建調用者 core/log/Logger.php ``` <?php namespace core\log; use core\log\driver\DailyLogger; use core\log\driver\StackLogger; class Logger { protected $channels = []; // 所有的實例化的通道 就是多例而已 protected $config; public function __construct() { $this->config = \App::getContainer()->get('config')->get('log'); } public function channel($name = null) { if(! $name) // 沒選擇名字 $name = $this->config['default']; if( isset($this->channels[$name])) return $this->channels[$name]; $config = \App::getContainer()->get('config')->get('log.channels.'.$name); //如:$config['driver'] = stack, 則調用createStack($config); return $this->channels['name'] = $this->{'create'.ucfirst($config['driver'])}($config); } // 放在同一個文件 public function createStack($config) { return new StackLogger($config); } public function __call($method, $parameters) { return $this->channel()->$method(...$parameters); } } ``` ## 綁定到容器 ![](https://img.kancloud.cn/b2/af/b2af6a95b71e9163b2102e67ca2c248d_790x308.png) ## 運行 ![](https://img.kancloud.cn/1a/8c/1a8cdaf45d326b2990df2c100a3a6127_948x199.png) ![](https://img.kancloud.cn/6b/1f/6b1fdeedf243210ca2ec17031ddd6d54_964x186.png) ![](https://img.kancloud.cn/d5/23/d5237af3d354463915b6869e028f59b9_1094x422.png) ## 結尾 ![](https://img.kancloud.cn/da/5e/da5e4ee1e2caf60cfb95fb0e297e9702_844x491.png) `driver` = `daily` 本教程未實現,太多代碼了。 但是讀者可自行實現。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看