Completed
Push — feature_AddTestsAndReduceCodeC... ( 65fe10...f59afe )
by Markus
02:10
created

SeqCompactJsonFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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