Logger.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. /**
  10. * Logger with common logging options into a file.
  11. *
  12. * @package Pimf
  13. * @author Gjero Krsteski <[email protected]>
  14. */
  15. class Logger
  16. {
  17. /**
  18. * @var resource
  19. */
  20. private $handle;
  21. /**
  22. * @var resource
  23. */
  24. private $warnHandle;
  25. /**
  26. * @var resource
  27. */
  28. private $errorHandle;
  29. /**
  30. * @var string
  31. */
  32. private $storageDir;
  33. /**
  34. * @var bool
  35. */
  36. private $separator;
  37. /**
  38. * @param string $localeStorageDir Use better the local TMP dir or dir with mod 777.
  39. * @param bool $trailingSeparator
  40. */
  41. public function __construct($localeStorageDir, $trailingSeparator = true)
  42. {
  43. $this->storageDir = (string)$localeStorageDir;
  44. $this->separator = (bool)$trailingSeparator;
  45. }
  46. /**
  47. * @throws \RuntimeException If something went wrong on creating the log dir and file.
  48. */
  49. public function init()
  50. {
  51. if (is_resource($this->errorHandle)
  52. && is_resource($this->handle)
  53. && is_resource($this->warnHandle)
  54. ) {
  55. return;
  56. }
  57. if (!is_dir($this->storageDir)) {
  58. mkdir($this->storageDir, 0777);
  59. }
  60. if (true === $this->separator) {
  61. $this->storageDir = rtrim(realpath($this->storageDir), '\\/') . DS;
  62. }
  63. $this->handle = fopen($this->storageDir . "pimf-logs.txt", "at+");
  64. $this->warnHandle = fopen($this->storageDir . "pimf-warnings.txt", "at+");
  65. $this->errorHandle = fopen($this->storageDir . "pimf-errors.txt", "at+");
  66. if (!$this->errorHandle || !$this->handle || !$this->warnHandle) {
  67. throw new \RuntimeException("failed to obtain a handle to logger file");
  68. }
  69. }
  70. /**
  71. * @param string $msg
  72. *
  73. * @return Logger
  74. */
  75. public function debug($msg)
  76. {
  77. if ($this->iniGetBool('display_errors') === true) {
  78. $this->write((string)$msg, 'DEBUG');
  79. }
  80. return $this;
  81. }
  82. /**
  83. * @param string $msg
  84. *
  85. * @return Logger
  86. */
  87. public function warn($msg)
  88. {
  89. if ($this->iniGetBool('display_errors') === true) {
  90. $this->write((string)$msg, 'WARNING');
  91. }
  92. return $this;
  93. }
  94. /**
  95. * @param string $msg
  96. *
  97. * @return Logger
  98. */
  99. public function error($msg)
  100. {
  101. $this->write((string)$msg, 'ERROR');
  102. return $this;
  103. }
  104. /**
  105. * @param string $msg
  106. *
  107. * @return Logger
  108. */
  109. public function info($msg)
  110. {
  111. if ($this->iniGetBool('display_errors') === true) {
  112. $this->write((string)$msg, 'INFO');
  113. }
  114. return $this;
  115. }
  116. /**
  117. * @param string $msg
  118. * @param string $severity
  119. */
  120. protected function write($msg, $severity = 'DEBUG')
  121. {
  122. $msg = $this->format($msg, $severity);
  123. // if severity is WARNING then write to warning file
  124. if ($severity == 'WARNING') {
  125. fwrite($this->warnHandle, $msg);
  126. } // if severity is ERROR then write to error file
  127. else if ($severity == 'ERROR') {
  128. fwrite($this->errorHandle, $msg);
  129. } else {
  130. fwrite($this->handle, $msg);
  131. }
  132. }
  133. public function __destruct()
  134. {
  135. if (is_resource($this->handle)
  136. && is_resource($this->warnHandle)
  137. && is_resource($this->errorHandle)
  138. ) {
  139. if (fclose($this->handle) === false) {
  140. // Failure to close the log file
  141. $this->error('Logger failed to close the handle to the log file');
  142. }
  143. fclose($this->warnHandle);
  144. fclose($this->errorHandle);
  145. }
  146. }
  147. /**
  148. * Formats the error message in representable manner.
  149. *
  150. * @param string $message
  151. * @param string $severity
  152. *
  153. * @return string
  154. */
  155. private function format($message, $severity)
  156. {
  157. $registry = new Registry();
  158. $remoteIP = $registry->env->getIp();
  159. $script = $registry->env->PHP_SELF;
  160. $msg = date("m-d-Y") . " " . date("G:i:s") . " " . $remoteIP;
  161. $IPLength = strlen($remoteIP);
  162. $numWhitespaces = 15 - $IPLength;
  163. for ($i = 0; $i < $numWhitespaces; $i++) {
  164. $msg .= " ";
  165. }
  166. $msg .= " " . $severity . ": ";
  167. // get the file name
  168. $lastSlashIndex = strrpos($script, "/");
  169. $fileName = $script;
  170. if ($lastSlashIndex !== false) {
  171. $fileName = substr($script, $lastSlashIndex + 1);
  172. }
  173. $msg .= $fileName . "\t";
  174. $msg .= $severity;
  175. $msg .= ": " . $message . "\r\n";
  176. return $msg;
  177. }
  178. /**
  179. * @param string $varname
  180. *
  181. * @return bool
  182. */
  183. protected function iniGetBool($varname)
  184. {
  185. $varvalue = ini_get($varname);
  186. switch (strtolower($varvalue)) {
  187. case 'on':
  188. case 'yes':
  189. case 'true':
  190. return 'assert.active' !== $varname;
  191. case 'stdout':
  192. case 'stderr':
  193. return 'display_errors' === $varname;
  194. default:
  195. return (bool)(int)$varvalue;
  196. }
  197. }
  198. }