LoggerFile.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace app\models\logger;
  3. use Yii;
  4. use app\models\logger\ILogger;
  5. /**
  6. * This is the model class for table "os_logs".
  7. *
  8. * @property int $id
  9. * @property string $date
  10. * @property string $section
  11. * @property string $error_text
  12. */
  13. class LoggerFile extends \yii\db\ActiveRecord implements \app\models\logger\ILogger
  14. {
  15. private $cat_name;
  16. private function saveToFile($message)
  17. {
  18. //$file = $_SERVER['DOCUMENT_ROOT'].'/api/'.$this->cat_name .'/'. date('Y-m-d').'.txt';
  19. $structure = $_SERVER['DOCUMENT_ROOT'].'/api/logs'.'/'.$this->cat_name;
  20. if ( !is_dir($structure) and !mkdir($structure, 0777, true)) {
  21. die('Не удалось создать директории...');
  22. }
  23. //var_dump('sdfsdf');
  24. $file = $structure . '/'.date('Y-m-d').'.txt';
  25. $current = '';
  26. if ( is_file($file)) {
  27. $current = file_get_contents($file);
  28. }
  29. $current .= $message . "\n";
  30. $res = file_put_contents($file, $current);
  31. // сохраняем в файл
  32. }
  33. public function log($message,$cat) {
  34. // запись ошибки в логфайл
  35. $cat = explode('\\',$cat);
  36. $this->cat_name = array_pop($cat);
  37. $this->saveToFile($message);
  38. }
  39. }