Completed
Pull Request — master (#1)
by Ryuichi
02:26
created

LoggerUtils   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 54
ccs 23
cts 26
cp 0.8846
rs 10
c 0
b 0
f 0

3 Methods

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