log
Use the log resource to create log entries. The log resource behaves like any other resource: built into the resource collection during the compile phase, and then run during the execution phase. (To create a log entry that is not built into the resource collection, use Chef::Log
instead of the log resource.)
Note
By default, every log resource that executes will count as an updated resource in the updated resource count at the end of a Chef run. You can disable this behavior by adding count_log_resource_updates false
to your Chef client.rb
configuration file.
Syntax
A log resource block adds messages to the log file based on events that occur during the chef-client run:
log 'message' do message 'A message add to the log.' level :info end
The full syntax for all of the properties that are available to the log resource is:
log 'name' do level Symbol message String # defaults to 'name' if not specified notifies # see description provider Chef::Provider::ChefLog subscribes # see description action Symbol # defaults to :write if not specified end
where
-
log
is the resource -
name
is the name of the resource block -
message
is the log message to write -
:action
identifies the steps the chef-client will take to bring the node into the desired state -
level
,message
, andprovider
are properties of this resource, with the Ruby type shown. See “Properties” section below for more information about all of the properties that may be used with this resource.
Actions
This resource has the following actions:
:nothing
- Define this resource block to do nothing until notified by another resource to take action. When this resource is notified, this resource block is either run immediately or it is queued up to be run at the end of the chef-client run.
:write
- Default. Write to log.
Properties
This resource has the following properties:
ignore_failure
-
Ruby Types: TrueClass, FalseClass
Continue running a recipe if a resource fails for any reason. Default value:
false
. level
-
Ruby Type: Symbol
The level of logging that is to be displayed by the chef-client. The chef-client uses the
mixlib-log
(https://github.com/chef/mixlib-log) to handle logging behavior. Options (in order of priority)::debug
,:info
,:warn
,:error
, and:fatal
. Default value::info
. message
-
Ruby Type: String
The message to be added to a log file. Default value: the
name
of the resource block See “Syntax” section above for more information. notifies
-
Ruby Type: Symbol, ‘Chef::Resource[String]’
A resource may notify another resource to take action when its state changes. Specify a
'resource[name]'
, the:action
that resource should take, and then the:timer
for that action. A resource may notifiy more than one resource; use anotifies
statement for each resource to be notified.A timer specifies the point during the chef-client run at which a notification is run. The following timers are available:
:delayed
- Default. Specifies that a notification should be queued up, and then executed at the very end of the chef-client run.
-
:immediate
,:immediately
- Specifies that a notification should be run immediately, per resource notified.
The syntax for
notifies
is:notifies :action, 'resource[name]', :timer
provider
-
Ruby Type: Chef Class
Optional. Explicitly specifies a provider.
retries
-
Ruby Type: Integer
The number of times to catch exceptions and retry the resource. Default value:
0
. retry_delay
-
Ruby Type: Integer
The retry delay (in seconds). Default value:
2
. subscribes
-
Ruby Type: Symbol, ‘Chef::Resource[String]’
A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a
'resource[name]'
, the:action
to be taken, and then the:timer
for that action.A timer specifies the point during the chef-client run at which a notification is run. The following timers are available:
:delayed
- Default. Specifies that a notification should be queued up, and then executed at the very end of the chef-client run.
-
:immediate
,:immediately
- Specifies that a notification should be run immediately, per resource notified.
The syntax for
subscribes
is:subscribes :action, 'resource[name]', :timer
Providers
Where a resource represents a piece of the system (and its desired state), a provider defines the steps that are needed to bring that piece of the system from its current state into the desired state.
The chef-client will determine the correct provider based on configuration data collected by Ohai at the start of the chef-client run. This configuration data is then mapped to a platform and an associated list of providers.
Generally, it’s best to let the chef-client choose the provider, and this is (by far) the most common approach. However, in some cases, specifying a provider may be desirable. There are two approaches:
- Use a more specific short name—
yum_package "foo" do
instead ofpackage "foo" do
,script "foo" do
instead ofbash "foo" do
, and so on—when available - Use the
provider
property within the resource block to specify the long name of the provider as a property of a resource. For example:provider Chef::Provider::Long::Name
This resource has the following providers:
-
Chef::Provider::Log::ChefLog
,log
- The default provider for all platforms.
Examples
The following examples demonstrate various approaches for using resources in recipes. If you want to see examples of how Chef uses resources in recipes, take a closer look at the cookbooks that Chef authors and maintains: https://github.com/chef-cookbooks.
Set default logging level
log 'a string to log'
Set debug logging level
log 'a debug string' do level :debug end
Add a message to a log file
log 'message' do message 'This is the message that will be added to the log.' level :info end
Use Chef::Log to create a log entry
The following example shows two log entries, one created using Chef::Log
and the other using the log resource. Only the log entry created by the log resource is built into the resource collection.
... client_bin = find_chef_client Chef::Log.debug("Found chef-client in #{client_bin}") node.default['chef_client']['bin'] = client_bin create_directories log 'Using winsw_service on this Chef version is deprecated; use windows_service' do level :warn only_if { Gem::Requirement.new('>= 11.5').satisfied_by?(Gem::Version.new(::Chef::VERSION)) } end ...
© Chef Software, Inc.
Licensed under the Creative Commons Attribution 3.0 Unported License.
The Chef™ Mark and Chef Logo are either registered trademarks/service marks or trademarks/servicemarks of Chef, in the United States and other countries and are used with Chef Inc's permission.
We are not affiliated with, endorsed or sponsored by Chef Inc.
https://docs-archive.chef.io/release/11-18/resource_log.html