summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/logging.rb
blob: b4a531bfc7d03257430944b7b16cc90e33ff8ccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# A module to make logging a bit easier.
require 'puppet/util/log'

module Puppet::Util::Logging

    def send_log(level, message)
        Puppet::Util::Log.create({:level => level, :source => log_source, :message => message}.merge(log_metadata))
    end

    # Create a method for each log level.
    Puppet::Util::Log.eachlevel do |level|
        define_method(level) do |args|
            args = args.join(" ") if args.is_a?(Array)
            send_log(level, args)
        end
    end

    private

    def is_resource?
        defined?(Puppet::Type) && is_a?(Puppet::Type)
    end

    def is_resource_parameter?
        defined?(Puppet::Parameter) && is_a?(Puppet::Parameter)
    end

    def log_metadata
        [:file, :line, :version, :tags].inject({}) do |result, attr|
            result[attr] = send(attr) if respond_to?(attr)
            result
        end
    end

    def log_source
        # We need to guard the existence of the constants, since this module is used by the base Puppet module.
        (is_resource? or is_resource_parameter?) and respond_to?(:path) and return path.to_s
        to_s
    end
end