4
0

Log.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * MINZ - Copyright 2011 Marien Fressinaud
  5. * Sous licence AGPL3 <https://www.gnu.org/licenses/>
  6. */
  7. /**
  8. * The Minz_Log class is used to log errors and warnings
  9. */
  10. class Minz_Log {
  11. /**
  12. * Syslog priority corresponding to each value accepted by the `log_level` system setting,
  13. * from the most to the least severe.
  14. * @var array<string,int>
  15. */
  16. private const LOG_LEVELS = [
  17. 'error' => LOG_ERR,
  18. 'warning' => LOG_WARNING,
  19. 'notice' => LOG_NOTICE,
  20. 'info' => LOG_INFO,
  21. 'debug' => LOG_DEBUG,
  22. ];
  23. /**
  24. * Enregistre un message dans un fichier de log spécifique
  25. * Message non loggué si
  26. * - environment = SILENT
  27. * - level est moins sévère que le seuil déterminé par `log_level`,
  28. * ou par défaut par `environment` (PRODUCTION ne garde que warning et error)
  29. * @param string $information message d'erreur / information à enregistrer
  30. * @param int $level niveau d'erreur https://www.php.net/function.syslog
  31. * @param string $file_name fichier de log
  32. * @throws Minz_PermissionDeniedException
  33. */
  34. public static function record(string $information, int $level, ?string $file_name = null): void {
  35. $env = getenv('FRESHRSS_ENV');
  36. $log_level = '';
  37. try {
  38. $conf = Minz_Configuration::get('system');
  39. $log_level = $conf->log_level;
  40. if ($env == '') {
  41. $env = $conf->environment;
  42. }
  43. } catch (Minz_ConfigurationException $e) {
  44. if ($env == '') {
  45. $env = 'production';
  46. }
  47. }
  48. if ($log_level === '' || !isset(self::LOG_LEVELS[$log_level])) {
  49. $log_level = match ($env) {
  50. 'silent' => 'error',
  51. 'production' => 'warning',
  52. default => 'debug',
  53. };
  54. }
  55. if (! ($env === 'silent' || $level > self::LOG_LEVELS[$log_level])) {
  56. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  57. if ($file_name == null) {
  58. $file_name = join_path(USERS_PATH, $username, LOG_FILENAME);
  59. }
  60. $level_labels = array_flip(self::LOG_LEVELS);
  61. if (!isset($level_labels[$level])) {
  62. $level = LOG_INFO;
  63. }
  64. $level_label = $level_labels[$level];
  65. $log = '[' . date('r') . '] [' . $level_label . '] --- ' . str_replace(["\r", "\n"], ' ', $information) . "\n";
  66. if (defined('COPY_LOG_TO_SYSLOG') && COPY_LOG_TO_SYSLOG) {
  67. syslog($level, '[' . $username . '] ' . trim($log));
  68. }
  69. self::ensureMaxLogSize($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 do do that for performance reasons.
  80. *
  81. * @throws Minz_PermissionDeniedException
  82. */
  83. protected static function ensureMaxLogSize(string $file_name): void {
  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 (is_resource($fp) && flock($fp, LOCK_EX)) {
  88. fseek($fp, -(int)($maxSize / 2), SEEK_END);
  89. $content = fread($fp, $maxSize);
  90. rewind($fp);
  91. ftruncate($fp, 0);
  92. fwrite($fp, $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. fclose($fp);
  100. }
  101. }
  102. /**
  103. * Some helpers to Minz_Log::record() method
  104. * Parameters are the same of those of the record() method.
  105. * @throws Minz_PermissionDeniedException
  106. */
  107. public static function debug(string $msg, ?string $file_name = null): void {
  108. self::record($msg, LOG_DEBUG, $file_name);
  109. }
  110. /** @throws Minz_PermissionDeniedException */
  111. public static function notice(string $msg, ?string $file_name = null): void {
  112. self::record($msg, LOG_NOTICE, $file_name);
  113. }
  114. /** @throws Minz_PermissionDeniedException */
  115. public static function warning(string $msg, ?string $file_name = null): void {
  116. self::record($msg, LOG_WARNING, $file_name);
  117. }
  118. /** @throws Minz_PermissionDeniedException */
  119. public static function error(string $msg, ?string $file_name = null): void {
  120. self::record($msg, LOG_ERR, $file_name);
  121. }
  122. }