SyslogHandler.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. namespace MediaWiki\Logger\Monolog;
  21. use Monolog\Handler\SyslogUdpHandler;
  22. use Monolog\Logger;
  23. /**
  24. * Log handler that supports sending log events to a syslog server using RFC
  25. * 3164 formatted UDP packets.
  26. *
  27. * Monolog's SyslogUdpHandler creates a partial RFC 5424 header (PRI and
  28. * VERSION) and relies on the associated formatter to complete the header and
  29. * message payload. This makes using it with a fixed format formatter like
  30. * \Monolog\Formatter\LogstashFormatter impossible. Additionally, the
  31. * direct syslog input for Logstash only handles RFC 3164 syslog packets.
  32. *
  33. * This Handler should work with any Formatter. The formatted message will be
  34. * prepended with an RFC 3164 message header and a partial message body. The
  35. * resulting packet will looks something like:
  36. *
  37. * <PRI>DATETIME HOSTNAME PROGRAM: MESSAGE
  38. *
  39. * This format works as input to rsyslog and can also be processed by the
  40. * default Logstash syslog input handler.
  41. *
  42. * @since 1.25
  43. * @copyright © 2015 Wikimedia Foundation and contributors
  44. */
  45. class SyslogHandler extends SyslogUdpHandler {
  46. /**
  47. * @var string $appname
  48. */
  49. private $appname;
  50. /**
  51. * @var string $hostname
  52. */
  53. private $hostname;
  54. /**
  55. * @param string $appname Application name to report to syslog
  56. * @param string $host Syslog host
  57. * @param int $port Syslog port
  58. * @param int $facility Syslog message facility
  59. * @param int $level The minimum logging level at which this handler
  60. * will be triggered
  61. * @param bool $bubble Whether the messages that are handled can bubble up
  62. * the stack or not
  63. */
  64. public function __construct(
  65. $appname,
  66. $host,
  67. $port = 514,
  68. $facility = LOG_USER,
  69. $level = Logger::DEBUG,
  70. $bubble = true
  71. ) {
  72. parent::__construct( $host, $port, $facility, $level, $bubble );
  73. $this->appname = $appname;
  74. $this->hostname = php_uname( 'n' );
  75. }
  76. protected function makeCommonSyslogHeader( $severity ) {
  77. $pri = $severity + $this->facility;
  78. // Goofy date format courtesy of RFC 3164 :(
  79. // RFC 3164 actually specifies that the day of month should be space
  80. // padded rather than unpadded but this seems to work with rsyslog and
  81. // Logstash.
  82. $timestamp = date( 'M j H:i:s' );
  83. return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: ";
  84. }
  85. }