Completed
Push — master ( 3aab3d...871730 )
by Stanley
03:53
created

Log   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 25
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogLevel() 0 9 2
A enable() 0 23 1
1
<?php
2
3
namespace Starnerz\LaravelDaraja\Logging;
4
5
use Monolog\Logger;
6
use GuzzleHttp\Middleware;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\MessageFormatter;
9
use Monolog\Handler\StreamHandler;
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
     * @throws \Exception
65
     */
66
    protected static function getLogLevel()
67
    {
68
        $level = strtoupper(config('laravel-daraja.logs.level'));
69
70
        if (array_key_exists($level, self::$levels)) {
71
            return self::$levels[$level];
72
        }
73
74
        throw new \Exception('Debug level not recognized');
75
    }
76
}
77