Completed
Push — feature/0.7.0 ( 79c386...7f3169 )
by Ryuichi
03:02
created

LoggerUtils::toLogLevelValue()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 5.2653
cc 11
eloc 23
nc 11
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace WebStream\Module\Utility;
3
4
use WebStream\Exception\Extend\LoggerException;
5
6
/**
7
 * LoggerUtils
8
 * ロガー依存のUtility
9
 * @author Ryuichi Tanaka
10
 * @since 2015/12/26
11
 * @version 0.7
12
 */
13
trait LoggerUtils
14
{
15
    /**
16
     * デフォルトロガーフォーマッタ
17
     * @return string デフォルトロガーフォーマッタ
18
     */
19
    public function defaultLoggerFormatter()
20
    {
21
        return '[%d{' . $this->defaultDateTimeFormatter() . '.%f}][%5L] %m';
22
    }
23
24
    /**
25
     * デフォルトDateTimeフォーマッタ
26
     * @return string デフォルトDateTimeフォーマッタ
27
     */
28
    public function defaultDateTimeFormatter()
29
    {
30
        return "%Y-%m-%d %H:%M:%S";
31
    }
32
33
    /**
34
     * ログレベルを数値に変換
35
     * ログレベルはWebStream独自、PSR-3両方対応
36
     * @param string ログレベル文字列
37
     * @throws LoggerException
38
     * @return int ログレベル数値
39
     */
40
    public function toLogLevelValue(string $level)
41
    {
42
        switch (strtolower($level)) {
43
            case 'debug':
44
                return 1;
45
            case 'info':
46
                return 2;
47
            case 'notice':    // PSR-3
48
                return 3;
49
            case 'warn':
50
            case 'warning':   // PSR-3
51
                return 4;
52
            case 'error':
53
                return 5;
54
            case 'critical':  // PSR-3
55
                return 6;
56
            case 'alert':     // PSR-3
57
                return 7;
58
            case 'emergency': // PSR-3
59
                return 8;
60
            case 'fatal':
61
                return 9;
62
            default:
63
                throw new LoggerException("Undefined log level: $level");
64
        }
65
    }
66
}
67