Skip to content

Commit 3df4046

Browse files
committed
fixed loop bug in db query log
1 parent 2af0509 commit 3df4046

File tree

5 files changed

+102
-60
lines changed

5 files changed

+102
-60
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ XLog adds `User ID`, `User IP`, `Track ID` to each log
55
## Installation
66

77
```bash
8-
composer require tartan/laravel-xlog
8+
composer require php-monsters/laravel-xlog
99
```
1010

1111
Add this to your app service providers :
1212
```php
13-
Tartan\Log\XLogServiceProvider::class,
13+
PhpMonsters\Log\XLogServiceProvider::class,
1414
```
1515

1616
## Config (optional)
@@ -26,7 +26,7 @@ XLOG_TRACK_ID_KEY= (default xTrackId)
2626
## Usage
2727

2828
```php
29-
use Tartan\Log\XLog; // or register XLog Facade
29+
use PhpMonsters\Log\XLog; // or register XLog Facade
3030

3131
XLog::debug('test message');
3232
XLog::info('test message');

composer.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
{
2-
"name": "tartan/laravel-xlog",
2+
"name": "php-monsters/laravel-xlog",
33
"description": "extended Laravel 5+ log",
44
"keywords": ["laravel", "log", "xlog", "extended logger", "laravel log"],
55
"type": "laravel-component",
66
"license": "MIT",
77
"authors": [
88
{
9-
"name": "Aboozar Ghaffari <Tartan>",
9+
"name": "Aboozar Ghaffari <@samuraee>",
1010
"email": "[email protected]"
1111
}
1212
],
1313
"autoload": {
1414
"psr-4": {
15-
"Tartan\\Log\\": "src"
15+
"PhpMonsters\\Log\\": "src"
1616
},
1717
"files": [
1818
"src/helper.php"
1919
]
2020
},
2121
"require": {
22-
"php": ">=5.4",
22+
"php": ">=7.0",
2323
"illuminate/support": ">=5.0.0"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "~4.0"
27+
},
28+
"extra": {
29+
"laravel": {
30+
"providers": [
31+
"PhpMonsters\\Log\\XLogServiceProvider"
32+
],
33+
"aliases": {
34+
"XLog": "PhpMonsters\\Log\\Facade"
35+
}
36+
}
2737
}
2838
}

src/Facades/XLog.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
namespace Tartan\Log\Facades;
3+
namespace PhpMonsters\Log\Facades;
44

55
use Illuminate\Support\Facades\Facade;
66

77
/**
88
* Class XLog
9-
* @package Tartan\XLog
9+
* @package PhpMonsters\XLog
1010
* @author Aboozar Ghaffari <[email protected]>
1111
*/
1212
class XLog extends Facade
@@ -18,6 +18,6 @@ class XLog extends Facade
1818
*/
1919
protected static function getFacadeAccessor ()
2020
{
21-
return 'Tartan\Log\Logger';
21+
return 'PhpMonsters\Log\Logger';
2222
}
2323
}

src/Logger.php

Lines changed: 81 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Tartan\Log;
3+
namespace PhpMonsters\Log;
44

5-
use Illuminate\Support\Facades\Auth;
65
use Exception;
6+
use Illuminate\Support\Facades\Auth;
77

88
class Logger
99
{
@@ -13,20 +13,63 @@ class Logger
1313
private static $LOG_LEVELS = ['debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'];
1414

1515
/**
16-
* @param $name
17-
* @param $arguments
18-
*
16+
* @var string
17+
*/
18+
private static $userUserId = null;
19+
20+
/**
21+
* @var bool
22+
*/
23+
private static $firstCall = false;
24+
25+
/**
26+
* @param Exception $e
27+
* @param bool $trace log trace string or not
28+
* @param string $name
1929
* @return mixed
2030
*/
21-
public function __call($name, $arguments)
31+
public static function exception(Exception $e, bool $trace = false, string $name = 'error')
2232
{
23-
if (!in_array($name, self::$LOG_LEVELS)) {
24-
$name = 'debug';
33+
$arguments = [];
34+
$arguments [0] = 'exception-> ' . $e->getMessage();
35+
$arguments [1] = [
36+
'code' => $e->getCode(),
37+
'file' => basename($e->getFile()),
38+
'line' => $e->getLine(),
39+
self::getTrackIdKey() => self::getTrackId(),
40+
];
41+
42+
if ($trace) {
43+
$arguments[1]['trace'] = $e->getTraceAsString();
2544
}
2645

2746
return self::__callStatic($name, $arguments);
2847
}
2948

49+
/**
50+
* @return string
51+
*/
52+
public static function getTrackIdKey(): string
53+
{
54+
return env('XLOG_TRACK_ID_KEY', 'xTrackId');
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
protected static function getTrackId(): string
61+
{
62+
$trackIdKey = self::getTrackIdKey();
63+
64+
try {
65+
$trackId = resolve($trackIdKey);
66+
} catch (Exception $e) {
67+
$trackId = '-';
68+
}
69+
70+
return $trackId;
71+
}
72+
3073
/**
3174
* @param $name
3275
* @param $arguments
@@ -48,28 +91,19 @@ public static function __callStatic($name, $arguments)
4891
$arguments[1] = [];
4992
}
5093

51-
if(!is_array($arguments[1])){
94+
if (!is_array($arguments[1])) {
5295
$arguments[1] = [$arguments[1]];
5396
}
5497

55-
if (session_status() == PHP_SESSION_NONE) {
56-
$arguments[1]['sid'] = session_id();
57-
} else {
58-
$arguments[1]['sid'] = '';
59-
}
98+
$arguments[1]['sid'] = self::getSessionId();
6099

61100
$arguments[1]['uip'] = @clientIp();
62101

63102
// add user id to all logs
64-
if (env('XLOG_ADD_USERID', true)) {
65-
if (!Auth::guest()) {
66-
$arguments[1]['uid'] = 'us' . Auth::user()->id . 'er'; // user id as a tag
67-
}
68-
}
69-
$trackIdKey = env('XLOG_TRACK_ID_KEY', 'xTrackId');
103+
$arguments[1]['uid'] = self::getUserTag(); // user id as a tag
70104

71105
// get request track ID from service container
72-
106+
$trackIdKey = self::getTrackIdKey();
73107
if (!isset($arguments[1][$trackIdKey])) {
74108
$arguments[1][$trackIdKey] = self::getTrackId($trackIdKey);
75109
}
@@ -78,49 +112,47 @@ public static function __callStatic($name, $arguments)
78112
}
79113

80114
/**
81-
* @param Exception $e
82-
* @param string $level
83-
*
84-
* @return mixed
115+
* @return string
85116
*/
86-
public static function exception(Exception $e, $name = 'error')
117+
private static function getSessionId(): string
87118
{
88-
$arguments = [];
89-
$arguments [0] = 'exception-> ' . $e->getMessage();
90-
$arguments [1] = [
91-
'code' => $e->getCode(),
92-
'file' => basename($e->getFile()),
93-
'line' => $e->getLine(),
94-
self::getTrackIdKey() => self::getTrackId(),
95-
];
96-
97-
return self::__callStatic($name, $arguments);
119+
if (session_status() === PHP_SESSION_ACTIVE) {
120+
return session_id();
121+
}
122+
return '';
98123
}
99124

100125
/**
101126
* @return string
102127
*/
103-
public static function getTrackIdKey()
128+
private static function getUserTag(): string
104129
{
105-
return env('XLOG_TRACK_ID_KEY', 'xTrackId');
130+
// add user id to all logs
131+
if ((bool)env('XLOG_ADD_USERID', true) === false || Auth::guest() === true) {
132+
return 'user';
133+
}
134+
135+
if (self::$firstCall === true) {
136+
return 'us' . self::$userUserId . 'er';
137+
}
138+
139+
self::$firstCall = true;
140+
self::$userUserId = Auth::user()->id;
141+
return 'us' . self::$userUserId . 'er';
106142
}
107143

108144
/**
109-
* @param $trackIdKey
145+
* @param $name
146+
* @param $arguments
110147
*
111-
* @return string
148+
* @return mixed
112149
*/
113-
protected static function getTrackId()
150+
public function __call($name, $arguments)
114151
{
115-
$trackIdKey = self::getTrackIdKey();
116-
117-
try {
118-
$trackId = resolve($trackIdKey);
119-
} catch (Exception $e) {
120-
$trackId = '-';
152+
if (!in_array($name, self::$LOG_LEVELS)) {
153+
$name = 'debug';
121154
}
122155

123-
return $trackId;
156+
return self::__callStatic($name, $arguments);
124157
}
125-
126158
}

src/XLogServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tartan\Log;
3+
namespace PhpMonsters\Log;
44

55
use Illuminate\Support\ServiceProvider;
66

0 commit comments

Comments
 (0)