| 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 | |
| 21 | namespace MediaWiki\Logger\Monolog; |
| 22 | |
| 23 | use DateTimeInterface; |
| 24 | use Monolog\Handler\SyslogUdpHandler; |
| 25 | use Monolog\Logger; |
| 26 | |
| 27 | /** |
| 28 | * Write logs to a syslog server, using RFC 3164 formatted UDP packets. |
| 29 | * |
| 30 | * This builds on Monolog's SyslogUdpHandler, which creates only a partial |
| 31 | * RFC 5424 header (PRI and VERSION), with rest intending to come |
| 32 | * from a specifically configured LineFormatter. |
| 33 | * |
| 34 | * This makes use of SyslogUdpHandler it impossible with a formatter like |
| 35 | * \Monolog\Formatter\LogstashFormatter. Additionally, the direct syslog |
| 36 | * input for Logstash requires and accepts only RFC 3164 formatted packets. |
| 37 | * |
| 38 | * This is a complete syslog handler and should work with any formatter. The |
| 39 | * formatted message will be prepended with a complete RFC 3164 message |
| 40 | * header and a partial message body. The resulting packet looks like: |
| 41 | * |
| 42 | * <PRI>DATETIME HOSTNAME PROGRAM: MESSAGE |
| 43 | * |
| 44 | * This format works as input to rsyslog and can also be processed by the |
| 45 | * default Logstash syslog input handler. |
| 46 | * |
| 47 | * @since 1.25 |
| 48 | * @ingroup Debug |
| 49 | * @copyright © 2015 Wikimedia Foundation and contributors |
| 50 | */ |
| 51 | class SyslogHandler extends SyslogUdpHandler { |
| 52 | |
| 53 | private string $appname; |
| 54 | private string $hostname; |
| 55 | |
| 56 | /** |
| 57 | * @param string $appname Application name to report to syslog |
| 58 | * @param string $host Syslog host |
| 59 | * @param int $port Syslog port |
| 60 | * @param int $facility Syslog message facility |
| 61 | * @param int $level The minimum logging level at which this handler |
| 62 | * will be triggered |
| 63 | * @param bool $bubble Whether the messages that are handled can bubble up |
| 64 | * the stack or not |
| 65 | */ |
| 66 | public function __construct( |
| 67 | $appname, |
| 68 | $host, |
| 69 | $port = 514, |
| 70 | $facility = LOG_USER, |
| 71 | $level = Logger::DEBUG, |
| 72 | $bubble = true |
| 73 | ) { |
| 74 | parent::__construct( $host, $port, $facility, $level, $bubble ); |
| 75 | $this->appname = $appname; |
| 76 | $this->hostname = php_uname( 'n' ); |
| 77 | } |
| 78 | |
| 79 | protected function makeCommonSyslogHeader( int $severity, DateTimeInterface $datetime ): string { |
| 80 | $pri = $severity + $this->facility; |
| 81 | |
| 82 | // Goofy date format courtesy of RFC 3164 :( |
| 83 | // RFC 3164 actually specifies that the day of month should be space |
| 84 | // padded rather than unpadded but this seems to work with rsyslog and |
| 85 | // Logstash. |
| 86 | $timestamp = date( 'M j H:i:s' ); |
| 87 | |
| 88 | return "<{$pri}>{$timestamp} {$this->hostname} {$this->appname}: "; |
| 89 | } |
| 90 | } |