選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

75 行
1.8KB

  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_INFO => LOG_INFO,
  34. Logger::LEVEL_WARNING => LOG_WARNING,
  35. Logger::LEVEL_ERROR => LOG_ERR,
  36. ];
  37. /**
  38. * Writes log messages to syslog
  39. */
  40. public function export()
  41. {
  42. openlog($this->identity, LOG_ODELAY | LOG_PID, $this->facility);
  43. foreach ($this->messages as $message) {
  44. syslog($this->_syslogLevels[$message[1]], $this->formatMessage($message));
  45. }
  46. closelog();
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function formatMessage($message)
  52. {
  53. list($text, $level, $category, $timestamp) = $message;
  54. $level = Logger::getLevelName($level);
  55. if (!is_string($text)) {
  56. // exceptions may not be serializable if in the call stack somewhere is a Closure
  57. if ($text instanceof \Throwable || $text instanceof \Exception) {
  58. $text = (string) $text;
  59. } else {
  60. $text = VarDumper::export($text);
  61. }
  62. }
  63. $prefix = $this->getMessagePrefix($message);
  64. return "{$prefix}[$level][$category] $text";
  65. }
  66. }