SoFunction
Updated on 2025-03-09

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

This article describes the method of using monolog_mysql to save system log information to mysql database. Share it for your reference, as follows:

Use monolog_mysql in Laravel to save system log information to mysql database

Source code reference:/markhilton/monolog-mysql

1. Install Installation

In the file root directory:

composer require markhilton/monolog-mysql

I found that there was an extra line of "require" in the file:

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

If you perform an error check for php version and laravel (5.5 or above) version,

Or you can paste it manually"markhilton/monolog-mysql": "^0.1.6"arriveGo up,

Then execute:

compser update

Can achieve the same effect

After success, I found that there was an extra markhilton folder in vendor

2. Add in 'providers' in config/

Copy the codeThe code is as follows:
'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 was founddatabase/migrationOne more in itcreate_logs_tableFiles

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

You can customize the database table name, default is logs, here I change it to sys_log

Then execute the following statement:

4. Database migration Migrate tables.

php artisan migrate

A sys_log table was generated

5. Application Integration

Add in bootstrap/

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

6. Configuration Environment configuration

Added in the .env configuration file (the database connection type that saves the log, and the table name that saves the log)

DB_LOG_TABLE=sys_log //The database table name that saves the logDB_LOG_CONNECTION=mysql //The database connection type that saves the log

7. Modification

The core file location where the logs are inserted into the database is:
/vendor/markhilton/monolog-mysql/src/Logger/Monolog/Handler/

Modify 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 information about Laravel, readers who are interested in this site can view the topic:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.