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 ) {
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 if ( $revId !== null ) {
101 return chr( self
::MAGIC
) . $this->encode_long( $revId ) . $this->io
->string();
103 // @todo: remove backward compat code and do not send messages without rev id.
104 return $this->io
->string();
108 * Format a set of records into a list of binary strings
109 * conforming to the configured schema.
111 * @param array $records
114 public function formatBatch( array $records ) {
116 foreach ( $records as $record ) {
117 $message = $this->format( $record );
118 if ( $message !== null ) {
119 $result[] = $message;
126 * Get the writer for the named channel
128 * @var string $channel Name of the schema to fetch
129 * @return \AvroSchema|null
131 protected function getSchema( $channel ) {
132 if ( !isset( $this->schemas
[$channel] ) ) {
135 $schemaDetails = &$this->schemas
[$channel];
137 if ( isset( $schemaDetails['revision'] ) && isset( $schemaDetails['schema'] ) ) {
138 $schema = &$schemaDetails['schema'];
140 // @todo: Remove backward compat code
141 $schema = &$schemaDetails;
144 if ( !$schema instanceof AvroSchema
) {
145 if ( is_string( $schema ) ) {
146 $schema = AvroSchema
::parse( $schema );
148 $schema = AvroSchema
::real_parse(
149 $this->schemas
[$channel],
151 new AvroNamedSchemata()
159 * Get the writer for the named channel
161 * @var string $channel Name of the schema
164 public function getSchemaRevisionId( $channel ) {
165 // @todo: remove backward compat code
166 if ( isset( $this->schemas
[$channel] )
167 && is_array( $this->schemas
[$channel] )
168 && isset( $this->schemas
[$channel]['revision'] ) ) {
169 return (int) $this->schemas
[$channel]['revision'];
175 * convert an integer to a 64bits big endian long (Java compatible)
176 * NOTE: certainly only compatible with PHP 64bits
178 * @return string the binary representation of $id
180 private function encode_long( $id ) {
181 $high = ( $id & 0xffffffff00000000 ) >> 32;
182 $low = $id & 0x00000000ffffffff;
183 return pack( 'NN', $high, $low );