Fix namespaced class references for Doxygen
[lhc/web/wiklou.git] / includes / debug / logger / monolog / AvroFormatter.php
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 AvroIODatumWriter;
24 use AvroIOBinaryEncoder;
25 use AvroIOTypeException;
26 use AvroNamedSchemata;
27 use AvroSchema;
28 use AvroStringIO;
29 use AvroValidator;
30 use Monolog\Formatter\FormatterInterface;
31
32 /**
33 * Log message formatter that uses the apache Avro format.
34 *
35 * @since 1.26
36 * @author Erik Bernhardson <ebernhardson@wikimedia.org>
37 * @copyright © 2015 Erik Bernhardson and Wikimedia Foundation.
38 */
39 class AvroFormatter implements FormatterInterface {
40 /**
41 * @var array Map from schema name to schema definition
42 */
43 protected $schemas;
44
45 /**
46 * @var AvroStringIO
47 */
48 protected $io;
49
50 /**
51 * @var AvroIOBinaryEncoder
52 */
53 protected $encoder;
54
55 /**
56 * @var AvroIODatumWriter
57 */
58 protected $writer;
59
60 /**
61 * @var array $schemas Map from Monolog channel to Avro schema.
62 * Each schema can be either the JSON string or decoded into PHP
63 * arrays.
64 */
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();
70 }
71
72 /**
73 * Formats the record context into a binary string per the
74 * schema configured for the records channel.
75 *
76 * @param array $record
77 * @return string|null The serialized record, or null if
78 * the record is not valid for the selected schema.
79 */
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" );
85 return null;
86 }
87 try {
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}" );
93 return null;
94 }
95 return $this->io->string();
96 }
97
98 /**
99 * Format a set of records into a list of binary strings
100 * conforming to the configured schema.
101 *
102 * @param array $records
103 * @return string[]
104 */
105 public function formatBatch( array $records ) {
106 $result = array();
107 foreach ( $records as $record ) {
108 $message = $this->format( $record );
109 if ( $message !== null ) {
110 $result[] = $message;
111 }
112 }
113 return $result;
114 }
115
116 /**
117 * Get the writer for the named channel
118 *
119 * @var string $channel Name of the schema to fetch
120 * @return \\AvroSchema|null
121 */
122 protected function getSchema( $channel ) {
123 if ( !isset( $this->schemas[$channel] ) ) {
124 return null;
125 }
126 if ( !$this->schemas[$channel] instanceof AvroSchema ) {
127 if ( is_string( $this->schemas[$channel] ) ) {
128 $this->schemas[$channel] = AvroSchema::parse( $this->schemas[$channel] );
129 } else {
130 $this->schemas[$channel] = AvroSchema::real_parse(
131 $this->schemas[$channel],
132 null,
133 new AvroNamedSchemata()
134 );
135 }
136 }
137 return $this->schemas[$channel];
138 }
139 }