Skip to content

Commit fb6c8cc

Browse files
committed
feat: add support for laravel-telescope
1 parent dfd3a4c commit fb6c8cc

File tree

12 files changed

+457
-51
lines changed

12 files changed

+457
-51
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"phpunit/phpunit": "^10.5",
4848
"roave/security-advisories": "dev-latest",
4949
"wayofdev/cs-fixer-config": "^1.2",
50-
"beberlei/assert": "^3.3"
50+
"beberlei/assert": "^3.3",
51+
"laravel/telescope": "^5.0"
5152
},
5253
"autoload": {
5354
"psr-4": {

composer.lock

Lines changed: 117 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/cycle.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,27 @@
104104
/*
105105
* Setup sqlite database in-memory for testing purposes
106106
*/
107-
'sqlite' => new Config\SQLiteDriverConfig(
107+
'memory' => new Config\SQLiteDriverConfig(
108108
connection: new Config\SQLite\MemoryConnectionConfig(),
109109
queryCache: true
110110
),
111111

112+
'sqlite' => new Config\SQLiteDriverConfig(
113+
connection: new Config\SQLite\FileConnectionConfig(
114+
database: env('DB_DATABASE', database_path('database.sqlite'))
115+
),
116+
driver: Driver\SQLite\SQLiteDriver::class,
117+
reconnect: true,
118+
timezone: 'UTC',
119+
queryCache: true,
120+
readonlySchema: false,
121+
readonly: false,
122+
options: [
123+
'logInterpolatedQueries' => env('DB_USE_TELESCOPE_LOGGER', false),
124+
'logQueryParameters' => env('DB_USE_TELESCOPE_LOGGER', false),
125+
],
126+
),
127+
112128
'pgsql' => new Config\PostgresDriverConfig(
113129
connection: new Config\Postgres\TcpConnectionConfig(
114130
database: env('DB_DATABASE', 'wod'),
@@ -121,7 +137,13 @@
121137
driver: Driver\Postgres\PostgresDriver::class,
122138
reconnect: true,
123139
timezone: 'UTC',
124-
queryCache: true
140+
queryCache: true,
141+
readonlySchema: false,
142+
readonly: false,
143+
options: [
144+
'logInterpolatedQueries' => env('DB_USE_TELESCOPE_LOGGER', false),
145+
'logQueryParameters' => env('DB_USE_TELESCOPE_LOGGER', false),
146+
],
125147
),
126148

127149
'mysql' => new Config\MySQLDriverConfig(
@@ -136,6 +158,12 @@
136158
reconnect: true,
137159
timezone: 'UTC',
138160
queryCache: true,
161+
readonlySchema: false,
162+
readonly: false,
163+
options: [
164+
'logInterpolatedQueries' => env('DB_USE_TELESCOPE_LOGGER', false),
165+
'logQueryParameters' => env('DB_USE_TELESCOPE_LOGGER', false),
166+
],
139167
),
140168

141169
'sqlserver' => new Config\SQLServerDriverConfig(
@@ -150,6 +178,12 @@
150178
reconnect: true,
151179
timezone: 'UTC',
152180
queryCache: true,
181+
readonlySchema: false,
182+
readonly: false,
183+
options: [
184+
'logInterpolatedQueries' => env('DB_USE_TELESCOPE_LOGGER', false),
185+
'logQueryParameters' => env('DB_USE_TELESCOPE_LOGGER', false),
186+
],
153187
),
154188
],
155189

@@ -159,7 +193,23 @@
159193
* Use any of channels configured in your logging.php file
160194
*/
161195
'logger' => [
196+
/*
197+
* If enabled, allows laravel/telescope to catch all queries
198+
*/
199+
'use_telescope' => env('DB_USE_TELESCOPE_LOGGER', false),
200+
201+
/*
202+
* Default logger channel to use with all databases
203+
* Set to null to disable logging
204+
* Use any of channels configured in your logging.php file
205+
*/
162206
'default' => env('DB_DEFAULT_LOGGER', null),
207+
208+
/*
209+
* Logger channels for specific drivers
210+
* Set to null to disable logging
211+
* Use any of channels configured in your logging.php file
212+
*/
163213
'drivers' => [
164214
'sqlite' => env('DB_DEFAULT_LOGGER', null),
165215
'pgsql' => env('DB_DEFAULT_LOGGER', null),

docs/pages/getting-started/configuration.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ These variables offer further customization for CycleORM's operation within Lara
7474
export const extraEnv = [
7575
["DB_DEFAULT_CONNECTION", "string", "default", "The default database connection."],
7676
["DB_DEFAULT_LOGGER", "?string", "null", "Laravel logger channel, to log queries. Loggers can be added in `config/logging.php`"],
77+
["DB_USE_TELESCOPE_LOGGER", "boolean", "false", "When enabled, `QueryExecuted` event will be fired through `TelescopeLogger` class."],
7778
["DB_MIGRATIONS_TABLE", "string", "cycle_migrations", "The table name for the migrations."],
7879
["CYCLE_TOKENIZER_CACHE_TARGETS", "boolean", "true", "Determines whether to cache located classes utilizing annotations."],
7980
["CYCLE_TOKENIZER_LOAD_CLASSES", "boolean", "true", "Controls whether files of type `class` should be loaded."],

docs/pages/services/_meta.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"factories": "Factories",
3+
"seeders": "Seeders",
4+
"testing": "Testing",
5+
"validation": "Validation",
6+
"laravel-telescope": "Laravel Telescope"
7+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import ExternalLink from "../../components/external-link";
2+
import Image from 'next/image'
3+
4+
# Laravel Telescope
5+
6+
## 🗒️ Introduction
7+
8+
<ExternalLink href="https://laravel.com/docs/11.x/telescope">Laravel Telescope</ExternalLink> is an elegant debug assistant for the Laravel framework. Telescope provides insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, mail, notifications, cache operations, scheduled tasks, variable dumps and more. Telescope makes a wonderful companion to your local Laravel development environment.
9+
10+
This adapter package provides you with abillity to also log and debug your Cycle ORM queries in Laravel Telescope.
11+
12+
## 📀 Installation
13+
14+
<div className="steps-container">
15+
16+
### Step 1: Install Telescope
17+
18+
Install, telescope as usual, via Composer package manager:
19+
20+
```bash
21+
composer require laravel/telescope
22+
```
23+
24+
### Step 2: Publish Telescope Assets and run Migrations
25+
26+
After installing Telescope, publish its assets and migrations using the telescope:install Artisan command. After installing Telescope, you should also run the migrate command in order to create the tables needed to store Telescope's data:
27+
28+
```bash
29+
php artisan telescope:install
30+
31+
php artisan migrate
32+
```
33+
34+
### Step 3: Add the CycleORM Query Watcher
35+
36+
Next, edit your `config/telescope.php` configuration file and add the following lines to the `watchers` array, right after the default`Watchers\QueryWatcher::class` line:
37+
38+
```php {13-18} filename="config/telescope.php"
39+
return [
40+
// ...
41+
42+
'watchers' => [
43+
// ...
44+
Watchers\QueryWatcher::class => [
45+
'enabled' => true,
46+
'ignore_packages' => true,
47+
'ignore_paths' => [],
48+
'slow' => 100,
49+
],
50+
51+
WayOfDev\Cycle\Bridge\Telescope\Watchers\QueryWatcher::class => [
52+
'enabled' => true,
53+
'ignore_packages' => true,
54+
'ignore_paths' => [],
55+
'slow' => 100,
56+
],
57+
],
58+
// ...
59+
];
60+
```
61+
62+
### Step 4: Add .env Configuration
63+
64+
Add the following configuration to your `.env` file:
65+
66+
```dotenv filename=".env"
67+
...
68+
69+
DB_USE_TELESCOPE_LOGGER=true
70+
71+
...
72+
```
73+
74+
### Step 5: Access Laravel Telescope
75+
76+
Finally, you may access the Telescope dashboard via the `/telescope` route. Of course, don't forget to start your Laravel application:
77+
78+
```bash
79+
php artisan serve
80+
```
81+
</div>
82+
83+
## 🎉 Final Result
84+
85+
Now, you can access the Laravel Telescope dashboard and see all the CycleORM queries executed by your application.
86+
87+
<Image src="/images/telescope-min.png" alt="Hello" width={1600} height={1483} />
177 KB
Loading

src/Bridge/Laravel/LoggerFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Illuminate\Log\LogManager;
1212
use Psr\Log\LoggerInterface;
1313
use Psr\Log\NullLogger;
14+
use WayOfDev\Cycle\Bridge\Telescope\TelescopeLogger;
15+
use WayOfDev\Cycle\Support\Arr;
1416

1517
final class LoggerFactory implements LoggerFactoryInterface
1618
{
@@ -39,6 +41,12 @@ public function getLogger(?DriverInterface $driver = null): LoggerInterface
3941
}
4042
}
4143

42-
return $this->manager->channel($channel);
44+
$logger = $this->manager->channel($channel);
45+
46+
if (Arr::get($this->config, 'use_telescope') === true) {
47+
return new TelescopeLogger($logger);
48+
}
49+
50+
return $logger;
4351
}
4452
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Telescope\Events\Database;
6+
7+
/**
8+
* @see https://github.com/laravel/framework/blob/11.x/src/Illuminate/Database/Connection.php
9+
* @see https://github.com/laravel/framework/blob/11.x/src/Illuminate/Database/Events/QueryExecuted.php
10+
*/
11+
final class QueryExecuted
12+
{
13+
public function __construct(public string $sql, public array $bindings, public ?float $time, public ?string $driver = null)
14+
{
15+
$this->time = $time * 1000;
16+
$this->driver = null !== $driver ? 'CycleORM/' . $driver : 'CycleORM';
17+
}
18+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Telescope;
6+
7+
use Psr\Log\LoggerInterface;
8+
use Stringable;
9+
use WayOfDev\Cycle\Bridge\Telescope\Events\Database\QueryExecuted;
10+
11+
class TelescopeLogger implements LoggerInterface
12+
{
13+
private LoggerInterface $parentLogger;
14+
15+
public function __construct($parentLogger)
16+
{
17+
$this->parentLogger = $parentLogger;
18+
}
19+
20+
public function log($level, $message, array $context = []): void
21+
{
22+
$this->parentLogger->log($level, $message, $context);
23+
24+
if ('info' === $level && isset($context['elapsed'])) {
25+
event(
26+
new QueryExecuted(
27+
sql: $message,
28+
bindings: $context['parameters'] ?? [],
29+
time: $context['elapsed'],
30+
driver: $context['driver'] ?? null
31+
)
32+
);
33+
}
34+
}
35+
36+
public function info(Stringable|string $message, array $context = []): void
37+
{
38+
$this->log('info', $message, $context);
39+
}
40+
41+
public function error(Stringable|string $message, array $context = []): void
42+
{
43+
$this->log('error', $message, $context);
44+
}
45+
46+
public function alert(Stringable|string $message, array $context = []): void
47+
{
48+
$this->log('alert', $message, $context);
49+
}
50+
51+
public function emergency(Stringable|string $message, array $context = []): void
52+
{
53+
$this->log('emergency', $message, $context);
54+
}
55+
56+
public function critical(Stringable|string $message, array $context = []): void
57+
{
58+
$this->log('emergency', $message, $context);
59+
}
60+
61+
public function warning(Stringable|string $message, array $context = []): void
62+
{
63+
$this->log('warning', $message, $context);
64+
}
65+
66+
public function notice(Stringable|string $message, array $context = []): void
67+
{
68+
$this->log('notice', $message, $context);
69+
}
70+
71+
public function debug(Stringable|string $message, array $context = []): void
72+
{
73+
$this->log('debug', $message, $context);
74+
}
75+
}

0 commit comments

Comments
 (0)