Skip to content

Commit e258529

Browse files
committed
ADD more info in log: method Name caller, arguments, stackTrace.
1 parent 00ebbee commit e258529

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ QUERYMONITOR_BUILDER_ATTIVA=true
77
#in milliseconds
88
QUERYMONITOR_BUILDER_MAX_EXECUTION_TIME=200
99
QUERYMONITOR_BUILDER_METHOD_REGEX='^.*$'
10+
11+
QUERYMONITOR_BUILDER_MAX_STACK_DEPTH=5

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,30 @@ Once installed and configured, the package will automatically monitor and log SQ
7777

7878
**Example Log Entry:**
7979

80-
```bash
81-
[2024-11-28 12:34:56] local.INFO: QueryMonitor: Slow Eloquent method detected {"method":"get","execution_time":"250 ms"}
80+
```json
81+
{
82+
"method": "get",
83+
"execution_time": "250 ms",
84+
"query": "SELECT * FROM `articles` WHERE `status` = 'published'",
85+
"arguments": [
86+
["*"]
87+
],
88+
"stack_trace": [
89+
{
90+
"file": "/path/to/your/project/app/Models/QueryBase.php",
91+
"line": 123,
92+
"class": "App\\Models\\QueryBase",
93+
"function": "getPublishedArticles"
94+
},
95+
{
96+
"file": "/path/to/your/project/app/Http/Controllers/ArticleController.php",
97+
"line": 45,
98+
"class": "App\\Http\\Controllers\\ArticleController",
99+
"function": "index"
100+
},
101+
// ... additional frames up to the maximum stack depth
102+
]
103+
}
82104
```
83105

84106
## Difference Between Monitoring SQL Queries and Eloquent Methods

config/querymonitor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@
7878
'methodRegEx' => env('QUERYMONITOR_BUILDER_METHOD_REGEX', '^.*$'),
7979
],
8080

81+
/*
82+
|--------------------------------------------------------------------------
83+
| Miscellaneous Settings
84+
|--------------------------------------------------------------------------
85+
|
86+
*/
87+
88+
/*
89+
* Maximum stack trace depth to include in the logs.
90+
* Set to 0 to disable stack trace logging.
91+
*/
92+
'maxStackDepth' => env('QUERYMONITOR_BUILDER_MAX_STACK_DEPTH', 5),
8193
];

src/Builders/QueryMonitorBuilder.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,44 @@ private function executeWithTiming(string $methodName, array $arguments)
6565
$sql = preg_replace('/\?/', $value, $sql, 1);
6666
}
6767

68+
// Capture the call stack up to the maximum depth
69+
$maxStackDepth = $builderConfig['maxStackDepth'] ?? 5;
70+
$stackTrace = $this->getStackTrace($maxStackDepth);
71+
6872
// Log the slow Eloquent method execution
6973
Log::info('QueryMonitor: Slow Eloquent method detected', [
7074
'method' => $methodName,
71-
'execution_time' => $executionTime . ' ms',
75+
'arguments' => $arguments,
7276
'query' => $sql,
77+
'execution_time' => $executionTime . ' ms',
78+
'stack_trace' => $stackTrace,
7379
]);
7480

7581
// Return the result of the parent method
7682
return $results;
7783
}
7884

85+
/**
86+
* Captures the stack trace up to the specified depth.
87+
*/
88+
private function getStackTrace(int $maxStackDepth): array
89+
{
90+
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $maxStackDepth + 1);
91+
$stackTrace = [];
92+
93+
for ($i = 1; $i <= $maxStackDepth && isset($backtrace[$i]); $i++) {
94+
$frame = $backtrace[$i];
95+
$stackTrace[] = [
96+
'file' => $frame['file'] ?? '',
97+
'line' => $frame['line'] ?? '',
98+
'class' => $frame['class'] ?? '',
99+
'function' => $frame['function'] ?? '',
100+
];
101+
}
102+
103+
return $stackTrace;
104+
}
105+
79106
/**
80107
* Overrides the get method to include performance monitoring.
81108
*
@@ -126,10 +153,10 @@ public function pluck($column, $key = null)
126153
/**
127154
* Overrides the paginate method to include performance monitoring.
128155
*
129-
* @param int|null $perPage Items per page.
130-
* @param array|string $columns The columns to retrieve.
131-
* @param string $pageName The page query string parameter.
132-
* @param int|null $page The current page.
156+
* @param int|null $perPage Items per page.
157+
* @param array|string $columns The columns to retrieve.
158+
* @param string $pageName The page query string parameter.
159+
* @param int|null $page The current page.
133160
* @return LengthAwarePaginator The paginator instance.
134161
*/
135162
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

0 commit comments

Comments
 (0)