Action_Logger.php
1 week ago
Admin.php
1 week ago
Canonical_Formatter.php
1 week ago
File_Logger.php
1 week ago
Logger.php
1 week ago
Monolog_Logger.php
1 week ago
Null_Logger.php
1 week ago
Service_Provider.php
1 week ago
Canonical_Formatter.php
175 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ${CARET} |
| 4 | * |
| 5 | * @since 4.9.16 |
| 6 | * |
| 7 | * @package Tribe\Log |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | namespace Tribe\Log; |
| 12 | |
| 13 | |
| 14 | use Monolog\Formatter\FormatterInterface; |
| 15 | use Monolog\Formatter\LineFormatter; |
| 16 | |
| 17 | class Canonical_Formatter implements FormatterInterface { |
| 18 | /** |
| 19 | * @since 4.12.13 |
| 20 | * |
| 21 | * @var string Our standard format for the Monolog LineFormatter. |
| 22 | */ |
| 23 | protected $standard_format = 'tribe.%channel%.%level_name%: %message%'; |
| 24 | |
| 25 | /** |
| 26 | * @since 4.12.13 |
| 27 | * |
| 28 | * @var string Our standard format Monolog LineFormatter. |
| 29 | */ |
| 30 | protected $standard_formatter; |
| 31 | |
| 32 | /** |
| 33 | * @since 4.12.13 |
| 34 | * |
| 35 | * @var string Our context-aware format for the Monolog LineFormatter. |
| 36 | */ |
| 37 | protected $context_format = 'tribe-canonical-line channel=%channel% %message%'; |
| 38 | |
| 39 | /** |
| 40 | * @since 4.12.13 |
| 41 | * |
| 42 | * @var string Our context-aware Monolog LineFormatter. |
| 43 | */ |
| 44 | protected $context_formatter; |
| 45 | |
| 46 | /** |
| 47 | * Formats a log record. |
| 48 | * |
| 49 | * @since 4.9.16 |
| 50 | * |
| 51 | * @param array $record A record to format. |
| 52 | * |
| 53 | * @return mixed The formatted record. |
| 54 | */ |
| 55 | public function format( array $record ) { |
| 56 | $has_context = ! empty( $record['context'] ); |
| 57 | |
| 58 | if ( $has_context ) { |
| 59 | $record['message'] = $this->format_record_message( $record ); |
| 60 | $formatter = $this->get_context_formatter(); |
| 61 | } else { |
| 62 | // Fall-back on a standard format if the message does not have a context. |
| 63 | $formatter = $this->get_standard_formatter(); |
| 64 | } |
| 65 | |
| 66 | return $formatter->format( $record ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets a LineFormatter whose format is context aware. |
| 71 | * |
| 72 | * @since 4.12.13 |
| 73 | * |
| 74 | * @return LineFormatter |
| 75 | */ |
| 76 | public function get_context_formatter() { |
| 77 | if ( empty( $this->context_formatter ) ) { |
| 78 | $this->context_formatter = new LineFormatter( $this->context_format ); |
| 79 | } |
| 80 | |
| 81 | return $this->context_formatter; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets a LineFormatter whose format is our standard logging format. |
| 86 | * |
| 87 | * @since 4.12.13 |
| 88 | * |
| 89 | * @return LineFormatter |
| 90 | */ |
| 91 | public function get_standard_formatter() { |
| 92 | if ( empty( $this->standard_formatter ) ) { |
| 93 | $this->standard_formatter = new LineFormatter( $this->standard_format ); |
| 94 | } |
| 95 | |
| 96 | return $this->standard_formatter; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Formats a set of log records. |
| 101 | * |
| 102 | * This simply hands off the work of formatting Batches to the LineFormatter. |
| 103 | * |
| 104 | * @since 4.12.13 |
| 105 | * |
| 106 | * @param array $records A set of records to format |
| 107 | * @return mixed The formatted set of records |
| 108 | */ |
| 109 | public function formatBatch( array $records ) { |
| 110 | $line_formatter = new LineFormatter(); |
| 111 | |
| 112 | return $line_formatter->formatBatch( $records ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Formats the record to the canonical format. |
| 117 | * |
| 118 | * @since 4.9.16 |
| 119 | * |
| 120 | * @param array $record The record to process. |
| 121 | * |
| 122 | * @return string The formatted message, as built from the record context and message, in the format `<key>=<value>`. |
| 123 | */ |
| 124 | protected function format_record_message( array $record ) { |
| 125 | $message = []; |
| 126 | $extra = []; |
| 127 | |
| 128 | $extra['level'] = isset( $record['level_name'] ) ? strtolower( $record['level_name'] ) : 'debug'; |
| 129 | |
| 130 | if ( ! empty( $record['message'] ) ) { |
| 131 | // Use the message as the source. |
| 132 | $extra['source'] = $this->escape_quotes( $record['message'] ); |
| 133 | } |
| 134 | |
| 135 | $context = $record['context']; |
| 136 | $context = array_merge( $extra, $context ); |
| 137 | |
| 138 | foreach ( $context as $key => $value ) { |
| 139 | $escape = false; |
| 140 | |
| 141 | if ( is_bool( $value ) ) { |
| 142 | $value = $value ? 'true' : 'false'; |
| 143 | } elseif ( ! is_scalar( $value ) ) { |
| 144 | $value = json_encode( $value ); |
| 145 | if ( false === $value ) { |
| 146 | $value = 'malformed'; |
| 147 | } else { |
| 148 | $escape = true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if ( $escape || ( is_string( $value ) && preg_match( '~[\\\\/\\s]+~', $value ) ) ) { |
| 153 | $value = '"' . $this->escape_quotes( $value ) . '"'; |
| 154 | } |
| 155 | |
| 156 | $message[] = "{$key}={$value}"; |
| 157 | } |
| 158 | |
| 159 | return implode( ' ', $message ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Escapes the double quotes in a string. |
| 164 | * |
| 165 | * @since 4.9.16 |
| 166 | * |
| 167 | * @param string $string The string to escape the quotes in. |
| 168 | * |
| 169 | * @return string The string, with the quotes escaped. |
| 170 | */ |
| 171 | protected function escape_quotes( $string ) { |
| 172 | return str_replace( '"', '\\"', $string ) ; |
| 173 | } |
| 174 | } |
| 175 |