1. The Database Event Listener Code
1. Kode untuk Event Listener Database
This is the core component that listens for every query event. We will place this logic inside a dedicated Service Provider. Ini adalah komponen inti yang akan mendengarkan setiap event kueri. Kita akan menempatkan logika ini di dalam sebuah Service Provider khusus. A. Create the Service Provider / Buat Service Provider Run this command in your terminal: Jalankan perintah ini di terminal Anda:app/Providers/QueryLoggingServiceProvider.php and paste the following code. This code listens for queries and dispatches a Job to handle the logging, ensuring it doesn’t slow down the user’s request.
Buka file app/Providers/QueryLoggingServiceProvider.php dan salin kode berikut. Kode ini mendengarkan kueri dan mengirimkan sebuah Job untuk menangani proses logging, memastikan proses ini tidak memperlambat permintaan dari pengguna.
config/app.php and add your provider to the providers array.
Buka config/app.php dan tambahkan provider Anda ke dalam array providers.
2. Asynchronous Logging to a Separate MongoDB Instance
2. Logging Asinkron ke Instans MongoDB Terpisah
We will use Laravel’s queue system to process logs in the background. Kita akan menggunakan sistem antrian (queue) Laravel untuk memproses log di latar belakang. A. Configure the Logging Database Connection / Konfigurasi Koneksi Database Logging Inconfig/database.php, add a new connection for your logging database.
Di file config/database.php, tambahkan koneksi baru untuk database logging Anda.
.env file.
Kemudian, tambahkan variabel yang sesuai ke file .env Anda.
app/Jobs/LogQueryToMongo.php and modify it like this:
Buka app/Jobs/LogQueryToMongo.php dan ubah menjadi seperti ini:
3. Setting up the Queue Driver (Alternative to Redis)
3. Mengatur Driver Queue (Alternatif Selain Redis)
Since you don’t have Redis set up yet, the easiest and quickest alternative is to use your existing database (MySQL, PostgreSQL, or even another MongoDB collection) to manage the queue jobs. Karena Anda belum memiliki Redis, alternatif termudah dan tercepat adalah menggunakan database Anda yang sudah ada (MySQL, PostgreSQL, atau bahkan koleksi MongoDB lain) untuk mengelola antrian job. We will use thedatabase queue driver.
Kita akan menggunakan driver queue database.
A. Create the Queue Tables / Buat Tabel untuk Queue
Laravel needs tables to keep track of jobs. Run these two commands:
Laravel membutuhkan tabel untuk mencatat job. Jalankan dua perintah ini:
jobs and failed_jobs tables in your default database connection.
Perintah ini akan membuat tabel jobs dan failed_jobs di koneksi database default Anda.
B. Configure the Queue Driver / Konfigurasi Driver Queue
In your .env file, change the QUEUE_CONNECTION from sync to database.
Di file .env Anda, ubah QUEUE_CONNECTION dari sync menjadi database.
Summary of the Workflow
Ringkasan Alur Kerja
- A user visits a page on your Laravel site. Seorang pengguna mengunjungi halaman di situs Laravel Anda.
- Your application code (e.g., a model) executes a database query. Kode aplikasi Anda (misalnya, sebuah model) mengeksekusi kueri database.
- The
DB::listenevent in yourQueryLoggingServiceProviderautomatically catches this query. EventDB::listendi dalamQueryLoggingServiceProviderAnda secara otomatis menangkap kueri ini. - The listener immediately dispatches
LogQueryToMongojob onto the queue (a new row in thejobstable). This is extremely fast. Listener segera mengirimkan jobLogQueryToMongoke dalam antrian (baris baru di tabeljobs). Proses ini sangat cepat. - The controller finishes its work and sends the response back to the user without any delay. Controller menyelesaikan tugasnya dan mengirimkan respons kembali ke pengguna tanpa penundaan.
- In the background, the
php artisan queue:workprocess sees the new job, picks it up, and executes itshandle()method. Di latar belakang, prosesphp artisan queue:workmelihat job baru, mengambilnya, dan mengeksekusi methodhandle()-nya. - The job connects to the separate
mongodb_loggingdatabase and inserts the query details. Job tersebut terhubung ke database terpisahmongodb_loggingdan memasukkan detail kueri.
