SeqCompactJsonFormatter::processDatetime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Msschl\Monolog\Formatter;
4
5
use DateTime;
6
use Monolog\Formatter\FormatterInterface;
7
use Monolog\Formatter\JsonFormatter;
8
use Msschl\Monolog\Exception\InvalidCodePathException;
9
10
/**
11
 * This file is part of the msschl\monolog-seq-handler package.
12
 *
13
 * Copyright (c) 2018 Markus Schlotbohm
14
 *
15
 * For the full copyright and license information, please view the LICENSE.md
16
 * file that was distributed with this source code.
17
 */
18
class SeqCompactJsonFormatter extends SeqBaseFormatter
19
{
20
21
    /**
22
     * The extract context flag.
23
     * Whether to extract the context array to the root or not.
24
     *
25
     * @var bool
26
     */
27
    protected $extractContext;
28
29
    /**
30
     * The extract extras flag.
31
     * Whether to extract the extras array to the root or not.
32
     *
33
     * @var bool
34
     */
35
    protected $extractExtras;
36
37
    /**
38
     * Initializes a new instance of the {@see SeqCompactJsonFormatter} class.
39
     *
40
     * @param  bool $extractContext Flag that indicates whether to extract the extras array
41
     *                              to the root or not.
42
     * @param  bool $extractExtras  Flag that indicates whether to extract the context array
43
     *                              to the root or not.
44
     */
45
	public function __construct(bool $extractContext = true, bool $extractExtras = true)
46
	{
47
        $this->extractContext = $extractContext;
48
        $this->extractExtras = $extractExtras;
49
50
        parent::__construct(JsonFormatter::BATCH_MODE_NEWLINES);
51
	}
52
53
    /**
54
     * Returns a string with the content type for the seq-formatter.
55
     *
56
     * @return string
57
     */
58
    public function getContentType() : string {
59
        return 'application/vnd.serilog.clef';
60
    }
61
62
    /**
63
     * Gets whether the flag extract content is set or not.
64
     *
65
     * @return bool
66
     */
67
    public function getExtractContent() : bool
68
    {
69
        return $this->extractContext;
70
    }
71
72
    /**
73
     * Sets the flag extract content.
74
     *
75
     * @param  bool $value The flag.
76
     * @return self
77
     */
78
    public function setExtractContent(bool $value)
79
    {
80
        $this->extractContext = $value;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Gets whether the flag extract extras is set or not.
87
     *
88
     * @return bool
89
     */
90
    public function getExtractExtras()
91
    {
92
        return $this->extractExtras;
93
    }
94
95
    /**
96
     * Sets the flag extract extras.
97
     *
98
     * @param  bool $value The flag.
99
     * @return self
100
     */
101
    public function setExtractExtras(bool $value)
102
    {
103
        $this->extractExtras = $value;
104
105
        return $this;
106
    }
107
108
    /**
109
     * This function should never be called!!!
110
     *
111
     * @throws \Msschl\Monolog\Exception\InvalidCodePathException
112
     */
113
    protected function formatBatchJson(array $records)
114
    {
115
        /* istanbul ignore next */
116
        throw new InvalidCodePathException();
117
    }
118
119
    /**
120
     * Processes the log message.
121
     *
122
     * @param  array  &$normalized Reference to the normalized array, where all normalized data get stored.
123
     * @param  string $message     The log message.
124
     * @return void
125
     */
126
    protected function processMessage(array &$normalized, string $message)
127
    {
128
        $normalized['@m'] = $message;
129
        if (!(strpos($message, '{') === false)) {
130
            $normalized['@mt'] = $message;
131
        }
132
    }
133
134
    /**
135
     * Processes the context array.
136
     *
137
     * @param  array &$normalized Reference to the normalized array, where all normalized data get stored.
138
     * @param  array $message     The context array.
139
     * @return void
140
     */
141
    protected function processContext(array &$normalized, array $context)
142
    {
143
        $this->processContextException($normalized, $context);
144
        $array = $this->getNormalizedArray($context);
145
146
        if ($this->extractContext) {
147
            $normalized = array_merge($array, $normalized);
148
        } else {
149
            $normalized['Context'] = $array;
150
        }
151
    }
152
153
    /**
154
     * Processes the log level.
155
     *
156
     * @param  array &$normalized Reference to the normalized array, where all normalized data get stored.
157
     * @param  int   $message     The log level.
158
     * @return void
159
     */
160
    protected function processLevel(array &$normalized, int $level)
161
    {
162
        $normalized['@l'] = $this->logLevelMap[$level];
163
        $normalized['Code'] = $level;
164
    }
165
166
    /**
167
     * Processes the log level name.
168
     *
169
     * @param  array  &$normalized Reference to the normalized array, where all normalized data get stored.
170
     * @param  string $message     The log level name.
171
     * @return void
172
     */
173
    protected function processLevelName(array &$normalized, string $levelName)
174
    {
175
        $normalized['LevelName'] = $levelName;
176
    }
177
178
    /**
179
     * Processes the channel name.
180
     *
181
     * @param  array  &$normalized Reference to the normalized array, where all normalized data get stored.
182
     * @param  string $message     The log channel name.
183
     * @return void
184
     */
185
    protected function processChannel(array &$normalized, string $name)
186
    {
187
        $normalized['Channel'] = $name;
188
    }
189
190
    /**
191
     * Processes the log timestamp.
192
     *
193
     * @param  array    &$normalized Reference to the normalized array, where all normalized data get stored.
194
     * @param  DateTime $message     The log timestamp.
195
     * @return void
196
     */
197
    protected function processDatetime(array &$normalized, DateTime $datetime)
198
    {
199
        $normalized['@t'] = $datetime->format(DateTime::ISO8601);
200
    }
201
202
    /**
203
     * Processes the extras array.
204
     *
205
     * @param  array &$normalized Reference to the normalized array, where all normalized data get stored.
206
     * @param  array $message     The extras array.
207
     * @return void
208
     */
209
    protected function processExtra(array &$normalized, array $extras)
210
    {
211
        $array = $this->getNormalizedArray($extras);
212
213
        if ($this->extractExtras) {
214
            $normalized = array_merge($array, $normalized);
215
        } else {
216
            $normalized['Extra'] = $array;
217
        }
218
    }
219
220
    /**
221
     * Extracts the exception from the context array.
222
     *
223
     * @param  array  &$normalized Reference to the normalized array, where all normalized data get stored.
224
     * @param  array  $context     The context array.
225
     * @return void
226
     */
227
    private function processContextException(array &$normalized, array $context)
228
    {
229
        $exception = $this->extractException($context);
230
        if ($exception !== null) {
231
            $normalized['@x'] = $this->normalizeException($exception);
232
        }
233
    }
234
235
    /**
236
     * Gets a normalized array.
237
     *
238
     * @param  array $array The array to process.
239
     * @return array
240
     */
241
    private function getNormalizedArray(array $array) : array
242
    {
243
        $normalized = [];
244
        $count = 1;
245
        foreach ($array as $key => $value) {
246
            if ($count++ >= 1000) {
247
                $normalized['...'] = 'Over 1000 items, aborting normalization';
248
                break;
249
            }
250
251
            if (is_int($key)) {
252
                $normalized[] = $value;
253
            } else {
254
                $key = SeqCompactJsonFormatter::ConvertSnakeCaseToPascalCase($key);
255
                $normalized[$key] = $value;
256
            }
257
        }
258
259
        return $normalized;
260
    }
261
}