MediaWiki  master
LogstashFormatter.php
Go to the documentation of this file.
1 <?php
2 
4 
15 class LogstashFormatter extends \Monolog\Formatter\LogstashFormatter {
17  protected $reservedKeys = [
18  // from LogstashFormatter
19  'message', 'channel', 'level', 'type',
20  // from WebProcessor
21  'url', 'ip', 'http_method', 'server', 'referrer',
22  // from WikiProcessor
23  'host', 'wiki', 'reqId', 'mwversion',
24  // from config magic
25  'normalized_message',
26  ];
27 
33  protected function formatV0( array $record ) {
34  if ( $this->contextPrefix ) {
35  return parent::formatV0( $record );
36  }
37 
38  $context = !empty( $record['context'] ) ? $record['context'] : [];
39  $record['context'] = [];
40  $formatted = parent::formatV0( $record );
41 
42  $formatted['@fields'] = $this->fixKeyConflicts( $formatted['@fields'], $context );
43  return $formatted;
44  }
45 
51  protected function formatV1( array $record ) {
52  if ( $this->contextPrefix ) {
53  return parent::formatV1( $record );
54  }
55 
56  $context = !empty( $record['context'] ) ? $record['context'] : [];
57  $record['context'] = [];
58  $formatted = parent::formatV1( $record );
59 
60  $formatted = $this->fixKeyConflicts( $formatted, $context );
61  return $formatted;
62  }
63 
71  protected function fixKeyConflicts( array $fields, array $context ) {
72  foreach ( $context as $key => $val ) {
73  if (
74  in_array( $key, $this->reservedKeys, true ) &&
75  isset( $fields[$key] ) && $fields[$key] !== $val
76  ) {
77  $fields['logstash_formatter_key_conflict'][] = $key;
78  $key = 'c_' . $key;
79  }
80  $fields[$key] = $val;
81  }
82  return $fields;
83  }
84 
90  protected function normalizeException( $e ) {
91  if ( !$e instanceof \Exception && !$e instanceof \Throwable ) {
92  throw new \InvalidArgumentException( 'Exception/Throwable expected, got '
93  . gettype( $e ) . ' / ' . get_class( $e ) );
94  }
95 
96  $data = [
97  'class' => get_class( $e ),
98  'message' => $e->getMessage(),
99  'code' => $e->getCode(),
100  'file' => $e->getFile() . ':' . $e->getLine(),
102  ];
103 
104  $previous = $e->getPrevious();
105  if ( $previous ) {
106  $data['previous'] = $this->normalizeException( $previous );
107  }
108 
109  return $data;
110  }
111 }
normalizeException( $e)
Use a more user-friendly trace format than NormalizerFormatter.
array $reservedKeys
Keys which should not be used in log context.
formatV1(array $record)
Prevent key conflicts.
LogstashFormatter squashes the base message array and the context and extras subarrays into one...
static getRedactedTraceAsString( $e)
Generate a string representation of an exception&#39;s stack trace.
IContextSource $context
Definition: MediaWiki.php:37
formatV0(array $record)
Prevent key conflicts.
fixKeyConflicts(array $fields, array $context)
Check whether some context field would overwrite another message key.