| 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 Monolog\Handler\SyslogUdpHandler; |
| 24 | use Monolog\Logger; |
| 25 | |
| 26 | /** |
| 27 | * Write logs to syslog with the channel appended to the application name. |
| 28 | * |
| 29 | * This use case for this handler is to emulate Wikimedia Foundation's |
| 30 | * udp2log system by leveraging syslog (and e.g. Rsyslog/Kafka) and |
| 31 | * allow an unstructured string to pass through mostly as-is, with the |
| 32 | * exception of the channel name, which is encoded in transit as part |
| 33 | * of the syslog "application name". It is intended that the syslog |
| 34 | * consumer "wildcard" subscribes to all messages with the app prefix, |
| 35 | * and then * strips it off at some point before writing the messages |
| 36 | * to a log file named after the channel. |
| 37 | * |
| 38 | * Transition plan (2016): |
| 39 | * - https://phabricator.wikimedia.org/T205856#4957430 |
| 40 | * - https://phabricator.wikimedia.org/T126989 |
| 41 | * |
| 42 | * @unstable |
| 43 | * @since 1.32 |
| 44 | * @ingroup Debug |
| 45 | * @copyright © 2019 Wikimedia Foundation and contributors |
| 46 | */ |
| 47 | class MwlogHandler extends SyslogUdpHandler { |
| 48 | |
| 49 | /** |
| 50 | * @var string |
| 51 | */ |
| 52 | private $appprefix; |
| 53 | |
| 54 | /** |
| 55 | * @var string |
| 56 | */ |
| 57 | private $hostname; |
| 58 | |
| 59 | /** |
| 60 | * @param string $appprefix Application prefix to use, channel will be appended. |
| 61 | * @param string $host Syslog host |
| 62 | * @param int $port Syslog port |
| 63 | * @param int $facility Syslog message facility |
| 64 | * @param int $level The minimum logging level at which this handler |
| 65 | * will be triggered |
| 66 | * @param bool $bubble Whether the messages that are handled can bubble up |
| 67 | * the stack or not |
| 68 | */ |
| 69 | public function __construct( |
| 70 | $appprefix, |
| 71 | $host, |
| 72 | $port = 514, |
| 73 | $facility = LOG_USER, |
| 74 | $level = Logger::DEBUG, |
| 75 | $bubble = true |
| 76 | ) { |
| 77 | parent::__construct( $host, $port, $facility, $level, $bubble ); |
| 78 | $this->appprefix = $appprefix; |
| 79 | $this->hostname = php_uname( 'n' ); |
| 80 | } |
| 81 | |
| 82 | protected function syslogHeader( $severity, $app ) { |
| 83 | $pri = $severity + $this->facility; |
| 84 | |
| 85 | // Goofy date format courtesy of RFC 3164 :( |
| 86 | // RFC 3164 actually specifies that the day of month should be space |
| 87 | // padded rather than unpadded but this seems to work with rsyslog and |
| 88 | // Logstash. |
| 89 | $timestamp = date( 'M j H:i:s' ); |
| 90 | |
| 91 | return "<{$pri}>{$timestamp} {$this->hostname} {$app}: "; |
| 92 | } |
| 93 | |
| 94 | private function splitMessageIntoLines( $message ): array { |
| 95 | if ( is_array( $message ) ) { |
| 96 | $message = implode( "\n", $message ); |
| 97 | } |
| 98 | |
| 99 | return preg_split( '/$\R?^/m', (string)$message, -1, PREG_SPLIT_NO_EMPTY ); |
| 100 | } |
| 101 | |
| 102 | protected function write( array $record ): void { |
| 103 | $lines = $this->splitMessageIntoLines( $record['formatted'] ); |
| 104 | $header = $this->syslogHeader( |
| 105 | $this->logLevels[$record['level']], |
| 106 | $this->appprefix . $record['channel'] ); |
| 107 | |
| 108 | foreach ( $lines as $line ) { |
| 109 | $this->socket->write( $line, $header ); |
| 110 | } |
| 111 | } |
| 112 | } |