11<?php
22
3- namespace Tartan \Log ;
3+ namespace PhpMonsters \Log ;
44
5- use Illuminate \Support \Facades \Auth ;
65use Exception ;
6+ use Illuminate \Support \Facades \Auth ;
77
88class 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}
0 commit comments