|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tartan\Log; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Auth; |
| 6 | +use Exception; |
| 7 | + |
| 8 | +class XLog |
| 9 | +{ |
| 10 | + private static $LOG_LEVELS = ['debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency']; |
| 11 | + |
| 12 | + public function __call($name, $arguments) |
| 13 | + { |
| 14 | + if (!in_array($name, self::$LOG_LEVELS)) { |
| 15 | + $name = 'debug'; |
| 16 | + } |
| 17 | + |
| 18 | + return call_user_func_array(['Illuminate\Support\Facades\Log', $name], $arguments); |
| 19 | + } |
| 20 | + |
| 21 | + public static function __callStatic($name, $arguments) |
| 22 | + { |
| 23 | + if (session_status() == PHP_SESSION_NONE) { |
| 24 | + $arguments[1]['sid'] = session_id(); |
| 25 | + } else { |
| 26 | + $arguments[1]['sid'] = ''; |
| 27 | + } |
| 28 | + |
| 29 | + $arguments[1]['uip'] = @clientIp(); |
| 30 | + |
| 31 | + // add user id to all logs |
| 32 | + if (env('XLOG_ADD_USERID', true)) { |
| 33 | + if (!Auth::guest()) { |
| 34 | + $arguments[1]['uid'] = 'us' . Auth::user()->id . 'er'; // user id as a tag |
| 35 | + } |
| 36 | + } |
| 37 | + $trackIdKey = env('XLOG_TRACK_ID_KEY', 'xTrackId'); |
| 38 | + |
| 39 | + // get request track ID from service container |
| 40 | + if (!isset($arguments[1][$trackIdKey])) { |
| 41 | + $arguments[1][$trackIdKey] = resolve($trackIdKey); |
| 42 | + } |
| 43 | + |
| 44 | + return call_user_func_array(['Illuminate\Support\Facades\Log', $name], $arguments); |
| 45 | + } |
| 46 | + |
| 47 | + public static function exception(Exception $e, $level = 'error') |
| 48 | + { |
| 49 | + $arguments = []; |
| 50 | + $arguments [0] = 'exception' . $e->getMessage(); |
| 51 | + $arguments [1] = [ |
| 52 | + 'code' => $e->getCode(), |
| 53 | + 'file' => basename($e->getFile()), |
| 54 | + 'line' => $e->getLine(), |
| 55 | + ]; |
| 56 | + |
| 57 | + return call_user_func_array(['XLog', $level], $arguments); |
| 58 | + } |
| 59 | +} |
0 commit comments