Skip to content

Conversation

@huangdijia
Copy link
Contributor

@huangdijia huangdijia commented Sep 19, 2025

Summary

  • Enhanced LogsHandler constructor to accept integer log levels in addition to LogLevel objects
  • Added automatic conversion from Monolog integer levels to Sentry LogLevel objects
  • Maintains full backward compatibility with existing LogLevel usage

Changes

  • Modified constructor parameter type from ?LogLevel to int|LogLevel|null
  • Added logic to convert integer log levels using getSentryLogLevelFromMonologLevel()
  • Improved flexibility for users working with Monolog's integer-based log levels

Test plan

  • Verify existing LogLevel objects continue to work
  • Test integer log level conversion functionality
  • Confirm backward compatibility is maintained
  • Run existing test suite to ensure no regressions

Summary by CodeRabbit

  • 新特性
    • 日志级别配置更灵活:现支持以整数或枚举方式设置,并自动转换为 Sentry 日志级别。
    • 保持向后兼容:现有配置无需改动,默认行为不变。

- Accept int|LogLevel|null in constructor parameter
- Automatically convert integer log levels to Sentry LogLevel objects
- Maintains backward compatibility with existing LogLevel usage
- Improves flexibility for users passing Monolog integer levels
@coderabbitai
Copy link

coderabbitai bot commented Sep 19, 2025

Walkthrough

更新 LogsHandler 构造函数签名:$logLevel 由原先仅支持 ?LogLevel 改为 int|LogLevel|null。当传入 int 时,先通过 getSentryLogLevelFromMonologLevel 转换为 Sentry 的 LogLevel,再调用父类构造函数;其余行为不变。

Changes

Cohort / File(s) Summary
Monolog 日志处理器
src/sentry/src/Monolog/LogsHandler.php
构造函数签名变更为 `int

Sequence Diagram(s)

sequenceDiagram
    actor Caller
    participant LogsHandler as LogsHandler::__construct
    participant Converter as getSentryLogLevelFromMonologLevel
    participant Parent as parent::__construct

    Caller->>LogsHandler: new LogsHandler(group, logLevel, bubble)
    alt logLevel is int
        LogsHandler->>Converter: convert(monologLevel:int)
        Converter-->>LogsHandler: sentryLevel:LogLevel
        LogsHandler->>Parent: __construct(..., sentryLevel, ...)
    else logLevel is LogLevel/null
        LogsHandler->>Parent: __construct(..., logLevel, ...)
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

我把级别拨一拨,咔哒变换间,
整数跳成小徽章,贴上 Log 的肩。
构造轻轻向上递,父类稳如山,
泥土里挖到新路径,🐇耳随风旋。
今夜日志更懂我,星光照栈帧。

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed 标题 "feat(sentry): add integer log level support to LogsHandler" 简洁且明确地概括了本 PR 的主要变更——为 LogsHandler 添加对整数型 Monolog 日志级别的支持,内容与变更描述高度一致且便于在提交历史中快速识别。
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/sentry-logshandler-int-loglevel-support

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 PHPStan (2.1.17)

At least one path must be specified to analyse.

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/sentry/src/Monolog/LogsHandler.php (3)

24-28: 补充构造函数 PHPDoc,说明可接受的整型范围与含义

便于 IDE/静态分析与使用者理解(如“传入 Monolog 整型级别:Logger::WARNING 等”)。

可考虑:

     public function __construct(
         protected string $group = 'default',
-        int|LogLevel|null $logLevel = null,
+        /**
+         * @param int|LogLevel|null $logLevel Monolog 整型级别(如 \Monolog\Logger::WARNING)或 Sentry\Logs\LogLevel;null 使用默认
+         */
+        int|LogLevel|null $logLevel = null,
         protected bool $bubble = true
     ) {

29-31: 对非法整型级别增加更友好的错误提示

若传入的 int 不可映射,当前会沿用底层异常信息。建议在此处捕获并抛出语义更清晰的 InvalidArgumentException。

-        if (is_int($logLevel)) {
-            $logLevel = self::getSentryLogLevelFromMonologLevel($logLevel);
-        }
+        if (is_int($logLevel)) {
+            try {
+                $logLevel = self::getSentryLogLevelFromMonologLevel($logLevel);
+            } catch (\Throwable $e) {
+                throw new \InvalidArgumentException(
+                    sprintf('Unsupported Monolog integer level: %d', $logLevel),
+                    0,
+                    $e
+                );
+            }
+        }

44-44: 拼写小修正:separately

注释里 “seperately” → “separately”。

-        // Do not collect logs for exceptions, they should be handled seperately by the `Handler` or `captureException`
+        // Do not collect logs for exceptions, they should be handled separately by the `Handler` or `captureException`
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7e5b9d0 and 28d1676.

📒 Files selected for processing (1)
  • src/sentry/src/Monolog/LogsHandler.php (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.1 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.3 with Swoole 5.1.7
  • GitHub Check: Test on PHP 8.2 with Swoole 5.1.7
🔇 Additional comments (1)
src/sentry/src/Monolog/LogsHandler.php (1)

26-26: 签名扩展为 int|LogLevel|null — 总体 LGTM

已在 src/sentry/src/Monolog/LogsHandler.php 确认构造函数签名为 int|LogLevel|null;仓库中对 LogsHandler 的实例化尚未定位(上次 rg 搜索未检索到文件)。合并前需核查是否存在传入 string 或 Monolog\Level 的调用,使用下列修正后的搜索脚本复核:

#!/bin/bash
set -euo pipefail

# 直接实例化
rg -nP -S --hidden --glob '!vendor/**' -C2 'new\s+LogsHandler\s*\(' || true

# 可能传入字符串作为第二参数
rg -nP -S --hidden --glob '!vendor/**' -C1 "new\s+LogsHandler\s*\([^,]+,\s*['\"][^'\"]+['\"]" || true

# 可能传入 Monolog\Level 常量
rg -nP -S --hidden --glob '!vendor/**' -C1 'new\s+LogsHandler\s*\([^,]+,\s*Monolog\\Level::' || true

# DI / 工厂 / 配置 引用
rg -n --hidden --glob '!vendor/**' -C3 'LogsHandler::class' || true
rg -n --hidden --glob '!vendor/**' -C3 'LogsHandler' || true

public function __construct(
protected string $group = 'default',
?LogLevel $logLevel = null,
int|LogLevel|null $logLevel = null,
Copy link
Contributor

@zds-s zds-s Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int|LogLevel|null $logLevel = null,
null|int|LogLevel$logLevel = null,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个要改cs-fixer配置吧

@huangdijia huangdijia merged commit 258f9e1 into main Sep 19, 2025
23 of 24 checks passed
@huangdijia huangdijia deleted the feat/sentry-logshandler-int-loglevel-support branch September 19, 2025 10:20
huangdijia added a commit that referenced this pull request Sep 19, 2025
- Accept int|LogLevel|null in constructor parameter
- Automatically convert integer log levels to Sentry LogLevel objects
- Maintains backward compatibility with existing LogLevel usage
- Improves flexibility for users passing Monolog integer levels

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants