123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- /**
- * Pimf
- *
- * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
- * @license http://krsteski.de/new-bsd-license New BSD License
- */
- namespace Pimf;
- /**
- * Logger with common logging options into a file.
- *
- * @package Pimf
- * @author Gjero Krsteski <[email protected]>
- */
- class Logger
- {
- /**
- * @var resource
- */
- private $handle;
- /**
- * @var resource
- */
- private $warnHandle;
- /**
- * @var resource
- */
- private $errorHandle;
- /**
- * @var string
- */
- private $storageDir;
- /**
- * @var bool
- */
- private $separator;
- /**
- * @param string $localeStorageDir Use better the local TMP dir or dir with mod 777.
- * @param bool $trailingSeparator
- */
- public function __construct($localeStorageDir, $trailingSeparator = true)
- {
- $this->storageDir = (string)$localeStorageDir;
- $this->separator = (bool)$trailingSeparator;
- }
- /**
- * @throws \RuntimeException If something went wrong on creating the log dir and file.
- */
- public function init()
- {
- if (is_resource($this->errorHandle)
- && is_resource($this->handle)
- && is_resource($this->warnHandle)
- ) {
- return;
- }
- if (!is_dir($this->storageDir)) {
- mkdir($this->storageDir, 0777);
- }
- if (true === $this->separator) {
- $this->storageDir = rtrim(realpath($this->storageDir), '\\/') . DS;
- }
- $this->handle = fopen($this->storageDir . "pimf-logs.txt", "at+");
- $this->warnHandle = fopen($this->storageDir . "pimf-warnings.txt", "at+");
- $this->errorHandle = fopen($this->storageDir . "pimf-errors.txt", "at+");
- if (!$this->errorHandle || !$this->handle || !$this->warnHandle) {
- throw new \RuntimeException("failed to obtain a handle to logger file");
- }
- }
- /**
- * @param string $msg
- *
- * @return Logger
- */
- public function debug($msg)
- {
- if ($this->iniGetBool('display_errors') === true) {
- $this->write((string)$msg, 'DEBUG');
- }
- return $this;
- }
- /**
- * @param string $msg
- *
- * @return Logger
- */
- public function warn($msg)
- {
- if ($this->iniGetBool('display_errors') === true) {
- $this->write((string)$msg, 'WARNING');
- }
- return $this;
- }
- /**
- * @param string $msg
- *
- * @return Logger
- */
- public function error($msg)
- {
- $this->write((string)$msg, 'ERROR');
- return $this;
- }
- /**
- * @param string $msg
- *
- * @return Logger
- */
- public function info($msg)
- {
- if ($this->iniGetBool('display_errors') === true) {
- $this->write((string)$msg, 'INFO');
- }
- return $this;
- }
- /**
- * @param string $msg
- * @param string $severity
- */
- protected function write($msg, $severity = 'DEBUG')
- {
- $msg = $this->format($msg, $severity);
- // if severity is WARNING then write to warning file
- if ($severity == 'WARNING') {
- fwrite($this->warnHandle, $msg);
- } // if severity is ERROR then write to error file
- else if ($severity == 'ERROR') {
- fwrite($this->errorHandle, $msg);
- } else {
- fwrite($this->handle, $msg);
- }
- }
- public function __destruct()
- {
- if (is_resource($this->handle)
- && is_resource($this->warnHandle)
- && is_resource($this->errorHandle)
- ) {
- if (fclose($this->handle) === false) {
- // Failure to close the log file
- $this->error('Logger failed to close the handle to the log file');
- }
- fclose($this->warnHandle);
- fclose($this->errorHandle);
- }
- }
- /**
- * Formats the error message in representable manner.
- *
- * @param string $message
- * @param string $severity
- *
- * @return string
- */
- private function format($message, $severity)
- {
- $registry = new Registry();
- $remoteIP = $registry->env->getIp();
- $script = $registry->env->PHP_SELF;
- $msg = date("m-d-Y") . " " . date("G:i:s") . " " . $remoteIP;
- $IPLength = strlen($remoteIP);
- $numWhitespaces = 15 - $IPLength;
- for ($i = 0; $i < $numWhitespaces; $i++) {
- $msg .= " ";
- }
- $msg .= " " . $severity . ": ";
- // get the file name
- $lastSlashIndex = strrpos($script, "/");
- $fileName = $script;
- if ($lastSlashIndex !== false) {
- $fileName = substr($script, $lastSlashIndex + 1);
- }
- $msg .= $fileName . "\t";
- $msg .= $severity;
- $msg .= ": " . $message . "\r\n";
- return $msg;
- }
- /**
- * @param string $varname
- *
- * @return bool
- */
- protected function iniGetBool($varname)
- {
- $varvalue = ini_get($varname);
- switch (strtolower($varvalue)) {
- case 'on':
- case 'yes':
- case 'true':
- return 'assert.active' !== $varname;
- case 'stdout':
- case 'stderr':
- return 'display_errors' === $varname;
- default:
- return (bool)(int)$varvalue;
- }
- }
- }
|