gy-doc-code
[ class tree: gy-doc-code ] [ index: gy-doc-code ] [ all elements ]

Source for file FileRoute.php

Documentation is available at FileRoute.php

  1. <?php
  2.  
  3. namespace Psr\Log\Logger;
  4.  
  5. use Psr\Log\Logger\Route;
  6.  
  7. /**
  8.  * Class FileRoute - роут который будет писать логи в файл
  9.  */
  10. class FileRoute extends Route
  11. {
  12.     public $filePath; // путь к файлу
  13.  
  14.     // шаблон сообщения
  15.     public $template = "{date} {level} {message} {context}";
  16.  
  17.     public function __construct(array $attributes = array())
  18.     {
  19.         parent::__construct($attributes);
  20.  
  21.         if (!file_exists($this->filePath))
  22.         {
  23.             touch($this->filePath);
  24.         }
  25.     }
  26.  
  27.     public function log($level, $message, array $context = array())
  28.     {
  29.         file_put_contents(
  30.             $this->filePath, 
  31.             trim(
  32.                 strtr(
  33.                     $this->template, 
  34.                     array(
  35.                         '{date}' => $this->getDate(),
  36.                         '{level}' => $level,
  37.                         '{message}' => $message,
  38.                         '{context}' => $this->contextStringify($context),
  39.                     )
  40.                 )
  41.             ).PHP_EOL, 
  42.             FILE_APPEND
  43.         );
  44.     }
  45. }

Documentation generated on Sat, 27 Apr 2024 11:17:29 +0300 by phpDocumentor 1.4.3