In Chef, is possible to get the recipe name when this recipe is running?
I want to add recipe name on logging/debugging. Something like:
Chef:Log.info "#{recipe_name} xxxx"
You can actually get the recipe name exactly the way you suggested in the question!
Chef::Log.info("I am a message from the #{recipe_name} recipe in the #{cookbook_name} cookbook.")
This and other handy methods are documented in the Recipe DSL page on the Opscode site.
I'm not sure about the recipe name, but you can use __FILE__
to log the filename of the currently executing Ruby script:
Chef::Log.info("#{__FILE__}")
shows up as
[2013-02-13T20:39:10+00:00] INFO: /tmp/vagrant-chef-1/chef-solo-1/cookbooks/gem/recipes/default.rb
at least for chef-solo.
I'm aware this is an old post, but it is still the top Google result, so I thought an updated answer for Chef 12 would be good to add in.
In Chef 12, I use this directly in my recipe:
log("Recipe @ #{__FILE__}")
The output looks like this:
* log[Recipe @ /var/chef/cache/cookbooks/crafter-cms-dev/recipes/test.rb] action write