Replies: 1 comment
-
|
And Rails 8.1 solves this problem a bit, yay! require "semantic_logger"
module ContextualLogger
class RailsContextBridge
class << self
def context
SemanticLogger.named_tags
end
def set_context(context_hash)
SemanticLogger.push_named_tags(context_hash)
end
def clear
Thread.current[:semantic_logger_named_tags] = []
end
end
end
endThis can then be brought in with: config.active_support.event_reporter_context_store = ContextualLogger::RailsContextBridgeIt's definitely not pretty, but it at least works and integrates perfectly well with existing MDC behaviors. Clearing named tags at the end of each request is still a bit strange and might have side effects for other use cases, but it's a viable proof of concept. I'd be curious to see how (and if) this library evolves to support the event system. There's also class MyController < ApplicationController
around_action :add_logs
def index
# do things
end
private def add_logs
SemanticLogger.named_tags(key: "value") do
yield
end
end
end |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to look at using
SemanticLogger#push_named_tagsto support MDC-like log contexts:This in and of itself works, but has a few quirks:
push_named_tags, the log context will leak between requests. As an example, a logged out request following a logged in request will still receive the named tags.push_named_tagsmethod in general does not perform a deep merge on provided context, meaning if multiplepush_named_tagswere to setuser, only the last one would win.As is, both of these can be resolved with some tricks that ultimately come down to a special tag named
_mdc, a lot of (inefficient) juggling, and a middleware to clear this after every request. This, however, is obviously inelegant and inefficient. A number of monkeypatches can also be applied, but that in turn has its own problems. It would be nicer to be able to useSemanticLogger.tagged, but I'm not sure how (or even if) it's possible to appropriately wrap Rails contexts in that.Has anyone managed to set up something like this before? Or, better yet, is this something that would be beneficial to SemanticLogger overall? It seems relatively simple to tweak the requisite behavior, but I'm not sure it's desired.
I suspect all that would really be needed would be to change named_tags to properly deep merge, and then inject some middleware accordingly to set/clear things.
Beta Was this translation helpful? Give feedback.
All reactions