PHPackages                             yesccx/laravel-database-logger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. yesccx/laravel-database-logger

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

yesccx/laravel-database-logger
==============================

record database execute logs

v1.2.1(3y ago)09.6k1MITPHPPHP &gt;=7.0

Since Dec 8Pushed 3y ago1 watchersCompare

[ Source](https://github.com/yesccx/laravel-database-logger)[ Packagist](https://packagist.org/packages/yesccx/laravel-database-logger)[ RSS](/packages/yesccx-laravel-database-logger/feed)WikiDiscussions 1.x Synced today

READMEChangelogDependencies (2)Versions (7)Used By (0)

Database-Logger
===============

[](#database-logger)

记录数据库MySQL、Mongo等执行日志，支持多数Laravel版本

目录
--

[](#目录)

- [目录](#%E7%9B%AE%E5%BD%95)
- [功能特点](#%E5%8A%9F%E8%83%BD%E7%89%B9%E7%82%B9)
    - [运行环境](#%E8%BF%90%E8%A1%8C%E7%8E%AF%E5%A2%83)
- [开始使用](#%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8)
    - [1. 安装](#1-%E5%AE%89%E8%A3%85)
    - [2. 初始化](#2-%E5%88%9D%E5%A7%8B%E5%8C%96)
    - [3. 配置](#3-%E9%85%8D%E7%BD%AE)
    - [4. 使用](#4-%E4%BD%BF%E7%94%A8)
- [API](#api)
    - [临时禁用日志记录](#%E4%B8%B4%E6%97%B6%E7%A6%81%E7%94%A8%E6%97%A5%E5%BF%97%E8%AE%B0%E5%BD%95)
    - [自定义记录器](#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AE%B0%E5%BD%95%E5%99%A8)
- [功能清单](#%E5%8A%9F%E8%83%BD%E6%B8%85%E5%8D%95)
- [License](#license)

功能特点
----

[](#功能特点)

- 支持多数Laravel版本：`Laravel 5+`、`Laravel 6+`、`Laravel 7+`、`Laravel 8+`、`Laravel 9+`
- 支持`MongoDB`、`MySQL`产生的执行日志
- 可选日志存储驱动`file`、`mysql`，并支持自定义扩展
- 更多API支持

### 运行环境

[](#运行环境)

PHP版本PHP &gt;= 7.0Laravel版本包版本支持状态9.x1.0.x支持8.x1.0.x支持7.x1.0.x支持6.x1.0.x支持5.8.x1.0.x支持5.7.x1.0.x支持5.6.x1.0.x支持开始使用
----

[](#开始使用)

### 1. 安装

[](#1-安装)

```
composer require yesccx/laravel-database-logger:1.*
```

### 2. 初始化

[](#2-初始化)

发布配置文件，ServiceProvider等。

```
> php artisan database-logger:install

# Publishing Service Provider... [app/Provider/DatabaseLoggerProvider.php]
# Publishing Configuration... [config/database-logger.php]
# Database Logger installed successfully.
```

如果需要将日志记录到数据表中，还需要提前初始化对应的`日志记录表`。

```
> php artisan database-logger:migration

# Publishing Migration... [database/migrations/2022_12_06_194505_create_database_logs_table.php]
# Migration created successfully!

> php artisan migrate --path=database/migrations/2022_12_06_194505_create_database_logs_table.php
```

### 3. 配置

[](#3-配置)

从`env`配置中开启记录器，并指定记录器的驱动。

> PS: 如果以数据库驱动，需要先准备好数据表。在初始化时执行相关的迁移文件即可

```
> .env

# 日志记录总开关
DL_ENABLED=true

# 日志记录器 驱动类型
# file - 文件
# mysql - 数据库
DL_LOGGER_DRIVER=file

```

### 4. 使用

[](#4-使用)

- 以 `file` 驱动时日志内容会存储至`storage/logs/dl_sql-*.log`文件中；
- 以 `mysql` 驱动时，日志内容会存储至`database_logs`数据表中。

API
---

[](#api)

### 临时禁用日志记录

[](#临时禁用日志记录)

如果想在某些情况下临时禁用日志记录功能，可以执行以下操作：

```
use Yesccx\DatabaseLogger\Services\DatabaseLoggerUtils;

DatabaseLoggerUtils::withoutLogger(function() {
    // 该闭包内执行的SQL语句将不会产生日志记录
    \App\Models\User::query()->first();
});
```

### 自定义记录器

[](#自定义记录器)

当需要将日志内容以别的方式记录时，可以通过自定义记录器实现

**1. 定义记录器：**

```
namespace App\Services\DatabaseLogger;

use Yesccx\DatabaseLogger\Contracts\LoggerContract;
use Yesccx\DatabaseLogger\Supports\ResolvingResult;

class MongoLogger implements LoggerContract
{
    public function write(ResolvingResult $resolvingResult)
    {
        // 100ms 格式化的执行耗时
        $formatExecuteTime = $resolvingResult->getFormatExecuteTime();

        // 1000000 执行耗时(纳秒)
        $executeTime = $resolvingResult->getExecuteTime();

        // SELECT .... 执行的SQL语句
        $executeSql = $resolvingResult->getExecuteSql();

        // 编写存储逻辑
    }
}
```

**2. 使用自定义记录器：**

```
> .env

# 日志记录器 驱动类型
DL_LOGGER_DRIVER=App\Services\DatabaseLogger\MongoLogger

```

功能清单
----

[](#功能清单)

- 记录数据日志
- 可选数据日志记录的存储驱动(file、mysql)
- 赋于查询时可禁用日志记录功能
- 赋于模型查询时直接打印SQL语句
- 日志记录事件
- 可视化面板查看日志
- 补充更多API
- 单元测试

License
-------

[](#license)

MIT

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Total

5

Last Release

1249d ago

PHP version history (2 changes)v1.0.0PHP &gt;=5.6.4

v1.1.0PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/01e0254020f7dd9bfb9995161a66dfefd7ef247a6478669bb417087ee19c4f0d?d=identicon)[yesccx](/maintainers/yesccx)

---

Tags

loglaravelyesccxdatabase log

### Embed Badge

![Health badge](/badges/yesccx-laravel-database-logger/health.svg)

```
[![Health](https://phpackages.com/badges/yesccx-laravel-database-logger/health.svg)](https://phpackages.com/packages/yesccx-laravel-database-logger)
```

###  Alternatives

[sentry/sentry

PHP SDK for Sentry (http://sentry.io)

1.9k247.1M334](/packages/sentry-sentry)[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

23.9k69.5k](/packages/grumpydictator-firefly-iii)[laravel/nightwatch

The official Laravel Nightwatch package.

36210.1M36](/packages/laravel-nightwatch)[guanguans/laravel-exception-notify

Monitor exception and report to the notification channels(Log、Mail、AnPush、Bark、Chanify、DingTalk、Discord、Gitter、GoogleChat、IGot、Lark、Mattermost、MicrosoftTeams、NowPush、Ntfy、Push、Pushback、PushBullet、PushDeer、PushMe、Pushover、PushPlus、QQ、RocketChat、ServerChan、ShowdocPush、SimplePush、Slack、Telegram、WeWork、WPush、XiZhi、YiFengChuanHua、ZohoCliq、ZohoCliqWebHook、Zulip).

14845.8k1](/packages/guanguans-laravel-exception-notify)[pagemachine/typo3-formlog

Form log for TYPO3

23238.6k8](/packages/pagemachine-typo3-formlog)[nightowl/agent

NightOwl monitoring agent — collects telemetry from laravel/nightwatch and writes to PostgreSQL

771.7k](/packages/nightowl-agent)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
