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

LoggerUtils::addStackTrace()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
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
     * @param string ログメッセージ
36
     * @param string スタックトレース文字列
37
     * @return string 加工済みログメッセージ
38
     */
39
    public function addStackTrace($msg, $stacktrace)
40
    {
41
        // スタックトレースから原因となるエラー箇所のみ抽出
42
        $stacktraceList = explode("#", $stacktrace);
43
        foreach ($stacktraceList as $stacktraceLine) {
44
            if ($stacktraceLine === "") {
45
                continue;
46
            }
47
            $msg .= PHP_EOL;
48
            $msg .= "\t#" . trim($stacktraceLine);
49
        }
50
51
        return $msg;
52
    }
53
54
    /**
55
     * ログレベルを数値に変換
56
     * ログレベルはWebStream独自、PSR-3両方対応
57
     * @param string ログレベル文字列
58
     * @throws LoggerException
59
     * @return int ログレベル数値
60
     */
61
    public function toLogLevelValue(string $level)
62
    {
63
        switch (strtolower($level)) {
64
            case 'debug':
65
                return 1;
66
            case 'info':
67
                return 2;
68
            case 'notice':    // PSR-3
69
                return 3;
70
            case 'warn':
71
            case 'warning':   // PSR-3
72
                return 4;
73
            case 'error':
74
                return 5;
75
            case 'critical':  // PSR-3
76
                return 6;
77
            case 'alert':     // PSR-3
78
                return 7;
79
            case 'emergency': // PSR-3
80
                return 8;
81
            case 'fatal':
82
                return 9;
83
            default:
84
                throw new LoggerException("Undefined log level: $level");
85
        }
86
    }
87
}
88