Log.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * La classe Log permet de logger des erreurs
  8. */
  9. class Minz_Log {
  10. /**
  11. * Les différents niveau de log
  12. * ERROR erreurs bloquantes de l'application
  13. * WARNING erreurs pouvant géner le bon fonctionnement, mais non bloquantes
  14. * NOTICE erreurs mineures ou messages d'informations
  15. * DEBUG Informations affichées pour le déboggage
  16. */
  17. const ERROR = 2;
  18. const WARNING = 4;
  19. const NOTICE = 8;
  20. const DEBUG = 16;
  21. /**
  22. * Enregistre un message dans un fichier de log spécifique
  23. * Message non loggué si
  24. * - environment = SILENT
  25. * - level = WARNING et environment = PRODUCTION
  26. * - level = NOTICE et environment = PRODUCTION
  27. * @param $information message d'erreur / information à enregistrer
  28. * @param $level niveau d'erreur
  29. * @param $file_name fichier de log
  30. * @throws Minz_PermissionDeniedException
  31. */
  32. public static function record ($information, $level, $file_name = null) {
  33. try {
  34. $conf = Minz_Configuration::get('system');
  35. $env = $conf->environment;
  36. } catch (Minz_ConfigurationException $e) {
  37. $env = 'production';
  38. }
  39. if (! ($env === 'silent'
  40. || ($env === 'production'
  41. && ($level >= Minz_Log::NOTICE)))) {
  42. if ($file_name === null) {
  43. $username = Minz_Session::param('currentUser', '');
  44. if ($username == '') {
  45. $username = '_';
  46. }
  47. $file_name = join_path(USERS_PATH, $username, 'log.txt');
  48. }
  49. switch ($level) {
  50. case Minz_Log::ERROR :
  51. $level_label = 'error';
  52. break;
  53. case Minz_Log::WARNING :
  54. $level_label = 'warning';
  55. break;
  56. case Minz_Log::NOTICE :
  57. $level_label = 'notice';
  58. break;
  59. case Minz_Log::DEBUG :
  60. $level_label = 'debug';
  61. break;
  62. default :
  63. $level_label = 'unknown';
  64. }
  65. $log = '[' . date('r') . ']'
  66. . ' [' . $level_label . ']'
  67. . ' --- ' . $information . "\n";
  68. self::ensureMaxLogSize($file_name);
  69. if (file_put_contents($file_name, $log, FILE_APPEND | LOCK_EX) === false) {
  70. throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
  71. }
  72. }
  73. }
  74. /**
  75. * Make sure we do not waste a huge amount of disk space with old log messages.
  76. *
  77. * This method can be called multiple times for one script execution, but its result will not change unless
  78. * you call clearstatcache() in between. We won't due do that for performance reasons.
  79. *
  80. * @param $file_name
  81. * @throws Minz_PermissionDeniedException
  82. */
  83. protected static function ensureMaxLogSize($file_name) {
  84. $maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 1048576;
  85. if ($maxSize > 0 && @filesize($file_name) > $maxSize) {
  86. $fp = fopen($file_name, 'c+');
  87. if ($fp && flock($fp, LOCK_EX)) {
  88. fseek($fp, -intval($maxSize / 2), SEEK_END);
  89. $content = fread($fp, $maxSize);
  90. rewind($fp);
  91. ftruncate($fp, 0);
  92. fwrite($fp, $content ? $content : '');
  93. fwrite($fp, sprintf("[%s] [notice] --- Log rotate.\n", date('r')));
  94. fflush($fp);
  95. flock($fp, LOCK_UN);
  96. } else {
  97. throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
  98. }
  99. if ($fp) {
  100. fclose($fp);
  101. }
  102. }
  103. }
  104. /**
  105. * Automatise le log des variables globales $_GET et $_POST
  106. * Fait appel à la fonction record(...)
  107. * Ne fonctionne qu'en environnement "development"
  108. * @param $file_name fichier de log
  109. */
  110. public static function recordRequest($file_name = null) {
  111. $msg_get = str_replace("\n", '', '$_GET content : ' . print_r($_GET, true));
  112. $msg_post = str_replace("\n", '', '$_POST content : ' . print_r($_POST, true));
  113. self::record($msg_get, Minz_Log::DEBUG, $file_name);
  114. self::record($msg_post, Minz_Log::DEBUG, $file_name);
  115. }
  116. /**
  117. * Some helpers to Minz_Log::record() method
  118. * Parameters are the same of those of the record() method.
  119. */
  120. public static function debug($msg, $file_name = null) {
  121. self::record($msg, Minz_Log::DEBUG, $file_name);
  122. }
  123. public static function notice($msg, $file_name = null) {
  124. self::record($msg, Minz_Log::NOTICE, $file_name);
  125. }
  126. public static function warning($msg, $file_name = null) {
  127. self::record($msg, Minz_Log::WARNING, $file_name);
  128. }
  129. public static function error($msg, $file_name = null) {
  130. self::record($msg, Minz_Log::ERROR, $file_name);
  131. }
  132. }