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

LoggerUtils::defaultDateTimeFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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