Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CloudWatchJsonFormatter | |
0.00% |
0 / 22 |
|
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 / 15 |
|
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 | |
| 26 | /** |
| 27 | * Json log formatter. |
| 28 | * |
| 29 | * @package oat\oatbox\log\logger\formatter |
| 30 | */ |
| 31 | class CloudWatchJsonFormatter implements FormatterInterface |
| 32 | { |
| 33 | /** |
| 34 | * Used datetime format. |
| 35 | */ |
| 36 | public const DATETIME_FORMAT = 'd/m/Y:H:i:s O'; |
| 37 | |
| 38 | /** |
| 39 | * @inheritdoc |
| 40 | * |
| 41 | * @throws \RuntimeException |
| 42 | */ |
| 43 | public function format(array $record) |
| 44 | { |
| 45 | $jsonString = json_encode($this->getOutputRecord($record)); |
| 46 | |
| 47 | if ($jsonString === false) { |
| 48 | throw new \RuntimeException('Error happened during the log format process! ' . json_last_error_msg()); |
| 49 | } |
| 50 | |
| 51 | return $jsonString . PHP_EOL; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @inheritdoc |
| 56 | * |
| 57 | * @throws \ErrorException |
| 58 | */ |
| 59 | public function formatBatch(array $records) |
| 60 | { |
| 61 | foreach ($records as $key => $record) { |
| 62 | $records[$key] = $this->format($record); |
| 63 | } |
| 64 | |
| 65 | return $records; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the customized record. |
| 70 | * |
| 71 | * @param array $record |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | protected function getOutputRecord(array $record) |
| 76 | { |
| 77 | // Adds the basic log details. |
| 78 | $output = [ |
| 79 | 'datetime' => $record['datetime']->format(static::DATETIME_FORMAT), |
| 80 | 'severity' => $record['level_name'], |
| 81 | 'message' => $record['message'], |
| 82 | 'tag' => $record['context'], |
| 83 | ]; |
| 84 | |
| 85 | if (isset($record['extra']['stack']['host_type'])) { |
| 86 | $output['host_type'] = $record['extra']['stack']['host_type']; |
| 87 | } |
| 88 | if (isset($record['extra']['stack']['id'])) { |
| 89 | $output['instance_id'] = $record['extra']['stack']['id']; |
| 90 | } |
| 91 | if (isset($record['extra']['trace'])) { |
| 92 | $output['trace'] = $record['extra']['trace']; |
| 93 | } |
| 94 | if (isset($record['user_id'])) { |
| 95 | $output['user_id'] = $record['user_id']; |
| 96 | } |
| 97 | |
| 98 | return $output; |
| 99 | } |
| 100 | } |