PHPackages                             jiannei/laravel-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. jiannei/laravel-logger

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

jiannei/laravel-logger
======================

Laravel logger.

v1.3.0(4y ago)264.9k↓44.4%2[2 issues](https://github.com/jiannei/laravel-logger/issues)MITPHP

Since Nov 30Pushed 2y ago1 watchersCompare

[ Source](https://github.com/jiannei/laravel-logger)[ Packagist](https://packagist.org/packages/jiannei/laravel-logger)[ RSS](/packages/jiannei-laravel-logger/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (8)Dependencies (3)Versions (9)Used By (0)

 laravel-logger
================

[](#-laravel-logger-)

[![Test](https://github.com/Jiannei/laravel-logger/workflows/Test/badge.svg)](https://github.com/Jiannei/laravel-logger/workflows/Test/badge.svg)[![StyleCI](https://camo.githubusercontent.com/adc0e0987db8e30bbb3a4011ba6115a84639aecc2b3873ace86b49b4c823d262/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3331373134343638302f736869656c643f6272616e63683d6d61696e)](https://github.styleci.io/repos/317144680?branch=main)[![Latest Stable Version](https://camo.githubusercontent.com/ec91d58d3183feb0cabcd717f4bed0c0ae8b068eb17e841e49c14828ccad1080/687474703a2f2f706f7365722e707567782e6f72672f6a69616e6e65692f6c61726176656c2d6c6f676765722f76)](https://packagist.org/packages/jiannei/laravel-logger)[![Total Downloads](https://camo.githubusercontent.com/0d4309c63e4ae19e811758fc6459900d5283c665f19540cde081617c946ddfda/687474703a2f2f706f7365722e707567782e6f72672f6a69616e6e65692f6c61726176656c2d6c6f676765722f646f776e6c6f616473)](https://packagist.org/packages/jiannei/laravel-logger)[![Monthly Downloads](https://camo.githubusercontent.com/84cff62803dc6b12757acb944fffd3b1b9f8edb5b7825adf392000f68cfe2efe/687474703a2f2f706f7365722e707567782e6f72672f6a69616e6e65692f6c61726176656c2d6c6f676765722f642f6d6f6e74686c79)](https://packagist.org/packages/jiannei/laravel-logger)[![Latest Unstable Version](https://camo.githubusercontent.com/272a2b5a159d37ae2edbc87f985b0f55623968b2f46a81e7e886904144f46601/687474703a2f2f706f7365722e707567782e6f72672f6a69616e6e65692f6c61726176656c2d6c6f676765722f762f756e737461626c65)](https://packagist.org/packages/jiannei/laravel-logger)[![License](https://camo.githubusercontent.com/f3c15e48bc8795b38202e3a53eeef9f7b5a1369e219415c9fcaf6d21bc8fc2e5/687474703a2f2f706f7365722e707567782e6f72672f6a69616e6e65692f6c61726176656c2d6c6f676765722f6c6963656e7365)](https://packagist.org/packages/jiannei/laravel-logger)

社区讨论文章
------

[](#社区讨论文章)

- [是时候使用 Lumen 8 + API Resource 开发项目了！](https://learnku.com/articles/45311)
- [教你更优雅地写 API 之「路由设计」](https://learnku.com/articles/45526)
- [教你更优雅地写 API 之「规范响应数据」](https://learnku.com/articles/52784)
- [教你更优雅地写 API 之「枚举使用」](https://learnku.com/articles/53015)
- [教你更优雅地写 API 之「记录日志」](https://learnku.com/articles/53669)
- [教你更优雅地写 API 之「灵活地任务调度」](https://learnku.com/articles/58403)

介绍
--

[](#介绍)

`laravel-logger` 主要用来扩展项目中的日志记录，使调试更加方便。

概览
--

[](#概览)

- 提供 `logger_async` 辅助函数，通过异步 Job 方式来记录日志；
- 增加 RequestLog 中间件来记录 api 的请求和响应；对于单个请求关联 `UNIQUE_ID`，根据`UNIQUE_ID`可以跟踪请求执行过程
- 适配 MongoDB 驱动，支持记录日志到 MongoDB；collection 支持按天、按月和按年拆分；(依赖 `mongodb/mongodb`或`jenssegers/mongodb`)
- 提供 `LOG_QUERY`、`LOG_REQUEST` 配置参数来开启关闭 sql 日志和 request 日志

安装
--

[](#安装)

```
$ composer require jiannei/laravel-logger -vvv
```

配置
--

[](#配置)

复制配置项到 `config/logging.php`中，参考：

### Laravel

[](#laravel)

- 添加中间件 RequestLog 来记录 API 请求日志

在 `app/Http/Kernel.php` 的 $middlewareGroups 中添加

```
protected $middlewareGroups = [
    'api' => [
        \Jiannei\Logger\Laravel\Http\Middleware\RequestLog::class,// 加在这个地方
    ],
];
```

-

### Lumen

[](#lumen)

- 加载配置

```
// bootstrap/app.php
$app->configure('logging');
```

- 添加中间件

```
$app->middleware([
    \Jiannei\Logger\Laravel\Http\Middleware\RequestLog::class,
]);
```

- 注册服务容器

```
$app->register(\Jiannei\Logger\Laravel\Providers\ServiceProvider::class);
```

### .env 中配置启用

[](#env-中配置启用)

```
LOG_CHANNEL=mongo
LOG_SLACK_WEBHOOK_URL=
LOG_QUERY=true
LOG_REQUEST=true

# 如果使用的是 mongo channel 需要配置
LOG_MONGODB_SEPARATE=daily
LOG_MONGODB_LEVEL=debug
LOG_MONGODB_HOST=127.0.0.1
LOG_MONGODB_PORT=27017
LOG_MONGODB_DATABASE=logs
LOG_MONGODB_USERNAME=
LOG_MONGODB_PASSWORD=
LOG_MONGODB_AUTHENTICATION_DATABASE=admin
```

### 其他

[](#其他)

如果需要记录日志到 MongoDB，需要先安装并配置[laravel-mongodb](https://github.com/jenssegers/laravel-mongodb)

如何使用
----

[](#如何使用)

可以参考 [lumen-api-starter](https://github.com/Jiannei/lumen-api-starter) 中的实际使用示例。

### 使用

[](#使用)

- `app/Repositories/Enums/LogEnum.php` 中定义记录日志时的 message
- 通过 logger\_async 方法记录日志

```
logger_async(LogEnum::SYSTEM_SQL, $arrayData);
```

- 如果队列任务异步执行，则需要开启队列消费 `php artisan queue:work`
- 记录到文件中的日志内容

```
[2021-01-18 12:03:36] local.DEBUG: System sql {"database":"lumen-api","duration":"11.08ms","sql":"select `roles`.*, `model_has_roles`.`model_id` as `pivot_model_id`, `model_has_roles`.`role_id` as `pivot_role_id`, `model_has_roles`.`model_type` as `pivot_model_type` from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `model_has_roles`.`model_id` = '11' and `model_has_roles`.`model_type` = 'App\\\\Repositories\\\\Models\\\\User'"} {"url":"/users","ip":"172.22.0.1","http_method":"get","server":"lumen-api.test","referrer":null,"unique_id":"43f54ea9-4ad4-47cf-b9da-1d3aa150ff61"}
[2021-01-18 12:03:36] local.DEBUG: System request {"request":[],"response":{"status":"success","code":200,"message":"操作成功","data":{"data":[{"id":1,"nickname":"Evert Stracke DVM","email":"aufderhar.kaden@example.net"},{"id":2,"nickname":"Milton Toy","email":"keagan.eichmann@example.org"},{"id":3,"nickname":"Mrs. Alyce O'Hara","email":"cartwright.sidney@example.org"},{"id":4,"nickname":"Prof. Evalyn Windler I","email":"bertram.bartoletti@example.org"},{"id":5,"nickname":"Brant Skiles","email":"jane16@example.net"},{"id":6,"nickname":"Sage Rodriguez I","email":"ryder50@example.org"},{"id":7,"nickname":"Ms. Angelica Wiegand DVM","email":"kaelyn.mueller@example.net"},{"id":8,"nickname":"Newton Zieme","email":"sipes.kip@example.com"},{"id":9,"nickname":"Natalia Ruecker","email":"stroman.kiley@example.com"},{"id":10,"nickname":"Hallie Parisian","email":"rosina74@example.net"},{"id":11,"nickname":"Jiannei","email":"longjian.huang@foxmail.com"}],"meta":{"pagination":{"total":11,"count":11,"per_page":15,"current_page":1,"total_pages":1,"links":[]}}},"error":[]},"start":1610942614.450748,"end":1610942615.785696,"duration":"1.33s"} {"url":"/users","ip":"172.22.0.1","http_method":"GET","server":"lumen-api.test","referrer":null,"unique_id":"43f54ea9-4ad4-47cf-b9da-1d3aa150ff61"}

```

- 记录到 Mongodb 的日志内容

```
/* 1 */
{
    "_id" : ObjectId("60050999ee7d025d4c62c8c2"),
    "message" : "System sql",
    "context" : {
        "database" : "lumen-api",
        "duration" : "54.19ms",
        "sql" : "select count(*) as aggregate from `users`"
    },
    "level" : 100,
    "level_name" : "DEBUG",
    "channel" : "mongo",
    "datetime" : ISODate("2021-01-18T12:07:53.410+08:00"),
    "extra" : {
        "url" : "/users",
        "ip" : "172.22.0.1",
        "http_method" : "get",
        "server" : "lumen-api.test",
        "referrer" : null,
        "unique_id" : "0cda1927-bf14-4acf-88e8-1d9ed67170b5"
    }
}

/* 2 */
{
    "_id" : ObjectId("60050999ee7d025d4c62c8c3"),
    "message" : "System sql",
    "context" : {
        "database" : "lumen-api",
        "duration" : "2.42ms",
        "sql" : "select * from `users` limit 15 offset 0"
    },
    "level" : 100,
    "level_name" : "DEBUG",
    "channel" : "mongo",
    "datetime" : ISODate("2021-01-18T12:07:53.500+08:00"),
    "extra" : {
        "url" : "/users",
        "ip" : "172.22.0.1",
        "http_method" : "get",
        "server" : "lumen-api.test",
        "referrer" : null,
        "unique_id" : "0cda1927-bf14-4acf-88e8-1d9ed67170b5"
    }
}

```

特别说明
----

[](#特别说明)

- SQL 日志记录参考：[laravel-query-logger](https://github.com/overtrue/laravel-query-logger)

License
-------

[](#license)

MIT

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

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 ~49 days

Recently: every ~81 days

Total

8

Last Release

1686d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1976f3bc442d9f7db752da3a494a9174132ea498d3338495b1978b1499032b45?d=identicon)[jiannei](/maintainers/jiannei)

---

Top Contributors

[![jiannei](https://avatars.githubusercontent.com/u/46595807?v=4)](https://github.com/jiannei "jiannei (7 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

job-queuelaravel-log-viewerlaravel-loggermongodb-driverrequestloggingsqllog

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B10.9k](/packages/psr-log)[open-telemetry/api

API for OpenTelemetry PHP.

1938.5M261](/packages/open-telemetry-api)[open-telemetry/sdk

SDK for OpenTelemetry PHP.

2326.5M315](/packages/open-telemetry-sdk)[illuminated/console-logger

Logging and Notifications for Laravel Console Commands.

8676.7k](/packages/illuminated-console-logger)

PHPackages © 2026

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