mod_syslog_file.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
  3. /**
  4. * Class to manage logging to a file
  5. */
  6. class mod_syslog_file extends LogHandler implements LogHandlerInterface
  7. {
  8. public $code = 'file';
  9. public $lastTime = 0;
  10. /**
  11. * Return name of logger
  12. *
  13. * @return string Name of logger
  14. */
  15. public function getName()
  16. {
  17. global $langs;
  18. return $langs->trans('File');
  19. }
  20. /**
  21. * Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
  22. *
  23. * @return string
  24. */
  25. public function getVersion()
  26. {
  27. return 'dolibarr';
  28. }
  29. /**
  30. * Content of the info tooltip.
  31. *
  32. * @return false|string
  33. */
  34. public function getInfo()
  35. {
  36. global $langs;
  37. return $langs->trans('YouCanUseDOL_DATA_ROOT');
  38. }
  39. /**
  40. * Is the module active ?
  41. *
  42. * @return int
  43. */
  44. public function isActive()
  45. {
  46. global $conf;
  47. return empty($conf->global->SYSLOG_DISABLE_LOGHANDLER_FILE) ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_FILE to 1 to disable this loghandler
  48. }
  49. /**
  50. * Return array of configuration data
  51. *
  52. * @return array Return array of configuration data
  53. */
  54. public function configure()
  55. {
  56. global $langs;
  57. return array(
  58. array(
  59. 'name' => $langs->trans('SyslogFilename'),
  60. 'constant' => 'SYSLOG_FILE',
  61. 'default' => 'DOL_DATA_ROOT/dolibarr.log',
  62. 'attr' => 'size="60"'
  63. )
  64. );
  65. }
  66. /**
  67. * Return if configuration is valid
  68. *
  69. * @return array Array of errors. Empty array if ok.
  70. */
  71. public function checkConfiguration()
  72. {
  73. global $langs;
  74. $errors = array();
  75. $filename = $this->getFilename();
  76. if (file_exists($filename) && is_writable($filename)) {
  77. dol_syslog('admin/syslog: file '.$filename);
  78. } else {
  79. $errors[] = $langs->trans("ErrorFailedToOpenFile", $filename);
  80. }
  81. return $errors;
  82. }
  83. /**
  84. * Return the parsed logfile path
  85. *
  86. * @param string $suffixinfilename When output is a file, append this suffix into default log filename.
  87. * @return string
  88. */
  89. private function getFilename($suffixinfilename = '')
  90. {
  91. global $conf;
  92. if (empty($conf->global->SYSLOG_FILE)) {
  93. $tmp = DOL_DATA_ROOT.'/dolibarr.log';
  94. } else {
  95. $tmp = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
  96. }
  97. if (!empty($conf->global->SYSLOG_FILE_ONEPERSESSION)) {
  98. if ($conf->global->SYSLOG_FILE_ONEPERSESSION == 1) { // file depend on session key name (Note that session name is same for all users and is not a per user value)
  99. $suffixinfilename .= '_'.session_name();
  100. }
  101. if ($conf->global->SYSLOG_FILE_ONEPERSESSION == 2) { // file depend on session value sor per user
  102. $suffixinfilename .= '_'.session_name().'_'.$_SERVER["REMOTE_ADDR"];
  103. }
  104. }
  105. return $suffixinfilename ?preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
  106. }
  107. /**
  108. * Export the message
  109. *
  110. * @param array $content Array containing the info about the message
  111. * @param string $suffixinfilename When output is a file, append this suffix into default log filename.
  112. * @return void
  113. */
  114. public function export($content, $suffixinfilename = '')
  115. {
  116. global $conf, $dolibarr_main_prod;
  117. if (!empty($conf->global->MAIN_SYSLOG_DISABLE_FILE)) {
  118. return; // Global option to disable output of this handler
  119. }
  120. $logfile = $this->getFilename($suffixinfilename);
  121. // Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
  122. if (defined('SYSLOG_FILE_NO_ERROR')) {
  123. $filefd = @fopen($logfile, 'a+');
  124. } else {
  125. $filefd = fopen($logfile, 'a+');
  126. }
  127. if (!$filefd) {
  128. if (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR')) {
  129. // Do not break dolibarr usage if log fails
  130. //throw new Exception('Failed to open log file '.basename($logfile));
  131. print 'Failed to open log file '.($dolibarr_main_prod ?basename($logfile) : $logfile);
  132. }
  133. } else {
  134. $logLevels = array(
  135. LOG_EMERG => 'EMERG',
  136. LOG_ALERT => 'ALERT',
  137. LOG_CRIT => 'CRIT',
  138. LOG_ERR => 'ERR',
  139. LOG_WARNING => 'WARNING',
  140. LOG_NOTICE => 'NOTICE',
  141. LOG_INFO => 'INFO',
  142. LOG_DEBUG => 'DEBUG'
  143. );
  144. $delay = "";
  145. if (!empty($conf->global->MAIN_SYSLOG_SHOW_DELAY)) {
  146. $now = microtime(true);
  147. $delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
  148. $this->lastTime = $now;
  149. }
  150. $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".($this->ident > 0 ?str_pad('', $this->ident, ' ') : '').$content['message'];
  151. fwrite($filefd, $message."\n");
  152. fclose($filefd);
  153. @chmod($logfile, octdec(empty($conf->global->MAIN_UMASK) ? '0664' : $conf->global->MAIN_UMASK));
  154. }
  155. }
  156. }