PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Framework/Packages/Framework/Logger/Logger.php

https://gitlab.com/Erdrix/overviewCompanies
PHP | 186 lines | 103 code | 24 blank | 59 comment | 10 complexity | 7c888380f4b1800b878e8eeecc844401 MD5 | raw file
  1. <?php
  2. class Logger
  3. {
  4. //////////////////////////
  5. // Attributs
  6. /////////
  7. /**
  8. * Droit sur la création des fichiers
  9. * @var integer
  10. */
  11. private $permission;
  12. /**
  13. * Chemin vers le dossier de log
  14. * @var mixed
  15. */
  16. private $path;
  17. //////////////////////////
  18. // Constructeur
  19. /////////
  20. public function __construct()
  21. {
  22. $conf = Conf::get('global');
  23. $this->path = $conf['logs'] ? getTemporary('logs') : false;
  24. $this->permission = $conf['permission']['files'];
  25. register_shutdown_function([$this, '__shutdown']);
  26. set_exception_handler([$this, '__dispatcher']);
  27. set_error_handler([$this, '__error']);
  28. ini_set('display_errors', 'off');
  29. }
  30. //////////////////////////
  31. // Public methods
  32. /////////
  33. /**
  34. * Intercepte les erreurs critiques
  35. */
  36. public function __shutdown()
  37. {
  38. $last_error = error_get_last();
  39. if (E_ERROR === $last_error['type'] || 4 === $last_error['type']) {
  40. $this->__error($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']);
  41. }
  42. }
  43. /**
  44. * Intercepte les erreurs non critiques
  45. *
  46. * @param integer $code : Code de l'erreur
  47. * @param string $message : Message de l'erreur
  48. * @param string $file : Chemin vers le fichier provoquant l'erreur
  49. * @param integer $line : Numéro de la ligne provoquant l'erreur
  50. */
  51. public function __error($code, $message, $file, $line)
  52. {
  53. if (E_ERROR === $code || 4 === $code) {
  54. $e = new ErrorException($message, $code, $code, $file, $line);
  55. $this->__dispatcher($e);
  56. }
  57. }
  58. /**
  59. * Intercepte les exception et les sauvegarde
  60. *
  61. * @param Exception $e
  62. */
  63. public function __dispatcher(Exception $e)
  64. {
  65. // Log error here
  66. if ($this->path) {
  67. $path = $this->path . DS . date('m-d-Y') . '.log';
  68. file_put_contents($path,
  69. '[' . date('H:i:s') . '] Erreur dans le fichier ' . $e->getFile() . ' a la ligne ' . $e->getLine() . "\n " . $e->getMessage() . "\n",
  70. FILE_APPEND);
  71. chmod($path, octdec('0' . $this->permission));
  72. }
  73. // Save data
  74. $traces[] = $this->generateTrace($e);
  75. foreach ($e->getTrace() as $trace) {
  76. $traces[] = $this->generateTrace($trace);
  77. }
  78. // Suppresion du code
  79. while (ob_get_level() > 0) {
  80. ob_end_clean();
  81. }
  82. // Affichage des erreurs
  83. Application::render('@framework.logger/error', ['traces' => $traces, 'page' => $this->getPageStats()]);
  84. // Fin du script
  85. die();
  86. }
  87. //////////////////////////
  88. // Private
  89. /////////
  90. /**
  91. * Génere une trace valide de l'erreur
  92. *
  93. * @param $trace : Trace à valider
  94. *
  95. * @return array
  96. */
  97. private function generateTrace($trace)
  98. {
  99. if ($trace instanceof Exception) {
  100. return [
  101. 'file' => $trace->getFile(),
  102. 'line' => $trace->getLine(),
  103. 'class' => get_class($trace),
  104. 'args' => [$trace->getMessage()],
  105. 'function' => get_class($trace),
  106. ];
  107. }
  108. return [
  109. 'file' => isset($trace['file']) ? $trace['file'] : false,
  110. 'line' => $trace['line'],
  111. 'class' => isset($trace['class']) ? $trace['class'] : false,
  112. 'args' => $this->generateSerializable($trace['args']),
  113. 'function' => (isset($trace['class']) && strpos($trace['function'],
  114. '{') === false) ? ($trace['class'] . $trace['type'] . $trace['function']) : ucfirst($trace['function']),
  115. ];
  116. }
  117. /**
  118. * Prépare un tableau pour une sérialisation
  119. *
  120. * @param array $array : Tableau a préparer pour une sérialisation
  121. *
  122. * @return array
  123. */
  124. private function generateSerializable($array)
  125. {
  126. $result = [];
  127. foreach ($array as $value) {
  128. $type = gettype($value);
  129. if ($value instanceof Closure) {
  130. $reflection = new ReflectionFunction($value);
  131. $arguments = $reflection->getParameters();
  132. $list = [];
  133. foreach ($arguments as $param) {
  134. $list[] = $param->getName();
  135. }
  136. if (!empty($list)) {
  137. $value .= 'Closure (<span style="color:orange">$' . implode(', $', $list) . '</span>)';
  138. }
  139. } elseif ('object' === $type) {
  140. $value = 'Class (<span style="color:orange">' . get_class($value) . '</span>)';
  141. } elseif (is_array($value)) {
  142. $value = $this->generateSerializable($value);
  143. }
  144. $result[] = $value;
  145. }
  146. return $result;
  147. }
  148. /**
  149. * Retoure les informations sur la page
  150. * @return array
  151. */
  152. private function getPageStats()
  153. {
  154. return [
  155. 'method' => $_SERVER['REQUEST_METHOD'],
  156. 'software' => $_SERVER['SERVER_SOFTWARE'],
  157. 'adresse' => $_SERVER['REMOTE_ADDR'],
  158. 'uri' => $_SERVER['REQUEST_URI'],
  159. 'query' => $_SERVER['QUERY_STRING'],
  160. ];
  161. }
  162. }