Using the 'log' resource vs Chef::Log.<loglevel>

Ohai,

I am trying to understand the differences between:

log 'Here's a log line'

and:

Chef::Log.info 'Here's another log line'

from a Chef internals perspective.

Can anyone shed any pros/cons of either?

Thanks,
-M

On Monday, July 8, 2013 at 8:30 AM, Mike wrote:

Ohai,

I am trying to understand the differences between:

log 'Here's a log line'

and:

Chef::Log.info (http://Log.info) 'Here's another log line'

from a Chef internals perspective.

Can anyone shed any pros/cons of either?

The log resource eventually calls Chef::Log. The big difference is timing; a direct call to Chef::Log is just ruby that's executed in the compile phase, while the resource version is added to the resource collection and then gets processed in the converge phase. As a resource, you're also able to notify it from other resources.

Thanks,
-M

--
Daniel DeLeo

Thanks.

Does this impact resources updated each run? I believe a log resource
counts as an updated one every run, correct?

From a pure idiom sense, which would be preferable to use? Can/should
a recipe's logging be closer to the execution phase or not?

And I'm guessing that these are equivalent?

Chef::Log.debug "Only seen when in debug mode"

log "Only seen in debug mode" do
level :debug
end

How are log resources affected by resource cloning (CHEF-3694) ?
-M

On Mon, Jul 8, 2013 at 11:48 AM, Daniel DeLeo dan@kallistec.com wrote:

On Monday, July 8, 2013 at 8:30 AM, Mike wrote:

Ohai,

I am trying to understand the differences between:

log 'Here's a log line'

and:

Chef::Log.info 'Here's another log line'

from a Chef internals perspective.

Can anyone shed any pros/cons of either?

The log resource eventually calls Chef::Log. The big difference is timing; a
direct call to Chef::Log is just ruby that's executed in the compile phase,
while the resource version is added to the resource collection and then gets
processed in the converge phase. As a resource, you're also able to notify
it from other resources.

Thanks,
-M

--
Daniel DeLeo

On Monday, July 8, 2013 at 9:24 AM, Mike wrote:

Thanks.

Does this impact resources updated each run? I believe a log resource
counts as an updated one every run, correct?

Yes, it's similar to an execute or ruby_block resource in this regard.

From a pure idiom sense, which would be preferable to use? Can/should
a recipe's logging be closer to the execution phase or not?

Depends on the situation. Think about it from the perspective of a user of the recipe who hasn't read the code. Where is the message most useful?

And I'm guessing that these are equivalent?

Chef::Log.debug "Only seen when in debug mode"

log "Only seen in debug mode" do
level :debug
end

Aside from the compile/converge phase stuff (which will control where the log message actually appears), these are identical.

How are log resources affected by resource cloning (CHEF-3694) ?
All resource types are affected by resource cloning in the same way. If a resource's type+name match a previously defined resource's type+name, it will be created from a clone instead of created from scratch.

-M

--
Daniel DeLeo

Thanks for the awesome answers - this is very helpful.

How are log resources affected by resource cloning (CHEF-3694) ?

All resource types are affected by resource cloning in the same way. If a
resource's type+name match a previously defined resource's type+name, it
will be created from a clone instead of created from scratch.

Will a log resource with a level of debug be created/updated when the
Chef run isn't in debug mode, and not be displayed? Or is the resource
evaluated at compile phase, and determined "we;re not running in debug
mode, don't add this to the collection"?

-M

On Mon, Jul 8, 2013 at 12:33 PM, Daniel DeLeo dan@kallistec.com wrote:

On Monday, July 8, 2013 at 9:24 AM, Mike wrote:

Thanks.

Does this impact resources updated each run? I believe a log resource
counts as an updated one every run, correct?

Yes, it's similar to an execute or ruby_block resource in this regard.

From a pure idiom sense, which would be preferable to use? Can/should
a recipe's logging be closer to the execution phase or not?

Depends on the situation. Think about it from the perspective of a user of
the recipe who hasn't read the code. Where is the message most useful?

And I'm guessing that these are equivalent?

Chef::Log.debug "Only seen when in debug mode"

log "Only seen in debug mode" do
level :debug
end

Aside from the compile/converge phase stuff (which will control where the
log message actually appears), these are identical.

How are log resources affected by resource cloning (CHEF-3694) ?

All resource types are affected by resource cloning in the same way. If a
resource's type+name match a previously defined resource's type+name, it
will be created from a clone instead of created from scratch.

-M

--
Daniel DeLeo

On Monday, July 8, 2013 at 9:43 AM, Mike wrote:

Thanks for the awesome answers - this is very helpful.

How are log resources affected by resource cloning (CHEF-3694) ?

All resource types are affected by resource cloning in the same way. If a
resource's type+name match a previously defined resource's type+name, it
will be created from a clone instead of created from scratch.

Will a log resource with a level of debug be created/updated when the
Chef run isn't in debug mode, and not be displayed? Or is the resource
evaluated at compile phase, and determined "we;re not running in debug
mode, don't add this to the collection"?

The log resource's implementation is about as simple as it gets:

https://github.com/opscode/chef/blob/master/lib/chef/provider/log.rb

What you're describing sounds like it could be a useful improvement.

-M

--
Daniel DeLeo