Level/Context override storage

未关闭
#138 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
停滞
技术栈
ruby
领域
tooling

调研方向

先阅读 #132、#136 和 #137,然后跟踪 Logger#initialize 以及 #with_level 和 #with_context 入口点。将提议的 ContextStore API 与当前的覆盖存储进行比较,并确定 Fiber 的继承行为。完成的标准是:无需继承 Logger 即可使用可自定义的共享上下文存储,并且现有的覆盖行为仍受到覆盖。

由索引模型根据 Issue 内容生成。

描述

I submitted a couple of PRs that solve the basic problems I have with the current level overrides:

  • #136
  • #137

But I'd prefer to change it a bit more.

If #132 (or something like it) is accepted:

  1. I believe that the "fiber scoped" block overrides for both #with_level and #with_context should use the same storage strategy. Rather than keeping two separate maps, I think it's better to combine them into a shared strategy: @override_store or @context_store.
  2. To share storage with a hash-like API, that suggests that level_key and context_key need to have different values. We could use something like Fiber[:logger_level_key] ||= Object.new, to support both Hash and ObjectSpace::WeakKeyMap, but that's kinda gross.

The current approach doesn't allow us to cleanly swap in the Fiber-storage API:

  • Fiber doesn't implement #delete
  • We can't reset storage with clone.clear (which is what I used in #137).

Fiber storage does face the additional question of how to handle when fibers aren't nested in a "structured concurrency" manner: does the child continue with its inherited storage after the parent leaves the #with_level or #with_context block. I think yes, it should... but others may want different behavior.

And many applications, frameworks, or libraries will already have their own encapsulation of execution context, which may not be identical to Fiber.current. As an example of this, see ActiveSupport::IsolatedExecutionState, which is used by the ActiveSupport::LoggerThreadSafeLevel mixin.

All these concerns can be addressed by creating a simple API, creating a default class that implements that API, and allowing an instance to be passed in as an argument to #initialize.

I propose something like the following:

class Logger
  class ContextStore
    def initialize
      @levels = ObjectSpace::WeakKeyMap.new
    end

    def level_override     = @levels[Fiber.current]
    def level_override=(val) @levels[Fiber.current] = val end

    def clear = @levels.clear

    private

    def initialize_copy(original)
      super
      @levels = @levels.dup
    end
  end

  def initialize(*, context_store: nil, **)
    # ...
    @context_store = context_store || ContextStore.new
    # ...
  end
end

This mimics the current implementation, with the addition of #136 and #137.

The benefits of this approach:

  • This allows users to customize context storage implementation without subclassing Logger.
  • By encapsulating all execution state context, we can remove implementation details (like #level_key) from the Logger class.
  • This simplifies using Fiber.storage for inheritance of logger context.
  • This can easily be extended to use the same strategy for both level overrides and context (see e.g: #132).
主要语言
Ruby
星标
161
派生
76
平均合并
2 天 5 小时
30 天内合并 PR
3

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/logger 的其他 Issue

查看 ruby/logger 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。