Syslog.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Syslog log writer.
  4. *
  5. * @package KO7
  6. * @category Logging
  7. * @author Jeremy Bush
  8. * @copyright (c) 2007-2016 Kohana Team
  9. * @copyright (c) since 2016 Koseven Team
  10. * @license https://koseven.dev/LICENSE
  11. */
  12. class KO7_Log_Syslog extends Log_Writer {
  13. /**
  14. * @var string The syslog identifier
  15. */
  16. protected $_ident;
  17. /**
  18. * Creates a new syslog logger.
  19. *
  20. * @link http://www.php.net/manual/function.openlog
  21. *
  22. * @param string $ident syslog identifier
  23. * @param int $facility facility to log to
  24. * @return void
  25. */
  26. public function __construct($ident = 'KO7PHP', $facility = LOG_USER)
  27. {
  28. $this->_ident = $ident;
  29. // Open the connection to syslog
  30. openlog($this->_ident, LOG_CONS, $facility);
  31. }
  32. /**
  33. * Writes each of the messages into the syslog.
  34. *
  35. * @param array $messages
  36. * @return void
  37. */
  38. public function write(array $messages)
  39. {
  40. foreach ($messages as $message)
  41. {
  42. syslog($message['level'], $message['body']);
  43. if (isset($message['additional']['exception']))
  44. {
  45. syslog(Log_Writer::$strace_level, $message['additional']['exception']->getTraceAsString());
  46. }
  47. }
  48. }
  49. /**
  50. * Closes the syslog connection
  51. *
  52. * @return void
  53. */
  54. public function __destruct()
  55. {
  56. // Close connection to syslog
  57. closelog();
  58. }
  59. }