Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SyslogTarget.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\log;
  8. use Yii;
  9. use yii\helpers\VarDumper;
  10. /**
  11. * SyslogTarget writes log to syslog.
  12. *
  13. * @author miramir <gmiramir@gmail.com>
  14. * @since 2.0
  15. */
  16. class SyslogTarget extends Target
  17. {
  18. /**
  19. * @var string syslog identity
  20. */
  21. public $identity;
  22. /**
  23. * @var integer syslog facility.
  24. */
  25. public $facility = LOG_USER;
  26. /**
  27. * @var array syslog levels
  28. */
  29. private $_syslogLevels = [
  30. Logger::LEVEL_TRACE => LOG_DEBUG,
  31. Logger::LEVEL_PROFILE_BEGIN => LOG_DEBUG,
  32. Logger::LEVEL_PROFILE_END => LOG_DEBUG,
  33. Logger::LEVEL_PROFILE => LOG_DEBUG,
  34. Logger::LEVEL_INFO => LOG_INFO,
  35. Logger::LEVEL_WARNING => LOG_WARNING,
  36. Logger::LEVEL_ERROR => LOG_ERR,
  37. ];
  38. /**
  39. * Writes log messages to syslog
  40. */
  41. public function export()
  42. {
  43. openlog($this->identity, LOG_ODELAY | LOG_PID, $this->facility);
  44. foreach ($this->messages as $message) {
  45. syslog($this->_syslogLevels[$message[1]], $this->formatMessage($message));
  46. }
  47. closelog();
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function formatMessage($message)
  53. {
  54. list($text, $level, $category, $timestamp) = $message;
  55. $level = Logger::getLevelName($level);
  56. if (!is_string($text)) {
  57. // exceptions may not be serializable if in the call stack somewhere is a Closure
  58. if ($text instanceof \Throwable || $text instanceof \Exception) {
  59. $text = (string) $text;
  60. } else {
  61. $text = VarDumper::export($text);
  62. }
  63. }
  64. $prefix = $this->getMessagePrefix($message);
  65. return "{$prefix}[$level][$category] $text";
  66. }
  67. }