定制 stefna/log 二次开发

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

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

stefna/log

Composer 安装命令:

composer require stefna/log

包简介

Psr-3 log implementation for stefna.is

README 文档

README

Stefna\Logger\ManagerInterface

Class that handles creation of logger classes.

This is where you configure your main logger.

Methods

  • pushProcessor(callable $callback, string $channel = null): Manager If channel is set the process will only be applied to that channel. If no instance of channel exists the process will be silently ignored

Stefna\Logger\MonologManager

Manager that uses Monolog as the main logger.

It has one extra method on the manager and that's pushHandler that is so that you can configure custom handlers for select channels

  • pushHandler(HandlerInterface $handler, $channel = null): Manager If channel is set the handler will only be applied to that channel. If no instance of channel exists the process will be silently ignored

Stefna\Logger\Logger Methods

setManager

Set log manager if this is not done all calls to getLogger will return a NullLogger

getManager

Get manager to add process or handler to main logger

setChannelConfig

Add a channel specific config

setGlobalConfig

Add multiple config options with one call

getLogger

Retrives a logging instance for channel

This method will create the logger if it don't exists and it check for configs for the specified channel

Example

<?php declare(strict_types=1);

use Stefna\Logger\Filters\MinLogLevelFilter;
use Stefna\Logger\Filters\CallbackFilter;
use Stefna\Logger\Filters\TimeLimitFilter;

$monolog = new \Monolog\Logger('main-channel', $handlers, $proccess);
$manager = new \Stefna\Logger\MonologManager($monolog, new \Stefna\Logger\Filters\FilterFactory());

\Stefna\Logger\Logger::setManager($manager);

$filters = [
	[MinLogLevelFilter::KEY, ['level' => \Psr\Log\LogLevel::ALERT]],
	[
		CallbackFilter::KEY,
		[
			'callback' => function(string $level, string $message, array $context) {
				return isset($context['exception']);
			},
		],
	],
	[TimeLimitFilter::KEY, ['cache' => $simpleCache, 'interval' => new DateInterval('P1D')]]
];

\Stefna\Logger\Logger::setChannelConfig(
    'test-channel',
    new Stefna\Logger\Config\Config('test-channel', $filters[[, $proccess], $handlers])
);

$logger = \Stefna\Logger\Logger::getLogger('test-channel');

Setup of a crash logger

<?php declare(strict_types=1);

$logger = new SimpleFileLogger('path/to/save/crash.log');
//or
$logger = new SystemErrorLogger();

$crashLogger = new BufferFilterLogger(
    $logger,
    new ActivateLevelFilter(LogLevel::ERROR)
);

// Will not add to log file
$crashLogger->debug('test');

// Will add all message prior and after this to the log
// This is so that we get a complete story of what happened during the execution
$crashLogger->error('error');

Setup debouncer filter

This filter is meant to be used to prevent logs from being filed with the same errors

Setup

<?php declare(strict_types=1);

use Stefna\Logger\Filters\DebounceFilter;
use Stefna\Logger\Logger\FilterLogger

$debounceFilter = new DebounceFilter(function($level, $message, $context) use ($cache) {
	// create cache key
	$key = md5(serialize([$message, $context]));
	if ($cache->has($key)) {
		return false;
	}
	$debounceInterval = $context[DebounceFilter::DEBOUNCE_INTERVAL];
	$cache->set($key, true, new DateInterval($debounceInterval));
	return true;
});

$logger = new FilterLogger($mainLogger, $debounceFilter);
$logger->alert('Db connect error', [
	DebounceFilter::DEBOUNCE_INTERVAL => 'PT1H', // only log once an hour
]);

Using LogChannel with stefna/di

This will add the channel to the logger context.

This is useful to filter logs

<?php declare(strict_types=1);

use Stefna\Logger\Di\Attributes\LogChannel;
use Psr\Log\LoggerInterface;

class SomeDomain {
	public function __construct(
		#[LogChannel('custom-channel')]
		private LoggerInterface $logger
	) {}
}

Using DataDog processor

This processor is meant to be used with the DataDog agent to send the stack trace to DataDog.

And it also supports excluding frames from the stack trace. This is useful if you have a lot of duplicate frames in the stack trace, and you don't want to send them to DataDog. It's also nice to remove framework code from the stack traces since in most cases it's not relevant.

Setup

<?php declare(strict_types=1);

use Stefna\Logger\Processor\DataDogProcessor;

$processor = new DataDogProcessor([
	[ // stack frames you aren't interested in. 
		'file' => 'vendor/phpunit/phpunit/src/Framework/TestSuite.php',
		'line' => 685, // optional, if not set all frames including the file will be excluded
	],
]);

stefna/log 适用场景与选型建议

stefna/log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.05k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-04