The Laravel framework uses monolog_mysql to implement the method of saving system log information to mysql database

  • 2021-10-27 06:46:41
  • OfStack

In this paper, the Laravel framework uses monolog_mysql to save system log information to mysql database. Share it for your reference, as follows:

monolog_mysql is used in Laravel to save system log information to mysql database

Source code reference: https://github.com/markhilton/monolog-mysql

1. Install Installation

In the file root directory:


composer require markhilton/monolog-mysql

Found an extra line for "require" in the composer. json file:


"markhilton/monolog-mysql": "^0.1.6",

If an error check is performed for php versions and laravel (5.5 +) versions,

Or you can paste it manually "markhilton/monolog-mysql": "^0.1.6" Go to composer. json,

Then execute:


compser update

Can achieve the same effect

After success, it is found that there is one more folder of markhilton in vendor

2. Insert 'providers' in config/app. php

'providers' => array( // ... Logger\Laravel\Provider\MonologMysqlHandlerServiceProvider::class,);

3. Generate database files


Publish config using Laravel Artisan CLI.
php artisan vendor:publish

After executing the above statement, it is found that there is an extra file of create_logs_table in database/migration

Rename the file to 2018_03_14_034420_create_logs_table (1 must be a file name in this format to perform database migration)

You can customize the database table name. The default is logs. Here I change it to sys_log

Then execute the following statement:

4. Database migration Migrate tables.


php artisan migrate

Generated 1 table for sys_log

5. Application Integration Application Integration

Insert in bootstrap/app. php


$app->configureMonologUsing(function($monolog) use($app) { $monolog->pushHandler(new Logger\Monolog\Handler\MysqlHandler());});

6. Configure the environment Environment configuration

In the configuration file of. env (the type of database connection where the log is saved, and the name of the table where the log is saved)


DB_LOG_TABLE=sys_log // Name of the database table where the log is saved 
DB_LOG_CONNECTION=mysql // Type of database connection to save logs 

7. Modifications

The core file location for inserting logs into the database is:
/vendor/markhilton/monolog-mysql/src/Logger/Monolog/Handler/MysqlHandler.php

Change the custom table name to sys_log


<?php
namespace Logger\Monolog\Handler;
use DB;
use Illuminate\Support\Facades\Auth;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
class MysqlHandler extends AbstractProcessingHandler
{
protected $table;
protected $connection;
public function __construct($level = Logger::DEBUG, $bubble = true)
{
$this->table = env('DB_LOG_TABLE', 'sys_log');
$this->connection = env('DB_LOG_CONNECTION', env('DB_CONNECTION', 'mysql'));
parent::__construct($level, $bubble);
}
protected function write(array $record)
{
$data = [
'instance' => gethostname(),
'message' => $record['message'],
'channel' => $record['channel'],
'level' => $record['level'],
'level_name' => $record['level_name'],
'context' => json_encode($record['context']),
'remote_addr' => isset($_SERVER['REMOTE_ADDR']) ? ip2long($_SERVER['REMOTE_ADDR']) : null,
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null,
'created_by' => Auth::id() > 0 ? Auth::id() : null,
'created_at' => $record['datetime']->format('Y-m-d H:i:s')
];
DB::connection($this->connection)->table($this->table)->insert($data);
}
}

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

Hope that this article is based on the framework of Laravel PHP programming help.


Related articles: