2
2
3
3
namespace Algolia \AlgoliaSearch \Logger ;
4
4
5
- use Algolia \AlgoliaSearch \Exceptions \ AlgoliaException ;
5
+ use Algolia \AlgoliaSearch \Exception \ DiagnosticsException ;
6
6
use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
7
7
use Algolia \AlgoliaSearch \Service \StoreNameFetcher ;
8
8
use Magento \Framework \Exception \NoSuchEntityException ;
@@ -17,14 +17,20 @@ class DiagnosticsLogger
17
17
/** @var array */
18
18
protected const ALGOLIA_TAGS = ['group ' => 'algolia ' ];
19
19
protected const PROFILE_LOG_MESSAGES_DEFAULT = false ;
20
+ protected const DEFAULT_NAMESPACE_DEPTH = 2 ;
21
+
20
22
protected bool $ isLoggerEnabled = false ;
21
23
protected bool $ isProfilerEnabled = false ;
22
24
25
+ /** @var string[] */
26
+ private array $ timerStack = [];
27
+
23
28
public function __construct (
24
29
protected ConfigHelper $ config ,
25
30
protected TimedLogger $ logger ,
26
31
protected StoreNameFetcher $ storeNameFetcher
27
- ) {
32
+ )
33
+ {
28
34
$ this ->isLoggerEnabled = $ this ->config ->isLoggingEnabled ();
29
35
$ this ->isProfilerEnabled = $ this ->config ->isProfilerEnabled ();
30
36
}
@@ -63,7 +69,7 @@ public function start(string $action, bool $profileMethod = self::PROFILE_LOG_ME
63
69
64
70
65
71
/**
66
- * @throws AlgoliaException
72
+ * @throws DiagnosticsException
67
73
*/
68
74
public function stop (string $ action , bool $ profileMethod = self ::PROFILE_LOG_MESSAGES_DEFAULT ): void
69
75
{
@@ -83,13 +89,26 @@ public function startProfiling(string $timerName): void
83
89
{
84
90
if (!$ this ->isProfilerEnabled ) return ;
85
91
92
+ $ timerName = $ this ->simplifyMethodName ($ timerName );
93
+ $ this ->timerStack [] = $ timerName ;
94
+
86
95
Profiler::start ($ timerName , self ::ALGOLIA_TAGS );
87
96
}
88
97
98
+ /**
99
+ * @throws DiagnosticsException
100
+ */
89
101
public function stopProfiling (string $ timerName ): void
90
102
{
91
103
if (!$ this ->isProfilerEnabled ) return ;
92
104
105
+ $ lastTimerName = array_pop ($ this ->timerStack );
106
+ $ timerName = $ this ->simplifyMethodName ($ timerName );
107
+
108
+ if ($ lastTimerName !== $ timerName ) {
109
+ throw new DiagnosticsException (__ ("Profiling on %1 was stopped before nested operation %2 completed. " , $ timerName , $ lastTimerName ));
110
+ }
111
+
93
112
Profiler::setDefaultTags (self ::ALGOLIA_TAGS );
94
113
Profiler::stop ($ timerName );
95
114
Profiler::setDefaultTags ([]);
@@ -102,7 +121,8 @@ public function log(string $message, int $logLevel = Logger::INFO): void
102
121
}
103
122
}
104
123
105
- public function error (string $ message ): void {
124
+ public function error (string $ message ): void
125
+ {
106
126
if ($ this ->isLoggerEnabled ) {
107
127
$ this ->logger ->log ($ message , Logger::ERROR );
108
128
}
@@ -111,6 +131,7 @@ public function error(string $message): void {
111
131
/**
112
132
* Gets the name of the method that called the diagnostics
113
133
*
134
+ * @param int $level
114
135
* @return string|null
115
136
*/
116
137
protected function getCallingMethodName (int $ level = 2 ): ?string
@@ -120,4 +141,21 @@ protected function getCallingMethodName(int $level = 2): ?string
120
141
? $ backtrace [$ level ]['class ' ] . ":: " . $ backtrace [$ level ]['function ' ]
121
142
: null ;
122
143
}
144
+
145
+ protected function simplifyMethodName (string $ methodName , int $ namespaceDepth = self ::DEFAULT_NAMESPACE_DEPTH ): string
146
+ {
147
+ $ separator = '\\' ;
148
+ $ parts = explode ($ separator , $ methodName );
149
+
150
+ if (count ($ parts ) <= abs ($ namespaceDepth )) {
151
+ return $ methodName ;
152
+ }
153
+
154
+ $ simplified = implode ($ separator , array_slice ($ parts , $ namespaceDepth ));
155
+ if (!count ($ this ->timerStack )) {
156
+ $ simplified = "ALGOLIA: " . $ simplified ;
157
+ }
158
+ return $ simplified ;
159
+ }
160
+
123
161
}
0 commit comments