Skip to content

Using log_tags with rake tasks #151

Answered by reidmorrison
dgmora asked this question in Q&A
Discussion options

You must be logged in to vote

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

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by reidmorrison
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #145 on January 17, 2022 20:21.