Log.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. const MAX_LOG_SIZE = 512000; // 500kB
  22. /**
  23. * Enregistre un message dans un fichier de log spécifique
  24. * Message non loggué si
  25. * - environment = SILENT
  26. * - level = WARNING et environment = PRODUCTION
  27. * - level = NOTICE et environment = PRODUCTION
  28. * @param $information message d'erreur / information à enregistrer
  29. * @param $level niveau d'erreur
  30. * @param $file_name fichier de log
  31. * @throws Minz_PermissionDeniedException
  32. */
  33. public static function record ($information, $level, $file_name = null) {
  34. try {
  35. $conf = Minz_Configuration::get('system');
  36. $env = $conf->environment;
  37. } catch (Minz_ConfigurationException $e) {
  38. $env = 'production';
  39. }
  40. if (! ($env === 'silent'
  41. || ($env === 'production'
  42. && ($level >= Minz_Log::NOTICE)))) {
  43. if ($file_name === null) {
  44. $username = Minz_Session::param('currentUser', '');
  45. if ($username == '') {
  46. $username = '_';
  47. }
  48. $file_name = join_path(USERS_PATH, $username, 'log.txt');
  49. }
  50. switch ($level) {
  51. case Minz_Log::ERROR :
  52. $level_label = 'error';
  53. break;
  54. case Minz_Log::WARNING :
  55. $level_label = 'warning';
  56. break;
  57. case Minz_Log::NOTICE :
  58. $level_label = 'notice';
  59. break;
  60. case Minz_Log::DEBUG :
  61. $level_label = 'debug';
  62. break;
  63. default :
  64. $level_label = 'unknown';
  65. }
  66. $log = '[' . date('r') . ']'
  67. . ' [' . $level_label . ']'
  68. . ' --- ' . $information . "\n";
  69. self::checkForLogfileSize($file_name);
  70. if (file_put_contents($file_name, $log, FILE_APPEND | LOCK_EX) === false) {
  71. throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
  72. }
  73. }
  74. }
  75. /**
  76. * Make sure we do not waste a huge amount of disk space with old log messages.
  77. *
  78. * This method can be called multiple times for one script execution, but its result will not change unless
  79. * you call clearstatcache() in between. We won't due do that for performance reasons.
  80. *
  81. * @param $file_name
  82. * @throws Minz_PermissionDeniedException
  83. */
  84. protected static function checkForLogfileSize($file_name) {
  85. if (file_exists($file_name) && filesize($file_name) > self::MAX_LOG_SIZE) {
  86. if (!unlink($file_name)) {
  87. throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
  88. }
  89. }
  90. }
  91. /**
  92. * Automatise le log des variables globales $_GET et $_POST
  93. * Fait appel à la fonction record(...)
  94. * Ne fonctionne qu'en environnement "development"
  95. * @param $file_name fichier de log
  96. */
  97. public static function recordRequest($file_name = null) {
  98. $msg_get = str_replace("\n", '', '$_GET content : ' . print_r($_GET, true));
  99. $msg_post = str_replace("\n", '', '$_POST content : ' . print_r($_POST, true));
  100. self::record($msg_get, Minz_Log::DEBUG, $file_name);
  101. self::record($msg_post, Minz_Log::DEBUG, $file_name);
  102. }
  103. /**
  104. * Some helpers to Minz_Log::record() method
  105. * Parameters are the same of those of the record() method.
  106. */
  107. public static function debug($msg, $file_name = null) {
  108. self::record($msg, Minz_Log::DEBUG, $file_name);
  109. }
  110. public static function notice($msg, $file_name = null) {
  111. self::record($msg, Minz_Log::NOTICE, $file_name);
  112. }
  113. public static function warning($msg, $file_name = null) {
  114. self::record($msg, Minz_Log::WARNING, $file_name);
  115. }
  116. public static function error($msg, $file_name = null) {
  117. self::record($msg, Minz_Log::ERROR, $file_name);
  118. }
  119. }