Using log_tags with rake tasks
dgmora opened this issue · 2 comments
Environment
Provide at least:
- Ruby Version. 2.6.8
- Rails Version. 5.2.6
- Semantic Logger Version. 4.8.2
- Rails Semantic Logger Version. 4.6.1
- Rails configuration. Only need the settings related to Rails Semantic Logger and Semantic Logger.
# application.rb
config.log_tags = {
request_id: ->(request) { request.uuid }
something: 'else'
}
config.rails_semantic_logger.format = SomeFormatter.new
Expected Behavior
named_tags
are also applied to rake tasks, or there's a way to configure that (I have not found how to do it)
Actual Behavior
I am using semantic logger to log in json. That part is working correctly also with rake. However the log_tags
are not attached and it seems that the custom formatter I use isn't used neither. I think that with rails the log_tags
are evaluated once per request, and that's probably the cause for them not being used, but I was wondering what would be an appropriate solution
and it seems that the custom formatter I use isn't used neither
Can't edit the original message. The custom formatter is actually being used correctly. It's only the named_tags that aren't applied
I don't think Rails uses the log_tags anywhere other than in the Rack middleware when processing http requests.
To add named tags to a rake task, or any ruby code, wrap the block with SemanticLogger.tagged
to supply any required named tags. Then those tags will be added to all log messages within that block.
SemanticLogger.tagged(user: 'Jack', zip_code: 12345) do
# All log entries in this block will include the above named tags
Rails.logger.debug("Hello World")
end
Output:
2021-12-23 13:36:04.741121 D [38248:13620 (irb):3] {user: Jack, zip_code: 12345} Rails -- Hello World