Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TaoJsonLogFormatter | |
0.00% |
0 / 21 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
| format | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| formatBatch | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getOutputRecord | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2018 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\log\logger\formatter; |
| 23 | |
| 24 | use Monolog\Formatter\FormatterInterface; |
| 25 | use oat\oatbox\log\logger\processor\BacktraceProcessor; |
| 26 | use oat\oatbox\log\logger\processor\EnvironmentProcessorAbstract; |
| 27 | |
| 28 | /** |
| 29 | * TAO Json style log formatter. |
| 30 | * |
| 31 | * @package oat\oatbox\log\logger\formatter |
| 32 | */ |
| 33 | class TaoJsonLogFormatter implements FormatterInterface |
| 34 | { |
| 35 | /** |
| 36 | * Used datetime format. |
| 37 | */ |
| 38 | public const DATETIME_FORMAT = 'd/m/Y:H:i:s O'; |
| 39 | |
| 40 | /** |
| 41 | * Datetime offset in the generated output. |
| 42 | */ |
| 43 | public const DATETIME_OFFSET = 'datetime'; |
| 44 | |
| 45 | /** |
| 46 | * Stack offset in the generated output. |
| 47 | */ |
| 48 | public const STACK_OFFSET = 'stack'; |
| 49 | |
| 50 | /** |
| 51 | * Severity offset in the generated output. |
| 52 | */ |
| 53 | public const SEVERITY_OFFSET = 'severity'; |
| 54 | |
| 55 | /** |
| 56 | * Line offset in the generated output. |
| 57 | */ |
| 58 | public const LINE_OFFSET = 'line'; |
| 59 | |
| 60 | /** |
| 61 | * File offset in the generated output. |
| 62 | */ |
| 63 | public const FILE_OFFSET = 'file'; |
| 64 | |
| 65 | /** |
| 66 | * Content/message offset in the generated output. |
| 67 | */ |
| 68 | public const CONTENT_OFFSET = 'content'; |
| 69 | |
| 70 | /** |
| 71 | * Backtrace offset in the generated output. |
| 72 | */ |
| 73 | public const TRACE_OFFSET = 'trace'; |
| 74 | |
| 75 | /** |
| 76 | * @inheritdoc |
| 77 | * |
| 78 | * @throws \RuntimeException |
| 79 | */ |
| 80 | public function format(array $record) |
| 81 | { |
| 82 | $jsonString = json_encode($this->getOutputRecord($record)); |
| 83 | |
| 84 | if ($jsonString === false) { |
| 85 | throw new \RuntimeException('Error happened during the log format process! (json encode error)'); |
| 86 | } |
| 87 | |
| 88 | return $jsonString . PHP_EOL; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @inheritdoc |
| 93 | * |
| 94 | * @throws \ErrorException |
| 95 | */ |
| 96 | public function formatBatch(array $records) |
| 97 | { |
| 98 | foreach ($records as $key => $record) { |
| 99 | $records[$key] = $this->format($record); |
| 100 | } |
| 101 | |
| 102 | return $records; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the customized record. |
| 107 | * |
| 108 | * @param array $record |
| 109 | * |
| 110 | * @return array |
| 111 | */ |
| 112 | protected function getOutputRecord(array $record) |
| 113 | { |
| 114 | // Adds the basic log details. |
| 115 | $output = [ |
| 116 | static::DATETIME_OFFSET => $record['datetime']->format(static::DATETIME_FORMAT), |
| 117 | static::SEVERITY_OFFSET => $record['level_name'], |
| 118 | static::CONTENT_OFFSET => $record['message'], |
| 119 | ]; |
| 120 | |
| 121 | // Adds the context file. |
| 122 | if (isset($record['context']['file'])) { |
| 123 | $output[static::FILE_OFFSET] = $record['context']['file']; |
| 124 | } |
| 125 | |
| 126 | // Adds the context line. |
| 127 | if (isset($record['context']['line'])) { |
| 128 | $output[static::LINE_OFFSET] = $record['context']['line']; |
| 129 | } |
| 130 | |
| 131 | // Adds the stack information. |
| 132 | if (isset($record['extra'][EnvironmentProcessorAbstract::LOG_STACK])) { |
| 133 | $output[static::STACK_OFFSET] = $record['extra'][EnvironmentProcessorAbstract::LOG_STACK]; |
| 134 | } |
| 135 | |
| 136 | // Adds the trace information. |
| 137 | if (isset($record['extra'][BacktraceProcessor::TRACE_OFFSET])) { |
| 138 | $output[static::TRACE_OFFSET] = $record['extra'][BacktraceProcessor::TRACE_OFFSET]; |
| 139 | } |
| 140 | |
| 141 | return $output; |
| 142 | } |
| 143 | } |