Passed
Push — dev ( 4443cf...7bc15a )
by Markus
02:04
created

SeqBaseFormatter::extractException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
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 Throwable;
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
abstract class SeqBaseFormatter extends JsonFormatter
19
{
20
21
    /**
22
     * The log level map.
23
     * Maps the monolog log levels to the seq log levels.
24
     *
25
     * @var array
26
     */
27
    protected $logLevelMap = [
28
        '100' => 'Debug',
29
        '200' => 'Information',
30
        '250' => 'Information',
31
        '300' => 'Warning',
32
        '400' => 'Error',
33
        '500' => 'Error',
34
        '550' => 'Fatal',
35
        '600' => 'Fatal',
36
    ];
37
38
    /**
39
     * Returns a string with the content type for the seq-formatter.
40
     *
41
     * @return string
42
     */
43
    public abstract function getContentType() : string;
44
45
    /**
46
     * Normalizes an exception to a string.
47
     *
48
     * @param  Throwable $e The throwable instance to normalize.
49
     * @throws InvalidArgumentException Thrown when the parameter is not an instance of a throwable.
50
     * @return string
51
     */
52
	protected function normalizeException($e) : string
53
    {
54
   		if (!$e instanceof \Throwable) {
0 ignored issues
show
introduced by
$e is always a sub-type of Throwable.
Loading history...
55
            throw new \InvalidArgumentException('Throwable expected, got ' . gettype($e) . ' / ' . get_class($e));
56
        }
57
58
        $previousText = '';
59
        if ($previous = $e->getPrevious()) {
60
            do {
61
                $previousText .= ', ' . get_class($previous) . '(code: ' . $previous->getCode() . '): ' . $previous->getMessage() . ' at ' . $previous->getFile() . ':' . $previous->getLine();
62
            } while ($previous = $previous->getPrevious());
63
        }
64
65
        $str = '[object] (' . get_class($e) . '(code: ' . $e->getCode() . '): ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . $previousText . ')';
66
        if ($this->includeStacktraces) {
67
            $str .= "\n[stacktrace]\n" . $e->getTraceAsString() . "\n";
68
        }
69
70
        return $str;
71
    }
72
73
    /**
74
     * Extracts the exception from an array.
75
     *
76
     * @param  array  &$array The array.
77
     * @return \Throwable|null
78
     */
79
    protected function extractException(array &$array) {
80
        $exception = $array['exception'] ?? null;
81
82
        if ($exception !== null) {
83
            unset($array['exception']);
84
        }
85
86
        return $exception;
87
    }
88
89
    /**
90
     * Converts a snake case string to a pascal case string.
91
     *
92
     * @param  string $value The string to convert.
93
     * @return string
94
     */
95
    protected static function ConvertSnakeCaseToPascalCase(string $value = null) : string {
96
        return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value)));
97
    }
98
}