Source of file fileLogger.php
Size: 4,294 Bytes - Last Modified: 2021-12-24T05:16:28+00:00
/var/www/docs.ssmods.com/process/src/code/thirdparty/fileLogger.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 | <?php require_once(dirname(__FILE__).'/abstractLoggerHandler.php'); /** * File logger * * A basic implementation of logging mechanism intended for debugging * * @author SOFORT AG (integration@sofort.com) * * @copyright 2010-2014 SOFORT AG * * @license Released under the GNU LESSER GENERAL PUBLIC LICENSE (Version 3) * @license http://www.gnu.org/licenses/lgpl.html * * @version SofortLib 2.1.1 * * @link http://www.sofort.com/ official website */ class FileLogger extends AbstractLoggerHandler { /** * File Handler * * @var resource $fp */ public $fp = null; /** * Maximum size of a log file in Bytes * * @var int $maxFilesize */ public $maxFilesize = 1048576; /** * Path to errors logfile * * @var string $_errorLogfilePath */ protected $_errorLogfilePath = false; /** * Path to Logfile * * @var string $_logfilePath */ protected $_logfilePath = false; /** * Path to warnings logfile * * @var string $_warningsLogfilePath */ protected $_warningsLogfilePath = false; /** * Constructor * Setting the LogfilePaths * * @param string $path */ public function __construct($path = '') { $this->_logfilePath = ($path != '') ? $path : dirname(__FILE__).'/logs/log.txt'; $this->_errorLogfilePath = dirname(__FILE__).'/logs/error_log.txt'; $this->_warningsLogfilePath = dirname(__FILE__).'/logs/warning_log.txt'; } /** * Setting a log entry * * @param string $message * @param string $log (default = 'log') * @return bool */ public function log($message, $log = 'log') { return $this->_log($message, $log); } /** * Set the path of the logfile * * @param string $path * @return void */ public function setLogfilePath($path) { $this->_logfilePath = $path; } /** * Logs $msg to a file which path is being set by it's unified resource locator * * @param string $message * @param string $log (default = 'log') * @return bool */ protected function _log($message, $log = 'log') { switch ($log) { case 'error': $file = $this->_errorLogfilePath; break; case 'warning': $file = $this->_warningsLogfilePath; break; default: case 'log': $file = $this->_logfilePath; } if (!is_file($file)) { $this->fp = fopen($file, 'w'); fclose($this->fp); } if (is_writable($file)) { if ($log == 'log' && $this->_logRotate()) { $this->fp = fopen($file, 'w'); fclose($this->fp); } $this->fp = fopen($file, 'a'); fwrite($this->fp, '['.date('Y-m-d H:i:s').'] '.$message."\n"); fclose($this->fp); return true; } return false; } /** * Copy the content of the logfile to a backup file if file size got too large * Put the old log file into a tarball for later reference * * @return bool */ protected function _logRotate() { if (!is_writable($this->_logfilePath)) { return false; } $date = date('Y-m-d_h-i-s', time()); if (file_exists($this->_logfilePath)) { if ($this->fp != null && filesize($this->_logfilePath) != false && filesize($this->_logfilePath) >= $this->maxFilesize) { $oldUri = $this->_logfilePath; // file ending $ending = $ext = pathinfo($oldUri, PATHINFO_EXTENSION); $newUri = dirname($oldUri).'/log_'.$date.'.'.$ending; rename($oldUri, $newUri); if (file_exists($oldUri)) { unlink($oldUri); } return true; } } return false; } } |