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

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

fengxinyhyl/logger
==================

一个通用的日志服务

v1.0.6(2y ago)22.9kMITPHPPHP &gt;=5.6

Since Jul 10Pushed 2y agoCompare

[ Source](https://github.com/fengxinyhyl/logger)[ Packagist](https://packagist.org/packages/fengxinyhyl/logger)[ RSS](/packages/fengxinyhyl-logger/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (22)Used By (0)

**logger**
==========

[](#logger)

---

### 安装方式

[](#安装方式)

- 方式一：composer require fengxinyhyl/logger
- 方式二：在composer.json文件的require里添加 "fengxinyhyl/logger":"~0.1"，运行composer update

---

### 项目依赖

[](#项目依赖)

```
"require": {
    "monolog/monolog": "^2.0",
    "ext-redis": "*",
    "php": ">=5.4.0",
    },

```

---

### 配置文件

[](#配置文件)

```
return array(
    /**
     * 当前项目的唯一标识
     * 用来区分日志系统中不同项目的日志
     * 用来创建es索引，不能出现大写字母
     */
    'projectName' => 'user_center',

    /**
     * redis缓存配置
     * 日志传送的redis数据库
     */
    'redisConfig' => array(
        'host' => '192.168.107.107',
        'port' => 6379,
        'select' => 0,
    ),

    /**
     * 短信报警配置
     * 每小时报警次数是alertCondition的整数倍会发送报警短信
     * 每小时至多发送5条
     */
    'smsConfig' => array(
        // 报警短信接收号码必须配置
        'phones' => array(
        ),
        // 发送条件必须为正整数，默认为10，每小时发生错误的数量是报警条件的整数倍时，发送一次报警短信
        'alertCondition' => 10,
    ),

    /**
     * 日志文件目录,使用绝对路径,默认tmp目录
     */
    'logDir' => '/tmp',
);

```

---

### 使用方法

[](#使用方法)

#### 1.日志分类

[](#1日志分类)

- common 常规日志，默认选项
- system 系统日志
- push 访问外部日志
- job 定时任务日志

#### 2.使用方法

[](#2使用方法)

```
$config = require('config.php');
Logger::getLogger()->initLogger($config); // 必须先根据配置初始化日志对象
Logger::getLogger()->info('bbbb');        // getLogger()返回日志单例对象，默认调用commen
Logger::getLogger()->system()->error('bbbb');

```

### 扩展

[](#扩展)

#### 1.BuildParams，添加参数

[](#1buildparams添加参数)

```
// 应用开始
'app_begin'    => [
    'your_path\BuildParams',
],

class BuildParams
{
    /**
     * 将参数添加到日志中
     */
    public function run() {
        $params = Request::instance()->param();
        Logger::getLogger()->system()->getUseAge()->pushProcessor(function ($record) use ($params) {
            $record['extra']['params'] = json_encode($params, JSON_UNESCAPED_UNICODE);
            return $record;
        });
    }

}

```

#### 2.buildResponse，添加返回的数据

[](#2buildresponse添加返回的数据)

```
// 应用结束（TP项目）
'app_end'      => [
    'your_path\BuildResponse',
],

/**
 * BuildResponse.php
 * 将返回数据添加到日志中
 * @param Response $response
 */
public function run(Response $response) {
    $response = $response->getData();
    // 本次访问返回的数据
    $response = is_array($response) ? json_encode($response, JSON_UNESCAPED_UNICODE) : $response;
    // 本次访问运行的时间
    $runTime = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
    if($runTime > 1){ // 时间可根据项目自行配置
        Logger::getLogger()->common()->warn('本次运行时间过长', array('runTime' => $runTime.' s'));
    }
    Logger::getLogger()->system()->getUseAge()->pushProcessor(function ($record) use ($response, $runTime) {
        $record['extra']['runTime'] = $runTime.' s';
        $record['extra']['response'] =  $response;
        return $record;
    });
    Logger::getLogger()->system()->info('success');
}

```

#### 3.CacheException，捕获系统异常，报警依赖此项

[](#3cacheexception捕获系统异常报警依赖此项)

ThinkPHP 异常处理

[Laravel 错误处理](https://laravelacademy.org/post/9548.html)

```
/**
 - 系统级别异常处理文件
 - config配置文件中 'exception_handle' => '\\app\\common\\exception\\Http',定义
 - Class Http
 - @package app\common\exception
 */
class Http extends Handle
{
    public function render(Exception $e)
    {
        //TODO::开发者对异常的操作
        //可以在此交由系统处理
        Logger::getLogger()->critical('file:'.$e->getFile().' line:'. $e->getLine().' msg:'.$e->getMessage());
        return parent::render($e);
    }
}

```

---

### 错误级别定义

[](#错误级别定义)

- 调试/DEBUG (100): 详细的调试信息。
- 信息/INFO (200): 有意义的事件，比如用户登录、SQL日志。
- 提示/NOTICE (250): 正常但是值得注意的事件。
- 警告/WARNING (300): 异常事件，但是并不是错误。比如使用了废弃了的API，错误地使用了一个API，以及其他不希望发生但是并非必要的错误。
- 错误/ERROR (400): 运行时的错误，不需要立即注意到，但是需要被专门记录并监控到。
- 严重/CRITICAL (500): 边界条件/危笃场景。比如应用组件不可用了，未预料到的异常。
- 警报/ALERT (550): 必须立即采取行动。比如整个网站都挂了，数据库不可用了等。这种情况应该发送短信警报，并把你叫醒。
- 紧急/EMERGENCY (600): 紧急请求：系统不可用了。

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity63

Established project with proven stability

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

Recently: every ~36 days

Total

21

Last Release

944d ago

Major Versions

v0.1.11 → v1.0.02023-05-25

PHP version history (2 changes)v0.0.2PHP &gt;=5.4.0

v1.0.0PHP &gt;=5.6

### Community

Maintainers

![](https://www.gravatar.com/avatar/a1cf92b6cd096e9b2f26dd99369c3c83644f0431637709d7786b40d916977d8d?d=identicon)[fengxinyhyl](/maintainers/fengxinyhyl)

### Embed Badge

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

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

###  Alternatives

[symfony/monolog-bridge

Provides integration for Monolog with various Symfony components

2.6k189.7M258](/packages/symfony-monolog-bridge)[rollbar/rollbar

Monitors errors and exceptions and reports them to Rollbar

33723.7M82](/packages/rollbar-rollbar)[illuminate/log

The Illuminate Log package.

6224.3M518](/packages/illuminate-log)[honeybadger-io/honeybadger-php

Honeybadger PHP library

381.5M4](/packages/honeybadger-io-honeybadger-php)[graycore/magento2-stdlogging

A Magento 2 module that changes all logging handlers to stdout

2382.6k](/packages/graycore-magento2-stdlogging)

PHPackages © 2026

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