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.
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.
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
21 use Monolog\Handler\SyslogUdpHandler
;
25 * Log handler that supports sending log events to a syslog server using RFC
26 * 3164 formatted UDP packets.
28 * Monolog's SyslogUdpHandler creates a partial RFC 5424 header (PRI and
29 * VERSION) and relies on the associated formatter to complete the header and
30 * message payload. This makes using it with a fixed format formatter like
31 * Monolog\Formatter\LogstashFormatter impossible. Additionally, the direct
32 * syslog input for Logstash only handles RFC 3164 syslog packets.
34 * This Handler should work with any Formatter. The formatted message will be
35 * prepended with an RFC 3164 message header and a partial message body. The
36 * resulting packet will looks something like:
38 * <PRI>DATETIME HOSTNAME PROGRAM: MESSAGE
40 * This format works as input to rsyslog and can also be processed by the
41 * default Logstash syslog input handler.
44 * @author Bryan Davis <bd808@wikimedia.org>
45 * @copyright © 2015 Bryan Davis and Wikimedia Foundation.
47 class MWLoggerMonologSyslogHandler
extends SyslogUdpHandler
{
50 * @var string $appname
55 * @var string $hostname
61 * @param string $appname Application name to report to syslog
62 * @param string $host Syslog host
63 * @param int $port Syslog port
64 * @param int $facility Syslog message facility
65 * @param string $level The minimum logging level at which this handler
67 * @param bool $bubble Whether the messages that are handled can bubble up
70 public function __construct(
75 $level = Logger
::DEBUG
,
78 parent
::__construct( $host, $port, $facility, $level, $bubble );
79 $this->appname
= $appname;
80 $this->hostname
= php_uname( 'n' );
83 protected function makeCommonSyslogHeader( $severity ) {
84 $pri = $severity +
$this->facility
;
86 // Goofy date format courtesy of RFC 3164 :(
87 // RFC 3164 actually specifies that the day of month should be space
88 // padded rather than unpadded but this seems to work with rsyslog and
90 $timestamp = date( 'M j H:i:s' );
92 return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: ";