Advertisement
schpnc

Logger

Jul 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2.  
  3. class Logger
  4. {
  5.     private $logPath;
  6.     private $files;
  7.  
  8.     public function __construct($relativeLogPath = '/logs', $files = 10)
  9.     {
  10.         if (class_exists('\Bitrix\Main\Application')) {
  11.             $this->logPath = \Bitrix\Main\Application::getDocumentRoot() . $relativeLogPath;
  12.         } elseif ($_SERVER['DOCUMENT_ROOT']) {
  13.             $this->logPath = $_SERVER['DOCUMENT_ROOT'] . $relativeLogPath;
  14.         } else {
  15.             $this->logPath = __DIR__ . $relativeLogPath;
  16.         }
  17.         $this->files = $files;
  18.     }
  19.  
  20.     public function write($dump, $file = 'info', $message = '')
  21.     {
  22.         if (!is_dir($this->logPath . '/')) {
  23.             mkdir($this->logPath . '/');
  24.         }
  25.         $file = $this->logPath . '/' . $file . '.log';
  26.  
  27.         if (!is_string($dump)) {
  28.             $dump = var_export($dump, true);
  29.         }
  30.  
  31.         ignore_user_abort(true);
  32.         if ($fp = @fopen($file, "ab")) {
  33.             if (flock($fp, LOCK_EX)) {
  34.                 @fwrite($fp, "Date: " . date("Y-m-d H:i:s") . "\n");
  35.                 if ($message) {
  36.                     @fwrite($fp, "Message: " . $message . "\n");
  37.                 }
  38.                 @fwrite($fp, "Dump: " . $dump . "\n");
  39.                 @fwrite($fp, str_repeat('-', 30) . "\n\n");
  40.                 @fflush($fp);
  41.                 @flock($fp, LOCK_UN);
  42.                 @fclose($fp);
  43.             }
  44.         }
  45.         ignore_user_abort(false);
  46.  
  47.         // if filesize more than 10 Mb rotate it
  48.         if (filesize($file) > 10485760) {
  49.             $this->rotate($file);
  50.         }
  51.     }
  52.  
  53.     private function rotate($file)
  54.     {
  55.         $path = pathinfo($file);
  56.         $rotate = implode('', array(
  57.             $path['dirname'],
  58.             '/',
  59.             $path['filename'],
  60.             '_',
  61.             date('Y-m-d_H:i:s'),
  62.             '.',
  63.             $path['extension']
  64.         ));
  65.  
  66.         copy($file, $rotate);
  67.         $this->clean($file);
  68.  
  69.         $files = glob($path['dirname'] . '/' . $path['filename'] . "*" . ".log");
  70.  
  71.         if (0 === $this->files) {
  72.             return;
  73.         }
  74.  
  75.         if (count($files) > $this->files) {
  76.             natsort($files);
  77.             $files = array_reverse($files);
  78.             foreach (array_slice($files, $this->files) as $log) {
  79.                 if (is_writable($log)) {
  80.                     unlink($log);
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     private function clean($file)
  87.     {
  88.         file_put_contents($file, '');
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement