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 AvroIODatumWriter
;
24 use AvroIOBinaryEncoder
;
25 use AvroIOTypeException
;
26 use AvroNamedSchemata
;
30 use Monolog\Formatter\FormatterInterface
;
33 * Log message formatter that uses the apache Avro format.
36 * @author Erik Bernhardson <ebernhardson@wikimedia.org>
37 * @copyright © 2015 Erik Bernhardson and Wikimedia Foundation.
39 class AvroFormatter
implements FormatterInterface
{
41 * @var Magic byte to encode schema revision id.
45 * @var array Map from schema name to schema definition
55 * @var AvroIOBinaryEncoder
60 * @var AvroIODatumWriter
65 * @var array $schemas Map from Monolog channel to Avro schema.
66 * Each schema can be either the JSON string or decoded into PHP
69 public function __construct( array $schemas ) {
70 $this->schemas
= $schemas;
71 $this->io
= new AvroStringIO( '' );
72 $this->encoder
= new AvroIOBinaryEncoder( $this->io
);
73 $this->writer
= new AvroIODatumWriter();
77 * Formats the record context into a binary string per the
78 * schema configured for the records channel.
80 * @param array $record
81 * @return string|null The serialized record, or null if
82 * the record is not valid for the selected schema.
84 public function format( array $record ) {
85 $this->io
->truncate();
86 $schema = $this->getSchema( $record['channel'] );
87 $revId = $this->getSchemaRevisionId( $record['channel'] );
88 if ( $schema === null ||
$revId === null ) {
89 trigger_error( "The schema for channel '{$record['channel']}' is not available" );
93 $this->writer
->write_data( $schema, $record['context'], $this->encoder
);
94 } catch ( AvroIOTypeException
$e ) {
95 $errors = AvroValidator
::getErrors( $schema, $record['context'] );
96 $json = json_encode( $errors );
97 trigger_error( "Avro failed to serialize record for {$record['channel']} : {$json}" );
100 return chr( self
::MAGIC
) . $this->encodeLong( $revId ) . $this->io
->string();
104 * Format a set of records into a list of binary strings
105 * conforming to the configured schema.
107 * @param array $records
110 public function formatBatch( array $records ) {
112 foreach ( $records as $record ) {
113 $message = $this->format( $record );
114 if ( $message !== null ) {
115 $result[] = $message;
122 * Get the writer for the named channel
124 * @var string $channel Name of the schema to fetch
125 * @return \AvroSchema|null
127 protected function getSchema( $channel ) {
128 if ( !isset( $this->schemas
[$channel] ) ) {
131 if ( !isset( $this->schemas
[$channel]['revision'], $this->schemas
[$channel]['schema'] ) ) {
135 if ( !$this->schemas
[$channel]['schema'] instanceof AvroSchema
) {
136 $schema = $this->schemas
[$channel]['schema'];
137 if ( is_string( $schema ) ) {
138 $this->schemas
[$channel]['schema'] = AvroSchema
::parse( $schema );
140 $this->schemas
[$channel]['schema'] = AvroSchema
::real_parse(
143 new AvroNamedSchemata()
147 return $this->schemas
[$channel]['schema'];
151 * Get the writer for the named channel
153 * @var string $channel Name of the schema
156 public function getSchemaRevisionId( $channel ) {
157 if ( isset( $this->schemas
[$channel]['revision'] ) ) {
158 return (int)$this->schemas
[$channel]['revision'];
164 * convert an integer to a 64bits big endian long (Java compatible)
165 * NOTE: certainly only compatible with PHP 64bits
167 * @return string the binary representation of $id
169 private function encodeLong( $id ) {
170 $high = ( $id & 0xffffffff00000000 ) >> 32;
171 $low = $id & 0x00000000ffffffff;
172 return pack( 'NN', $high, $low );