承接 yii1tech/psr-log 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yii1tech/psr-log

Composer 安装命令:

composer require yii1tech/psr-log

包简介

Provides support for PSR compatible logger for Yii1 application

README 文档

README

Yii1 PSR Log Extension


This extension allows integration with PSR compatible logger for Yii1. Its usage in particular it allows usage of Monolog logger.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii1tech/psr-log

or add

"yii1tech/psr-log": "*"

to the "require" section of your composer.json.

Usage

This extension allows integration with PSR compatible logger for Yii1. It provides several instruments for that. Please choose the one suitable for your particular needs.

Wrap PSR logger into Yii logger

The most common use case for PSR logger involvement into Yii application is usage of 3rd party log library like Monolog. This can be achieved using \yii1tech\psr\log\Logger instance as Yii logger. Its instance should be passed to \Yii::setLogger() before the application instantiation.

Application entry script example:

<?php
// file '/public/index.php'

require __DIR__ . '../vendor/autoload.php';
// ...

// set custom logger:
Yii::setLogger(
    \yii1tech\psr\log\Logger::new()
        ->setPsrLogger(function () {
            // use Monolog as internal PSR logger:
            $log = new \Monolog\Logger('yii');
            $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning));

            return $log;
        })
        ->enableYiiLog(true) // whether to continue passing logs to standard Yii log mechanism or not
);

// create and run Yii application:
Yii::createWebApplication($config)->run();

\yii1tech\psr\log\Logger passes all messages logged via Yii::log() to the related PSR logger, which stores them according to its own internal logic.

Also, usage of \yii1tech\psr\log\Logger grands you several additional benefits:

  • It allows usage of \Psr\log\LogLevel constants for log level specification instead of \CLogger ones.
  • It allows passing log context as array as a 3rd argument of the Yii::log() method, and saving it into Yii logs.

For example:

<?php

use Psr\log\LogLevel;

Yii::log('psr message', LogLevel::INFO, 'psr-category'); // same as `Yii::log('psr message', CLogger::LEVEL_INFO, 'psr-category');` 

Yii::log('context message', LogLevel::INFO, ['category' => 'context-category']); // same as `Yii::log('context message', CLogger::LEVEL_INFO, 'context-category');` 

Yii::log('context message', LogLevel::INFO, [
    'foo' => 'bar', // specifying log context, which will be passed to the related PSR logged, and added as JSON to the Yii log message, if it is enabled 
]);

try {
    // ...
} catch (\Throwable $exception) {
    Yii::log('context exception', LogLevel::ERROR, [
        'exception' => $exception, // exception data such as class, message, file, line and so on will be logged
    ]);
}

You may also specify a global log context, which should be written with every message. For example:

<?php

// set custom logger:
Yii::setLogger(
    \yii1tech\psr\log\Logger::new()
        ->setPsrLogger(function () {
            // ...
        })
        ->withGlobalContext(function () {
            $context = [];
            
            // log remote IP address if available:
            if (!empty($_SERVER['REMOTE_ADDR'])) {
                $context['ip'] = $_SERVER['REMOTE_ADDR'];
            }
            
            // log authenticated user ID, if available:
            $webUser = Yii::app()->getComponent('user', false);
            if ($webUser !== null && !$webUser->getIsGuest()) {
                $context['auth_user_id'] = $webUser->getId();
            }
            
            return $context;
        })
);

PSR Log Route

It is not necessary to \yii1tech\psr\log\Logger if you need to pass logs to PSR logger. As an alternative you can add \yii1tech\psr\log\PsrLogRoute log route to the standard Yii "log" component.

Application configuration example:

<?php

return [
    'components' => [
        'log' => [
            'class' => \CLogRouter::class,
            'routes' => [
                [
                    'class' => \yii1tech\psr\log\PsrLogRoute::class,
                    'psrLogger' => function () {
                        $log = new \Monolog\Logger('yii');
                        $log->pushHandler(new \Monolog\Handler\StreamHandler('path/to/your.log', \Monolog\Level::Warning));
 
                        return $log;
                    },
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

Note: even if you use \yii1tech\psr\log\Logger as Yii logger, this \yii1tech\psr\log\PsrLogRoute will be unable to handle passed log context correctly.

Wrap Yii logger into PSR logger

There is another use case related to PSR logger besides bootstrapping eternal log storage. Sometimes 3rd party libraries may require PSR logger instance to be passed to them in order to function. For example, imagine we have a 3rd party library for "daemon" application running:

<?php

namespace vendor\daemon;

use Psr\Log\LoggerInterface;

class DaemonApplication
{
    public function __construct(LoggerInterface $logger)
    {
        // ....
    }
}

You can use \yii1tech\psr\log\PsrLogger to wrap standard Yii logging mechanism into PSR interface.

Application configuration example:

<?php

return [
    'components' => [
        \Psr\Log\LoggerInterface::class => [
            'class' => \yii1tech\psr\log\PsrLogger::class,
        ],
        // ...
    ],
    // ...
];

Now while working with our example external "daemon" application, we can use following code:

<?php

use Psr\Log\LoggerInterface;
use vendor\daemon\DaemonApplication;

$daemon = new DaemonApplication(Yii::app()->getComponent(LoggerInterface::class));
// ...

Note: in order to handle log context properly \yii1tech\psr\log\PsrLogger should be used in junction with \yii1tech\psr\log\Logger.

yii1tech/psr-log 适用场景与选型建议

yii1tech/psr-log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.6k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「logging」 「monolog」 「psr」 「Yii1」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yii1tech/psr-log 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yii1tech/psr-log 我们能提供哪些服务?
定制开发 / 二次开发

基于 yii1tech/psr-log 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 30
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-07-18