generated from mimmi20/template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogmaticFormatterFactory.php
110 lines (92 loc) · 4 KB
/
LogmaticFormatterFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* This file is part of the mimmi20/monolog-factory package.
*
* Copyright (c) 2022-2023, Thomas Mueller <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace Mimmi20\MonologFactory\Formatter;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Monolog\Formatter\JsonFormatter;
use Monolog\Formatter\LogmaticFormatter;
use Psr\Container\ContainerInterface;
use RuntimeException;
use function array_key_exists;
use function is_array;
use function sprintf;
final class LogmaticFormatterFactory implements FactoryInterface
{
/**
* @param string $requestedName
* @param array<string, (bool|int|string)>|null $options
* @phpstan-param array{batchMode?: JsonFormatter::BATCH_MODE_*, appendNewline?: bool, hostname?: string, appName?: string, includeStacktraces?: bool, ignoreEmptyContextAndExtra?: bool, dateFormat?: string, maxNormalizeDepth?: int, maxNormalizeItemCount?: int, prettyPrint?: bool}|null $options
*
* @throws ServiceNotCreatedException
*
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
public function __invoke(
ContainerInterface $container,
$requestedName,
array | null $options = null,
): LogmaticFormatter {
$batchMode = JsonFormatter::BATCH_MODE_JSON;
$appendNewline = true;
$ignoreEmptyContextAndExtra = false;
$maxNormalizeDepth = NormalizerFormatterFactory::DEFAULT_NORMALIZER_DEPTH;
$maxNormalizeItemCount = NormalizerFormatterFactory::DEFAULT_NORMALIZER_ITEM_COUNT;
$prettyPrint = false;
if (is_array($options)) {
if (array_key_exists('batchMode', $options)) {
$batchMode = $options['batchMode'];
}
if (array_key_exists('appendNewline', $options)) {
$appendNewline = $options['appendNewline'];
}
if (array_key_exists('ignoreEmptyContextAndExtra', $options)) {
$ignoreEmptyContextAndExtra = $options['ignoreEmptyContextAndExtra'];
}
if (array_key_exists('maxNormalizeDepth', $options)) {
$maxNormalizeDepth = $options['maxNormalizeDepth'];
}
if (array_key_exists('maxNormalizeItemCount', $options)) {
$maxNormalizeItemCount = $options['maxNormalizeItemCount'];
}
if (array_key_exists('prettyPrint', $options)) {
$prettyPrint = $options['prettyPrint'];
}
}
try {
$formatter = new LogmaticFormatter($batchMode, $appendNewline, $ignoreEmptyContextAndExtra);
} catch (RuntimeException $e) {
throw new ServiceNotCreatedException(
sprintf('Could not create %s', LogmaticFormatter::class),
0,
$e,
);
}
if (is_array($options)) {
if (array_key_exists('hostname', $options)) {
$formatter->setHostname($options['hostname']);
}
if (array_key_exists('appName', $options)) {
$formatter->setAppName($options['appName']);
}
if (array_key_exists('includeStacktraces', $options)) {
$formatter->includeStacktraces($options['includeStacktraces']);
}
if (array_key_exists('dateFormat', $options)) {
$formatter->setDateFormat($options['dateFormat']);
}
}
$formatter->setMaxNormalizeDepth($maxNormalizeDepth);
$formatter->setMaxNormalizeItemCount($maxNormalizeItemCount);
$formatter->setJsonPrettyPrint($prettyPrint);
return $formatter;
}
}