|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: inhere |
| 5 | + * Date: 2017/9/21 |
| 6 | + * Time: 下午11:26 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Inhere\Library\Traits; |
| 10 | + |
| 11 | +use Inhere\Library\Helpers\PhpHelper; |
| 12 | +use Inhere\Library\Helpers\Req; |
| 13 | +use Monolog\Logger; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class LogShortTrait |
| 17 | + * @package Sws\Components |
| 18 | + */ |
| 19 | +trait LogShortTrait |
| 20 | +{ |
| 21 | + /** |
| 22 | + * System is unusable. |
| 23 | + * |
| 24 | + * @param string $message |
| 25 | + * @param array $context |
| 26 | + * |
| 27 | + * @return void |
| 28 | + */ |
| 29 | + public static function emergency($message, array $context = array()) |
| 30 | + { |
| 31 | + self::log(Logger::EMERGENCY, $message, $context); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Action must be taken immediately. |
| 36 | + * |
| 37 | + * Example: Entire website down, database unavailable, etc. This should |
| 38 | + * trigger the SMS alerts and wake you up. |
| 39 | + * |
| 40 | + * @param string $message |
| 41 | + * @param array $context |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public static function alert($message, array $context = array()) |
| 46 | + { |
| 47 | + self::log(Logger::ALERT, $message, $context); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Critical conditions. |
| 52 | + * |
| 53 | + * Example: Application component unavailable, unexpected exception. |
| 54 | + * |
| 55 | + * @param string $message |
| 56 | + * @param array $context |
| 57 | + * |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public static function critical($message, array $context = array()) |
| 61 | + { |
| 62 | + self::log(Logger::CRITICAL, $message, $context); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Runtime errors that do not require immediate action but should typically |
| 67 | + * be logged and monitored. |
| 68 | + * |
| 69 | + * @param string $message |
| 70 | + * @param array $context |
| 71 | + * |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + public static function error($message, array $context = array()) |
| 75 | + { |
| 76 | + self::log(Logger::ERROR, $message, $context); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Exceptional occurrences that are not errors. |
| 81 | + * |
| 82 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
| 83 | + * that are not necessarily wrong. |
| 84 | + * |
| 85 | + * @param string $message |
| 86 | + * @param array $context |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public static function warning($message, array $context = array()) |
| 91 | + { |
| 92 | + self::log(Logger::WARNING, $message, $context); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Normal but significant events. |
| 97 | + * |
| 98 | + * @param string $message |
| 99 | + * @param array $context |
| 100 | + * |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public static function notice($message, array $context = array()) |
| 104 | + { |
| 105 | + self::log(Logger::NOTICE, $message, $context); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Interesting events. |
| 110 | + * |
| 111 | + * Example: User logs in, SQL logs. |
| 112 | + * |
| 113 | + * @param string $message |
| 114 | + * @param array $context |
| 115 | + * |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public static function info($message, array $context = array()) |
| 119 | + { |
| 120 | + self::log(Logger::INFO, $message, $context); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Detailed debug information. |
| 125 | + * |
| 126 | + * @param string $message |
| 127 | + * @param array $context |
| 128 | + * |
| 129 | + * @return void |
| 130 | + */ |
| 131 | + public static function debug($message, array $context = array()) |
| 132 | + { |
| 133 | + self::log(Logger::DEBUG, $message, $context); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param string $message |
| 138 | + * @param array $context |
| 139 | + */ |
| 140 | + public static function trace($message, array $context = array()) |
| 141 | + { |
| 142 | + $tce = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
| 143 | + |
| 144 | + if ($info = $tce[1] ?? null) { |
| 145 | + $context['_called_at'] = sprintf('%s::%s Line %d', $info['class'], $info['function'], $tce[0]['line']); |
| 146 | + } |
| 147 | + |
| 148 | + $context['_stats'] = PhpHelper::runtime(Req::server('request_time_float'), Req::server('request_memory')); |
| 149 | + |
| 150 | + self::log(Logger::DEBUG, $message, $context); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Logs with an arbitrary level. |
| 155 | + * |
| 156 | + * @param mixed $level |
| 157 | + * @param string $message |
| 158 | + * @param array $context |
| 159 | + * |
| 160 | + * @return void |
| 161 | + */ |
| 162 | + abstract public static function log($level, $message, array $context = array()); |
| 163 | +} |
0 commit comments