21 'message',
'channel',
'level',
'type',
23 'url',
'ip',
'http_method',
'server',
'referrer',
25 'host',
'wiki',
'reqId',
'mwversion',
47 public function __construct(
string $applicationName, ?
string $systemName =
null,
48 string $extraKey =
'',
string $contextKey =
'ctxt_',
$version = self::V0
51 parent::__construct( $applicationName, $systemName, $extraKey, $contextKey );
54 public function format( array $record ): string {
55 $record = \Monolog\Formatter\NormalizerFormatter::
format( $record );
56 if ( $this->version === self::V1 ) {
57 $message = $this->formatv1( $record );
58 } elseif ( $this->version === self::V0 ) {
59 $message = $this->
formatV0( $record );
62 return $this->toJson( $message ) .
"\n";
71 if ( $this->contextKey !==
'' ) {
72 return $this->formatMonologV0( $record );
75 $context = !empty( $record[
'context'] ) ? $record[
'context'] : [];
76 $record[
'context'] = [];
77 $formatted = $this->formatMonologV0( $record );
79 $formatted[
'@fields'] = $this->fixKeyConflicts( $formatted[
'@fields'], $context );
92 if ( empty( $record[
'datetime'] ) ) {
93 $record[
'datetime'] = gmdate(
'c' );
96 '@timestamp' => $record[
'datetime'],
97 '@source' => $this->systemName,
100 if ( isset( $record[
'message'] ) ) {
101 $message[
'@message'] = $record[
'message'];
103 if ( isset( $record[
'channel'] ) ) {
104 $message[
'@tags'] = [ $record[
'channel'] ];
105 $message[
'@fields'][
'channel'] = $record[
'channel'];
107 if ( isset( $record[
'level'] ) ) {
108 $message[
'@fields'][
'level'] = $record[
'level'];
110 if ( $this->applicationName ) {
111 $message[
'@type'] = $this->applicationName;
113 if ( isset( $record[
'extra'][
'server'] ) ) {
114 $message[
'@source_host'] = $record[
'extra'][
'server'];
116 if ( isset( $record[
'extra'][
'url'] ) ) {
117 $message[
'@source_path'] = $record[
'extra'][
'url'];
119 if ( !empty( $record[
'extra'] ) ) {
120 foreach ( $record[
'extra'] as $key => $val ) {
121 $message[
'@fields'][$this->extraKey . $key] = $val;
124 if ( !empty( $record[
'context'] ) ) {
125 foreach ( $record[
'context'] as $key => $val ) {
126 $message[
'@fields'][$this->contextKey . $key] = $val;
139 if ( $this->contextKey ) {
140 return $this->formatMonologV1( $record );
143 $context = !empty( $record[
'context'] ) ? $record[
'context'] : [];
144 $record[
'context'] = [];
145 $formatted = $this->formatMonologV1( $record );
147 return $this->fixKeyConflicts( $formatted, $context );
158 if ( empty( $record[
'datetime'] ) ) {
159 $record[
'datetime'] = gmdate(
'c' );
162 '@timestamp' => $record[
'datetime'],
164 'host' => $this->systemName,
166 if ( isset( $record[
'message'] ) ) {
167 $message[
'message'] = $record[
'message'];
169 if ( isset( $record[
'channel'] ) ) {
170 $message[
'type'] = $record[
'channel'];
171 $message[
'channel'] = $record[
'channel'];
173 if ( isset( $record[
'level_name'] ) ) {
174 $message[
'level'] = $record[
'level_name'];
178 if ( isset( $record[
'level'] ) ) {
179 $message[
'monolog_level'] = $record[
'level'];
181 if ( $this->applicationName ) {
182 $message[
'type'] = $this->applicationName;
184 if ( !empty( $record[
'extra'] ) ) {
185 foreach ( $record[
'extra'] as $key => $val ) {
186 $message[$this->extraKey . $key] = $val;
189 if ( !empty( $record[
'context'] ) ) {
190 foreach ( $record[
'context'] as $key => $val ) {
191 $message[$this->contextKey . $key] = $val;
206 foreach ( $context as $key => $val ) {
208 in_array( $key, $this->reservedKeys,
true ) &&
209 isset( $fields[$key] ) && $fields[$key] !== $val
211 $fields[
'logstash_formatter_key_conflict'][] = $key;
214 $fields[$key] = $val;
227 'class' => get_class( $e ),
228 'message' => $e->getMessage(),
229 'code' => $e->getCode(),
230 'file' => $e->getFile() .
':' . $e->getLine(),
231 'trace' => \MWExceptionHandler::getRedactedTraceAsString( $e ),
234 $previous = $e->getPrevious();
236 $data[
'previous'] = $this->normalizeException( $previous );