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 array Map from schema name to schema definition
51 * @var AvroIOBinaryEncoder
56 * @var AvroIODatumWriter
61 * @var array $schemas Map from Monolog channel to Avro schema.
62 * Each schema can be either the JSON string or decoded into PHP
65 public function __construct( array $schemas ) {
66 $this->schemas
= $schemas;
67 $this->io
= new AvroStringIO( '' );
68 $this->encoder
= new AvroIOBinaryEncoder( $this->io
);
69 $this->writer
= new AvroIODatumWriter();
73 * Formats the record context into a binary string per the
74 * schema configured for the records channel.
76 * @param array $record
77 * @return string|null The serialized record, or null if
78 * the record is not valid for the selected schema.
80 public function format( array $record ) {
81 $this->io
->truncate();
82 $schema = $this->getSchema( $record['channel'] );
83 if ( $schema === null ) {
84 trigger_error( "The schema for channel '{$record['channel']}' is not available" );
88 $this->writer
->write_data( $schema, $record['context'], $this->encoder
);
89 } catch ( AvroIOTypeException
$e ) {
90 $errors = AvroValidator
::getErrors( $schema, $record['context'] );
91 $json = json_encode( $errors );
92 trigger_error( "Avro failed to serialize record for {$record['channel']} : {$json}" );
95 return $this->io
->string();
99 * Format a set of records into a list of binary strings
100 * conforming to the configured schema.
102 * @param array $records
105 public function formatBatch( array $records ) {
107 foreach ( $records as $record ) {
108 $message = $this->format( $record );
109 if ( $message !== null ) {
110 $result[] = $message;
117 * Get the writer for the named channel
119 * @var string $channel Name of the schema to fetch
120 * @return \\AvroSchema|null
122 protected function getSchema( $channel ) {
123 if ( !isset( $this->schemas
[$channel] ) ) {
126 if ( !$this->schemas
[$channel] instanceof AvroSchema
) {
127 if ( is_string( $this->schemas
[$channel] ) ) {
128 $this->schemas
[$channel] = AvroSchema
::parse( $this->schemas
[$channel] );
130 $this->schemas
[$channel] = AvroSchema
::real_parse(
131 $this->schemas
[$channel],
133 new AvroNamedSchemata()
137 return $this->schemas
[$channel];