Debugging Chef Runs with CHEF_LOG
When you have experience with troubleshooting Vagrant, you probably know that
you can enable detailed logging by setting the VAGRANT_LOG
environment
variable to a specific log level. For example, the info
level will make
Vagrant much more chatty:
While this is helpful for debugging Vagrant itself, I’ve always wanted to have something similar for Vagrant’s provisioners–in particular the Chef Solo provisioner, which I’m using most of the time. In order to get more detailed information about a Chef run, here is how you can enable debug-level logging in your Vagrantfile:
Since the very verbose debug output makes it hard to spot events during normal
Chef runs, I used to reduce the log level from debug
to info
(the default)
after each debugging session by commenting out the line. Of course, I don’t like
toggling log levels in source code, and neither does Git. As a result, I came up
with this VAGRANT_LOG
-like solution:
This bit of Ruby code allows you to configure Chef’s log level via the
environment variable CHEF_LOG
, which must be set to debug
, info
, warn
,
error
, or fatal
.
Now, debugging Chef runs boils down to:
This works with both Chef Solo and Chef Client. It should be trivial to
implement the same mechanism for other provisioners like Puppet (using --debug
or --test
) or Shell (with set -x
).
CHEF_FORMAT (added on 2013-12-13)
Besides the log level discussed above, there’s another configuration option you
can set in your Vagrantfiles to influence the output of Chef: formatter
. This
one, as its name implies, changes the output format of Chef. Valid formats are
doc
, min
(or minimal
), and null
. Chef uses doc
by default whenever you
run it directly from the command line. Vagrant, on the other hand, applies
null
by default, which effectively reduces output to log messages.
Similar to CHEF_LOG
, it’s straightforward to make the output format
configurable via an environment variable–say hello to CHEF_FORMAT
. The final
code looks like the following:
As an example, this combination of CHEF_FORMAT
and CHEF_LOG
will cause Chef
to only print out the most basic information:
Happy logging!