├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── Facades └── XLog.php ├── Logger.php └── helper.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Kraken Group 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-xlog 2 | Extended Laravel Log Component 3 | XLog adds `User ID`, `User IP`, and `unique Track ID` to each log in a PHP lifecycle. 4 | So you can fetch all logs that are submitted through a Laravel request lifecycle (from beginning to end). 5 | 6 | You can fetch all logs using the tail command. For example if your unique track id was `523586093e` then you can use it like below: 7 | 8 | ``` 9 | cat laravel.log | grep 523586093e 10 | ``` 11 | output: 12 | 13 | ``` 14 | [2024-06-26 00:36:12] production.DEBUG: AsanpardakhtController::token_request {"servicetypeid":1,"merchantconfigurationid":38718596,"localinvoiceid":17193495725213,"amountinrials":6980000,"localdate":"20240626 003612","callbackurl":"https://shop.test/payment/gateway-transactions/pqj5nm/callback","paymentid":0,"additionaldata":null,"settlementportions":[{"iban":"IR600780100710844707074710","amountInRials":6980000,"paymentId":0}],"sid":"","uip":"31.14.119.2","uid":"user","xTrackId":"523586093e"} 15 | [2024-06-26 00:36:13] production.INFO: AsanpardakhtController::token_response hd764QZna1fl1ALEwjoA: 200 {"sid":"","uip":"31.14.119.2","uid":"user","xTrackId":"523586093e"} 16 | ``` 17 | ## Installation 18 | 19 | ```bash 20 | composer require php-monsters/laravel-xlog 21 | ``` 22 | 23 | Add this to your app service providers (only for Laravel < 5.5): 24 | ```php 25 | PhpMonsters\Log\XLogServiceProvider::class, 26 | ``` 27 | 28 | ## Config (optional) 29 | add following keys to your project .env file 30 | 31 | ```ini 32 | # track id key 33 | XLOG_ADD_USERID= (default true) 34 | XLOG_TRACK_ID_KEY= (default xTrackId) 35 | ``` 36 | 37 | 38 | ## Usage 39 | 40 | ```php 41 | use PhpMonsters\Log\XLog; // or register XLog Facade 42 | 43 | XLog::debug('my log message'); 44 | XLog::info('my log message'); 45 | XLog::notice('my log message'); 46 | XLog::warning('my log message'); 47 | XLog::error('my log message'); 48 | XLog::critical('my log message'); 49 | XLog::alert('my log message'); 50 | XLog::emergency('my log message'); 51 | ``` 52 | 53 | ## Pass parameters 54 | ```php 55 | 56 | // passing string as the second parameter 57 | $string = 'test' 58 | XLog::info('log message', [$string]); 59 | 60 | // passing array 61 | $array = ['a' => 1, 'b' => 2, 'c' => 'value3', 'd' => 4.2]; 62 | XLog::info('log message', $array); 63 | // log input data in the controller's action 64 | XLog::info('verify transaction request', $request->all()); 65 | ``` 66 | 67 | 68 | ## Log exception 69 | 70 | ```php 71 | // The first parameter is the thrown exception. The second parameter specifies whether to log the trace or not. the third parameter is the level of the log. default value is `error`. 72 | XLog::exception($exception, true); 73 | XLog::exception($exception, true, 'error'); 74 | XLog::exception($e, false, 'emergency'); 75 | ``` 76 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "php-monsters/laravel-xlog", 3 | "description": "extended Laravel 5+ log", 4 | "keywords": ["laravel", "log", "xlog", "extended logger", "laravel log"], 5 | "type": "laravel-component", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Aboozar Ghaffari <@samuraee>", 10 | "email": "aboozar.ghf@gmail.com" 11 | } 12 | ], 13 | "autoload": { 14 | "psr-4": { 15 | "PhpMonsters\\Log\\": "src" 16 | }, 17 | "files": [ 18 | "src/helper.php" 19 | ] 20 | }, 21 | "require": { 22 | "php": ">=7.0", 23 | "illuminate/support": ">=5.0.0" 24 | }, 25 | "require-dev": { 26 | "phpunit/phpunit": "~4.0" 27 | }, 28 | "extra": { 29 | "laravel": { 30 | "aliases": { 31 | "XLog": "PhpMonsters\\Log\\Facades\\XLog" 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Facades/XLog.php: -------------------------------------------------------------------------------- 1 | 20 | */ 21 | class XLog extends Facade 22 | { 23 | /** 24 | * Get the registered name of the component. 25 | * 26 | * @return string 27 | */ 28 | protected static function getFacadeAccessor () 29 | { 30 | return 'PhpMonsters\Log\Logger'; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Logger.php: -------------------------------------------------------------------------------- 1 | ' . $e->getMessage(); 35 | $arguments [1] = [ 36 | 'code' => $e->getCode(), 37 | 'file' => basename($e->getFile()), 38 | 'line' => $e->getLine(), 39 | 'x_track_id' => REQUEST_TRACKING_ID, 40 | ]; 41 | 42 | if ($trace) { 43 | $arguments[1]['trace'] = $e->getTraceAsString(); 44 | } 45 | 46 | return self::__callStatic($name, $arguments); 47 | } 48 | 49 | 50 | /** 51 | * @param $name 52 | * @param $arguments 53 | * 54 | * @return mixed 55 | */ 56 | public static function __callStatic($name, $arguments) 57 | { 58 | if (!in_array($name, self::$LOG_LEVELS)) { 59 | $name = 'debug'; 60 | } 61 | 62 | if (!is_array($arguments)) { 63 | $arguments = [$arguments]; 64 | } 65 | 66 | // fix the wrong usage of xlog when second parameter is not an array 67 | if (!isset($arguments[1])) { 68 | $arguments[1] = []; 69 | } 70 | 71 | if (!is_array($arguments[1])) { 72 | $arguments[1] = [$arguments[1]]; 73 | } 74 | 75 | $arguments[1]['uip'] = @clientIp(); 76 | 77 | // add user id to all logs 78 | $arguments[1]['uid'] = self::getUserTag(); // user id as a tag 79 | 80 | if (session_status() === PHP_SESSION_ACTIVE) { 81 | $arguments[1]['sid'] = session_id(); 82 | } 83 | 84 | // get request track ID from service container 85 | $arguments[1]['x_track_id'] = REQUEST_TRACKING_ID; 86 | 87 | 88 | return call_user_func_array(['Illuminate\Support\Facades\Log', $name], $arguments); 89 | } 90 | 91 | /** 92 | * @return string 93 | */ 94 | private static function getUserTag(): string 95 | { 96 | // add user id to all logs 97 | if ((bool)env('XLOG_ADD_USERID', true) === false || Auth::guest() === true) { 98 | return 'user'; 99 | } 100 | 101 | if (self::$firstCall === true) { 102 | return 'us' . self::$userUserId . 'er'; 103 | } 104 | 105 | self::$firstCall = true; 106 | self::$userUserId = Auth::user()->id; 107 | return 'us' . self::$userUserId . 'er'; 108 | } 109 | 110 | /** 111 | * @param $name 112 | * @param $arguments 113 | * 114 | * @return mixed 115 | */ 116 | public function __call($name, $arguments) 117 | { 118 | if (!in_array($name, self::$LOG_LEVELS)) { 119 | $name = 'debug'; 120 | } 121 | 122 | return self::__callStatic($name, $arguments); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/helper.php: -------------------------------------------------------------------------------- 1 |