kaoken / laravel-db-email-log
Save logs handled by Laravel in DB, and send mail when it is over specified level.
Installs: 6
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/kaoken/laravel-db-email-log
Requires
- php: >=7.3.0
- ext-json: *
- laravel/framework: v8.77.1
README
Save logs handled by Laravel in Mysql, and send mail when it is over specified level.
Table of content
Install
composer:
composer require kaoken/laravel-db-email-log
or, add composer.json
"require": { ... "kaoken/laravel-db-email-log":"^1.0" }
Setting
Add to config\app.php as follows:
'providers' => [ ... // Add Kaoken\LaravelDBEmailLog\LaravelDBEmailLogServiceProvider::class ],
Add to config\database.php as follows:
'connections' => [ ... 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], // Add (Copy 'mysql' above) 'db_log' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], ...
Copy the above ['connections']['mysql'] and set the driver name to db_log.
This is necessary to prevent the log from being lost due to rollback after writing
the log when doing transaction processing (DB :: transaction, DB :: beginTransaction etc.)
with the driver name mysql.
Add to config\logging.php as follows:
connectionis the driver name for the data. Seeconfig\database.php.modelis a log model.emailsends a mail according toemail_send_levelif it istrue. In case offalse, do not send anything.email_send_levelspecifies the log level and send from the specified log level or higher. Since the priority is low,DEBUG、INFO、NOTICE、WARNING、ERROR、CRITICAL、ALERT、EMERGENCY.Capital letters and lower case letters are not distinguished.email_logshould modify the class derived from Mailable as necessary. Send log mail.email_send_limitshould modify the class derived from Mailable as necessary. Send when e-mail transmission limitmax_email_send_countis exceeded.max_email_send_count, the log e-mail that can be transmitted in one day. A simple warning mail is sent when the number exceeds the number of transmissions. Seeemail_send_level.tois the destination of the mail.
It is good to add it under the 'log_level' => env('APP_LOG_LEVEL', 'debug'), of the config\app.php file.
'default' => env('LOG_CHANNEL', 'db_log'), // Add 'db_log' => [ 'connection' => 'db_log', 'model' => Kaoken\LaravelDBEmailLog\Model\Log::class, 'email' => true, 'email_send_level' => 'ERROR', 'email_log' => Kaoken\LaravelDBEmailLog\Mail\LogMailToAdmin::class, 'email_send_limit' => Kaoken\LaravelDBEmailLog\Mail\SendLimitMailToAdmin::class, 'max_email_send_count' => 64, 'to' => 'hoge@hoge.com' ],
Command
php artisan vendor:publish --tag=db-email-log
After execution, the following directories and files are added.
databasemigrations2021_01_01_000001_create_logs_table.php
resourcesviewsvendormysql_email_loglog.blade.phpover_limit.blade.php
Migration
Migration file 2021_01_01_000001_create_logs_table.php should be modified as necessary.
php artisan migrate
In the configuration config\logging.php of the above setting,
The Kaoken\LaravelDBEmailLog\Mail\ConfirmationMailToUser::class of email_log is used as the log mail of the target level or higher.
The template uses views\vendor\mysql_email_log\log.blade.php. Change according to the specifications of the application.
The Kaoken\LaravelDBEmailLog\Mail\ConfirmationMailToUser::class of email_send_limit is used when the log above the target level reaches the send limit.
The template uses views\vendor\mysql_email_log\over_limit.blade.php. Change according to the specifications of the application.
Event
See inside the vendor\laravel-db-email-log\src\Events directory!
BeforeWriteLogEvent
Called before writing the log.