Completed
Push — dev ( 7bc15a...62ab2a )
by Markus
02:11
created

SeqBaseFormatter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractException() 0 8 2
A normalizeException() 0 15 4
A ConvertSnakeCaseToPascalCase() 0 2 1
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
     * @return string
50
     */
51
	protected function normalizeException($e) : string
52
    {
53
   		$previousText = '';
54
        if ($previous = $e->getPrevious()) {
55
            do {
56
                $previousText .= ', ' . get_class($previous) . '(code: ' . $previous->getCode() . '): ' . $previous->getMessage() . ' at ' . $previous->getFile() . ':' . $previous->getLine();
57
            } while ($previous = $previous->getPrevious());
58
        }
59
60
        $str = '[object] (' . get_class($e) . '(code: ' . $e->getCode() . '): ' . $e->getMessage() . ' at ' . $e->getFile() . ':' . $e->getLine() . $previousText . ')';
61
        if ($this->includeStacktraces) {
62
            $str .= "\n[stacktrace]\n" . $e->getTraceAsString() . "\n";
63
        }
64
65
        return $str;
66
    }
67
68
    /**
69
     * Extracts the exception from an array.
70
     *
71
     * @param  array  &$array The array.
72
     * @return \Throwable|null
73
     */
74
    protected function extractException(array &$array) {
75
        $exception = $array['exception'] ?? null;
76
77
        if ($exception !== null) {
78
            unset($array['exception']);
79
        }
80
81
        return $exception;
82
    }
83
84
    /**
85
     * Converts a snake case string to a pascal case string.
86
     *
87
     * @param  string $value The string to convert.
88
     * @return string
89
     */
90
    protected static function ConvertSnakeCaseToPascalCase(string $value = null) : string {
91
        return str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $value)));
92
    }
93
}