Log.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * The Minz_Log class is used to log errors and warnings
  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 string $information message d'erreur / information à enregistrer
  17. * @param int $level niveau d'erreur https://php.net/function.syslog
  18. * @param string $file_name fichier de log
  19. * @throws Minz_PermissionDeniedException
  20. */
  21. public static function record(string $information, int $level, ?string $file_name = null): void {
  22. $env = getenv('FRESHRSS_ENV');
  23. if ($env == '') {
  24. try {
  25. $conf = Minz_Configuration::get('system');
  26. $env = $conf->environment;
  27. } catch (Minz_ConfigurationException $e) {
  28. $env = 'production';
  29. }
  30. }
  31. if (! ($env === 'silent' || ($env === 'production' && ($level >= LOG_NOTICE)))) {
  32. $username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
  33. if ($file_name == null) {
  34. $file_name = join_path(USERS_PATH, $username, LOG_FILENAME);
  35. }
  36. switch ($level) {
  37. case LOG_ERR :
  38. $level_label = 'error';
  39. break;
  40. case LOG_WARNING :
  41. $level_label = 'warning';
  42. break;
  43. case LOG_NOTICE :
  44. $level_label = 'notice';
  45. break;
  46. case LOG_DEBUG :
  47. $level_label = 'debug';
  48. break;
  49. default :
  50. $level = LOG_INFO;
  51. $level_label = 'info';
  52. }
  53. $log = '[' . date('r') . '] [' . $level_label . '] --- ' . $information . "\n";
  54. if (defined('COPY_LOG_TO_SYSLOG') && COPY_LOG_TO_SYSLOG) {
  55. syslog($level, '[' . $username . '] ' . trim($log));
  56. }
  57. self::ensureMaxLogSize($file_name);
  58. if (file_put_contents($file_name, $log, FILE_APPEND | LOCK_EX) === false) {
  59. throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
  60. }
  61. }
  62. }
  63. /**
  64. * Make sure we do not waste a huge amount of disk space with old log messages.
  65. *
  66. * This method can be called multiple times for one script execution, but its result will not change unless
  67. * you call clearstatcache() in between. We won't due do that for performance reasons.
  68. *
  69. * @param string $file_name
  70. * @throws Minz_PermissionDeniedException
  71. */
  72. protected static function ensureMaxLogSize(string $file_name): void {
  73. $maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 1048576;
  74. if ($maxSize > 0 && @filesize($file_name) > $maxSize) {
  75. $fp = fopen($file_name, 'c+');
  76. if ($fp && flock($fp, LOCK_EX)) {
  77. fseek($fp, -intval($maxSize / 2), SEEK_END);
  78. $content = fread($fp, $maxSize);
  79. rewind($fp);
  80. ftruncate($fp, 0);
  81. fwrite($fp, $content ? $content : '');
  82. fwrite($fp, sprintf("[%s] [notice] --- Log rotate.\n", date('r')));
  83. fflush($fp);
  84. flock($fp, LOCK_UN);
  85. } else {
  86. throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
  87. }
  88. fclose($fp);
  89. }
  90. }
  91. /**
  92. * Some helpers to Minz_Log::record() method
  93. * Parameters are the same of those of the record() method.
  94. */
  95. public static function debug(string $msg, ?string $file_name = null): void {
  96. self::record($msg, LOG_DEBUG, $file_name);
  97. }
  98. public static function notice(string $msg, ?string $file_name = null): void {
  99. self::record($msg, LOG_NOTICE, $file_name);
  100. }
  101. public static function warning(string $msg, ?string $file_name = null): void {
  102. self::record($msg, LOG_WARNING, $file_name);
  103. }
  104. public static function error(string $msg, ?string $file_name = null): void {
  105. self::record($msg, LOG_ERR, $file_name);
  106. }
  107. }