oshco/db-exceptions-logger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

oshco/db-exceptions-logger

Composer 安装命令:

composer require oshco/db-exceptions-logger

包简介

A component which is used to log exceptions to database.

README 文档

README

A WebFiori framework extension that logs exceptions to a database with optional per-portal tracking. Currently supports SQL Server only.

Requirements

Installation

composer require oshco/db-exceptions-logger

Setup

1. Create the database table

Run the migration to create the system_exceptions table:

php webfiori migrations:ini --connection=<your-connection>
php webfiori migrations:run --connection=<your-connection>

2. Register the error handler

use Oshco\ErrHandler\DatabaseErrHandler;
use Oshco\Infrastructure\Repository\ExceptionsRepository;
use WebFiori\Database\Database;
use WebFiori\Error\Handler;
use WebFiori\Framework\App;

$db = new Database(App::getConfig()->getDBConnection('your-connection'));
$repo = new ExceptionsRepository($db);
Handler::registerHandler(new DatabaseErrHandler($repo));

3. (Optional) Configure portal tracking

To associate exceptions with a specific portal, set a resolver callable:

use Oshco\ErrHandler\DatabaseErrHandler;
use WebFiori\Framework\Session\SessionsManager;

DatabaseErrHandler::setPortalIdResolver(function () {
    $session = SessionsManager::getActiveSession();
    return $session?->get('portal-id');
});

The resolver is called when an exception occurs. It should return an int (portal ID) or null.

Usage

Querying logged exceptions

use Oshco\Infrastructure\Repository\ExceptionsRepository;
use WebFiori\Database\Database;
use WebFiori\Framework\App;

$db = new Database(App::getConfig()->getDBConnection('your-connection'));
$repo = new ExceptionsRepository($db);

// Get total count
$count = $repo->count();

// Get paginated list (page 1, 10 per page)
$exceptions = $repo->getAll(1, 10);

// Get by ID
$exception = $repo->getById(1);

// Get the most recent exception
$latest = $repo->getLast();

// Check if an exception with a given hash exists
$exists = $repo->existsByHash($hashString);

// Filter by portal
$portalExceptions = $repo->getByPortal(portalId: 1, page: 1, size: 10);
$portalCount = $repo->countByPortal(portalId: 1);

Entity properties

Each SystemException entity exposes:

Method Description
getId() Auto-increment ID
getHash() SHA-256 hash for deduplication
getDate() Timestamp when the exception was logged
getCode() Exception code
getClass() Class where the exception was thrown
getExceptionClass() The exception's fully-qualified class name
getMessage() Exception message
getLine() Line number
getUrl() Request URL
getParameters() Request parameters
getTrace() Stack trace
getPortalId() Portal ID (null if not set)

How It Works

When an exception occurs, DatabaseErrHandler:

  1. Captures exception details (code, class, message, file, line, stack trace)
  2. Captures request context (URL, parameters)
  3. Resolves the portal ID via the configured resolver (if set)
  4. Computes a SHA-256 hash for deduplication
  5. Stores everything in the system_exceptions table via ExceptionsRepository

Database Schema

Column Type Nullable Description
id INT (PK, identity) No Auto-increment ID
hash NVARCHAR(128) No SHA-256 hash for deduplication
date DATETIME2 No Timestamp (defaults to now)
code INT No Exception code
class VARCHAR(128) No Class where exception was thrown
exception_class VARCHAR(128) No Exception class name
message NVARCHAR(256) No Exception message
line INT No Line number
url NVARCHAR(256) Yes Request URL
parameters NVARCHAR(1024) Yes Request parameters
trace NVARCHAR(1024) No Stack trace
portal_id INT Yes Portal where exception occurred

Classes

Class Description
DatabaseErrHandler Error handler that captures exceptions and delegates storage to the repository. Supports a configurable portal ID resolver.
ExceptionsRepository Repository providing CRUD and filtering operations on the system_exceptions table.
SystemExceptionsTable Attribute-based MSSQL table schema definition.
SystemException Entity representing a logged exception record.
CreateSystemExceptionsTable Migration that creates the base table.
AlterSystemExceptionsAddPortalId Migration that adds the portal_id column (safe for existing tables).

Running Tests

Requires a running SQL Server instance:

SA_SQL_SERVER_PASSWORD='<your-password>' composer test

License

MIT

Statistics Methods

All statistics methods accept optional $from and $to date parameters (ISO format) for filtering by date range.

// Count by exception class
$stats = $repo->getStatsByExceptionClass('2026-01-01', '2026-12-31', limit: 10);

// Count by class + line (pinpoints exact code location)
$stats = $repo->getStatsByClassAndLine();

// Count by portal
$stats = $repo->getStatsByPortal();

// Count by URL (which endpoints fail most)
$stats = $repo->getStatsByUrl();

// Daily error trend
$stats = $repo->getStatsByDay('2026-06-01', '2026-06-30');

// Top recurring errors (grouped by hash)
$stats = $repo->getTopRecurring();

// Total count in date range
$count = $repo->countInRange('2026-06-01', '2026-06-30');

oshco/db-exceptions-logger 适用场景与选型建议

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

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

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

围绕 oshco/db-exceptions-logger 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-20