定制 lfbn/logger-trait 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

lfbn/logger-trait

Composer 安装命令:

composer require lfbn/logger-trait

包简介

A trait that allows you to add logging capabilities to any class.

README 文档

README

This is a Trait that allows to have logging capabilities in any class.

By default, it uses Monolog and streams to standard output, but you can override this behaviour.

Installation

composer require lfbn/logger-trait

Usage

Using the default behaviour

// In the class you want, add the use of the Trait.
use LoggerTrait;

(...)

// After that, you can use it.
$this->logError('Some message...', ['some context']);

Changing name, stream or minimum level

// Implement the following protected properties.
/* @var string */
protected static $loggerName = 'my-logger-name';

/* @var string */
protected static $loggerStream = 'php://stdout';

/* @var string */
protected static $loggerMinimumLevel = LogLevel::DEBUG;

Overriding the default behaviour

class MyClass {
   protected function initLogger(): bool
   {
       $this->logger = new \Monolog\Logger('Overriding default logger: '.$this->getLoggerName());

       try {
           $handler = new StreamHandler(
               $this->getLoggerStream(),
               $this->getLoggerMinimumLevel()
           );
           $this->logger->pushHandler($handler);
       } catch (Exception $e) {
           $this->logger = new NullLogger();

           return false;
       }

       return true;
   }
}

Inject your own logger

class Test {
    use \Lfbn\LoggerTrait\LoggerTrait;
    public function test(): void
    {
        $this->logDebug('Hello TEST!');
    }
}

$logger = new \Monolog\Logger('test4-my-own-logger');

try {
    $handler = new StreamHandler(
        'php://stdout',
        'debug'
    );
    $logger->pushHandler($handler);
} catch (Exception $e) {
    $logger = new NullLogger();
}

$myClass = (new Test());
$myClass->setLogger($logger);
$myClass->test();

Interpolate messages

$this->logWarning(
            self::interpolateMessage(
                'Hello my {private} TEST5!',
                ['private' => 'message']
            ),
        );

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固