1
1
<?php
2
2
3
- namespace Tartan \Log ;
3
+ namespace PhpMonsters \Log ;
4
4
5
- use Illuminate \Support \Facades \Auth ;
6
5
use Exception ;
6
+ use Illuminate \Support \Facades \Auth ;
7
7
8
8
class Logger
9
9
{
@@ -13,20 +13,63 @@ class Logger
13
13
private static $ LOG_LEVELS = ['debug ' , 'info ' , 'notice ' , 'warning ' , 'error ' , 'critical ' , 'alert ' , 'emergency ' ];
14
14
15
15
/**
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
19
29
* @return mixed
20
30
*/
21
- public function __call ( $ name , $ arguments )
31
+ public static function exception ( Exception $ e , bool $ trace = false , string $ name = ' error ' )
22
32
{
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 ();
25
44
}
26
45
27
46
return self ::__callStatic ($ name , $ arguments );
28
47
}
29
48
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
+
30
73
/**
31
74
* @param $name
32
75
* @param $arguments
@@ -48,28 +91,19 @@ public static function __callStatic($name, $arguments)
48
91
$ arguments [1 ] = [];
49
92
}
50
93
51
- if (!is_array ($ arguments [1 ])){
94
+ if (!is_array ($ arguments [1 ])) {
52
95
$ arguments [1 ] = [$ arguments [1 ]];
53
96
}
54
97
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 ();
60
99
61
100
$ arguments [1 ]['uip ' ] = @clientIp ();
62
101
63
102
// 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
70
104
71
105
// get request track ID from service container
72
-
106
+ $ trackIdKey = self :: getTrackIdKey ();
73
107
if (!isset ($ arguments [1 ][$ trackIdKey ])) {
74
108
$ arguments [1 ][$ trackIdKey ] = self ::getTrackId ($ trackIdKey );
75
109
}
@@ -78,49 +112,47 @@ public static function __callStatic($name, $arguments)
78
112
}
79
113
80
114
/**
81
- * @param Exception $e
82
- * @param string $level
83
- *
84
- * @return mixed
115
+ * @return string
85
116
*/
86
- public static function exception ( Exception $ e , $ name = ' error ' )
117
+ private static function getSessionId (): string
87
118
{
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 '' ;
98
123
}
99
124
100
125
/**
101
126
* @return string
102
127
*/
103
- public static function getTrackIdKey ()
128
+ private static function getUserTag (): string
104
129
{
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 ' ;
106
142
}
107
143
108
144
/**
109
- * @param $trackIdKey
145
+ * @param $name
146
+ * @param $arguments
110
147
*
111
- * @return string
148
+ * @return mixed
112
149
*/
113
- protected static function getTrackId ( )
150
+ public function __call ( $ name , $ arguments )
114
151
{
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 ' ;
121
154
}
122
155
123
- return $ trackId ;
156
+ return self :: __callStatic ( $ name , $ arguments ) ;
124
157
}
125
-
126
158
}
0 commit comments