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 namespace MediaWiki\Logger\Monolog
;
23 use Monolog\Handler\SyslogUdpHandler
;
27 * Write logs to syslog with the channel appended to the application name.
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.
38 * Transition plan (2016):
39 * - https://phabricator.wikimedia.org/T205856#4957430
40 * - https://phabricator.wikimedia.org/T126989
45 * @copyright © 2019 Wikimedia Foundation and contributors
47 class MwlogHandler
extends SyslogUdpHandler
{
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
66 * @param bool $bubble Whether the messages that are handled can bubble up
69 public function __construct(
74 $level = Logger
::DEBUG
,
77 parent
::__construct( $host, $port, $facility, $level, $bubble );
78 $this->appprefix
= $appprefix;
79 $this->hostname
= php_uname( 'n' );
82 protected function syslogHeader( $severity, $app ) {
83 $pri = $severity +
$this->facility
;
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
89 $timestamp = date( 'M j H:i:s' );
91 return "<{$pri}>{$timestamp} {$this->hostname} {$app}: ";
94 private function splitMessageIntoLines( $message ): array {
95 if ( is_array( $message ) ) {
96 $message = implode( "\n", $message );
99 return preg_split( '/$\R?^/m', (string)$message, -1, PREG_SPLIT_NO_EMPTY
);
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'] );
108 foreach ( $lines as $line ) {
109 $this->socket
->write( $line, $header );