mod_syslog_syslog.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
  3. /**
  4. * Class to manage logging to syslog
  5. */
  6. class mod_syslog_syslog extends LogHandler implements LogHandlerInterface
  7. {
  8. public $code = 'syslog';
  9. /**
  10. * Return name of logger
  11. *
  12. * @return string Name of logger
  13. */
  14. public function getName()
  15. {
  16. return 'Syslogd';
  17. }
  18. /**
  19. * Version of the module ('x.y.z' or 'dolibarr' or 'experimental' or 'development')
  20. *
  21. * @return string
  22. */
  23. public function getVersion()
  24. {
  25. return 'dolibarr';
  26. }
  27. /**
  28. * Content of the info tooltip.
  29. *
  30. * @return false|string
  31. */
  32. public function getInfo()
  33. {
  34. global $langs;
  35. return $langs->trans('OnlyWindowsLOG_USER');
  36. }
  37. /**
  38. * Is the module active ?
  39. *
  40. * @return int
  41. */
  42. public function isActive()
  43. {
  44. global $conf;
  45. // This function does not exists on some ISP (Ex: Free in France)
  46. if (!function_exists('openlog')) {
  47. return 0;
  48. }
  49. return empty($conf->global->SYSLOG_DISABLE_LOGHANDLER_SYSLOG) ? 1 : 0; // Set SYSLOG_DISABLE_LOGHANDLER_SYSLOG to 1 to disable this loghandler
  50. }
  51. /**
  52. * Return array of configuration data
  53. *
  54. * @return array Return array of configuration data
  55. */
  56. public function configure()
  57. {
  58. global $langs;
  59. return array(
  60. array(
  61. 'constant' => 'SYSLOG_FACILITY',
  62. 'name' => $langs->trans('SyslogFacility'),
  63. 'default' => 'LOG_USER'
  64. )
  65. );
  66. }
  67. /**
  68. * Return if configuration is valid
  69. *
  70. * @return array Array of errors. Empty array if ok.
  71. */
  72. public function checkConfiguration()
  73. {
  74. global $conf, $langs;
  75. $errors = array();
  76. $facility = constant($conf->global->SYSLOG_FACILITY);
  77. if ($facility) {
  78. // Only LOG_USER supported on Windows
  79. if (!empty($_SERVER["WINDIR"])) {
  80. $facility = constant('LOG_USER');
  81. }
  82. dol_syslog("admin/syslog: facility ".$facility);
  83. } else {
  84. $errors[] = $langs->trans("ErrorUnknownSyslogConstant", $facility);
  85. }
  86. return $errors;
  87. }
  88. /**
  89. * Export the message
  90. *
  91. * @param array $content Array containing the info about the message
  92. * @return void
  93. */
  94. public function export($content)
  95. {
  96. global $conf;
  97. if (!empty($conf->global->MAIN_SYSLOG_DISABLE_SYSLOG)) {
  98. return; // Global option to disable output of this handler
  99. }
  100. if (!empty($conf->global->SYSLOG_FACILITY)) { // Example LOG_USER
  101. $facility = constant($conf->global->SYSLOG_FACILITY);
  102. } else {
  103. $facility = constant('LOG_USER');
  104. }
  105. // (int) is required to avoid error parameter 3 expected to be long
  106. openlog('dolibarr', LOG_PID | LOG_PERROR, (int) $facility);
  107. syslog($content['level'], $content['message']);
  108. closelog();
  109. }
  110. }