<?php

require_once('Logger/class.LoggerBackend.php');

class fileLoggerBackend extends LoggerBackend {

   private $logLevel;
   private $hLogFile;

   public function __construct($urlData) {
      global $cfg;  //  tablica konfiguracyjna z zewntrznego pliku

      parent::__construct($urlData);

      $this->logLevel = $cfg['LOGGER_LEVEL'];

      $logFilePath = $this->urlData['path'];
      if(! strlen($logFilePath)) {
         throw new Exception('W identyfikatorze poczenia ' .
                             'nie podano cieki do pliku.');
      }

      // Otwiera plik dziennika. Tymczasowo wycza komunikaty o bdach.
      // Z bdami poradzimy sobie samodzielnie, zgaszajc wyjtek.
      $this->hLogFile = @fopen($logFilePath, 'a+');
      if(! is_resource($this->hLogFile)) {
         throw new Exception("Plik dziennika $logFilePath " .
                             'nie moe zosta otwarty ani utworzony.' .
                             'Sprawd uprawnienia.');
      }

   }

   public function logMessage($msg, $logLevel = LOGGER_INFO, $module = null) {
      if($logLevel <= $this->logLevel) {
         $time = strftime('%x %X', time());
         $msg = str_replace("\t", '    ', $msg);
         $msg = str_replace("\n", ' ', $msg);

         $strLogLevel = Logger::levelToString($logLevel);

         if(isset($module)) {
            $module = str_replace("\t", '    ', $module);
            $module = str_replace("\n", ' ', $module);
         }

         // zapisuje: data/czas poziom komunikat modu
         // oddzielony tabulacjami, zakoczony znakiem nowej linii
         $logLine = "$time\t$strLogLevel\t$msg\t$module\n";
         fwrite($this->hLogFile, $logLine);
      }
   }

}
?>