Log.php 3.9 KB

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