Log::enable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 23
rs 9.9
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Starnerz\LaravelDaraja\Logging;
4
5
use GuzzleHttp\HandlerStack;
6
use GuzzleHttp\MessageFormatter;
7
use GuzzleHttp\Middleware;
8
use Monolog\Handler\StreamHandler;
9
use Monolog\Logger;
10
11
class Log
12
{
13
    /**
14
     * All the available debug levels.
15
     *
16
     * @var array
17
     */
18
    protected static $levels = [
19
        'DEBUG' => Logger::DEBUG,
20
        'INFO' => Logger::INFO,
21
        'NOTICE' => Logger::NOTICE,
22
        'WARNING' => Logger::WARNING,
23
        'ERROR' => Logger::ERROR,
24
        'CRITICAL' => Logger::CRITICAL,
25
        'ALERT' => Logger::ALERT,
26
        'EMERGENCY' => Logger::EMERGENCY,
27
    ];
28
29
    /**
30
     * Set up the logging requirements for the Guzzle package.
31
     *
32
     * @param $options
33
     * @return int
34
     */
35
    public static function enable($options)
36
    {
37
        $level = self::getLogLevel();
38
39
        $handler = new Logger(
40
            'LaravelDaraja',
41
            [
42
                new StreamHandler(storage_path('logs/laravel-daraja.log'), $level),
43
            ]
44
45
        );
46
47
        $stack = HandlerStack::create();
48
        $stack->push(
49
            Middleware::log(
50
                $handler,
51
                new MessageFormatter('{method} {uri} HTTP/{version} {req_body} RESPONSE: {code} - {res_body}')
52
            )
53
        );
54
55
        $options['handler'] = $stack;
56
57
        return $options;
58
    }
59
60
    /**
61
     * Determine the log level specified in the configurations.
62
     *
63
     * @return mixed
64
     *
65
     * @throws \Exception
66
     */
67
    protected static function getLogLevel()
68
    {
69
        $level = strtoupper(config('laravel-daraja.logs.level'));
70
71
        if (array_key_exists($level, self::$levels)) {
72
            return self::$levels[$level];
73
        }
74
75
        throw new \Exception('Debug level not recognized');
76
    }
77
}
78