summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/log/destinations.rb
blob: 9550e2c3b2fd60147a986d4e4bd24c6379bcca4d (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
Puppet::Util::Log.newdesttype :syslog do
  def close
    Syslog.close
  end

  def initialize
    Syslog.close if Syslog.opened?
    name = Puppet[:name]
    name = "puppet-#{name}" unless name =~ /puppet/

    options = Syslog::LOG_PID | Syslog::LOG_NDELAY

    # XXX This should really be configurable.
    str = Puppet[:syslogfacility]
    begin
      facility = Syslog.const_get("LOG_#{str.upcase}")
    rescue NameError
      raise Puppet::Error, "Invalid syslog facility #{str}"
    end

    @syslog = Syslog.open(name, options, facility)
  end

  def handle(msg)
    # XXX Syslog currently has a bug that makes it so you
    # cannot log a message with a '%' in it.  So, we get rid
    # of them.
    if msg.source == "Puppet"
      @syslog.send(msg.level, msg.to_s.gsub("%", '%%'))
    else
      @syslog.send(msg.level, "(%s) %s" % [msg.source.to_s.gsub("%", ""),
          msg.to_s.gsub("%", '%%')
        ]
      )
    end
  end
end

Puppet::Util::Log.newdesttype :file do
  match(/^\//)

  def close
    if defined?(@file)
      @file.close
      @file = nil
    end
  end

  def flush
    @file.flush if defined?(@file)
  end

  attr_accessor :autoflush

  def initialize(path)
    @name = path
    # first make sure the directory exists
    # We can't just use 'Config.use' here, because they've
    # specified a "special" destination.
    unless FileTest.exist?(File.dirname(path))
      Puppet.recmkdir(File.dirname(path))
      Puppet.info "Creating log directory #{File.dirname(path)}"
    end

    # create the log file, if it doesn't already exist
    file = File.open(path, File::WRONLY|File::CREAT|File::APPEND)

    @file = file

    @autoflush = Puppet[:autoflush]
  end

  def handle(msg)
    @file.puts("#{msg.time} #{msg.source} (#{msg.level}): #{msg}")

    @file.flush if @autoflush
  end
end

Puppet::Util::Log.newdesttype :console do


  RED     = {:console => "", :html => "FFA0A0"}
  GREEN   = {:console => "", :html => "00CD00"}
  YELLOW  = {:console => "", :html => "FFFF60"}
  BLUE    = {:console => "", :html => "80A0FF"}
  PURPLE  = {:console => "", :html => "FFA500"}
  CYAN    = {:console => "", :html => "40FFFF"}
  WHITE   = {:console => "", :html => "FFFFFF"}
  HRED    = {:console => "", :html => "FFA0A0"}
  HGREEN  = {:console => "", :html => "00CD00"}
  HYELLOW = {:console => "", :html => "FFFF60"}
  HBLUE   = {:console => "", :html => "80A0FF"}
  HPURPLE = {:console => "", :html => "FFA500"}
  HCYAN   = {:console => "", :html => "40FFFF"}
  HWHITE  = {:console => "", :html => "FFFFFF"}
  RESET   = {:console => "",    :html => ""      }

  Colormap = {
    :debug => WHITE,
    :info => GREEN,
    :notice => CYAN,
    :warning => YELLOW,
    :err => HPURPLE,
    :alert => RED,
    :emerg => HRED,
    :crit => HRED
  }

  def colorize(level, str)
    case Puppet[:color]
    when true, :ansi, "ansi", "yes"; console_color(level, str)
    when :html, "html"; html_color(level, str)
    else
      str
    end
  end

  def console_color(level, str)
    Colormap[level][:console] + str + RESET[:console]
  end

  def html_color(level, str)
    %{<span style="color: %s">%s</span>} % [Colormap[level][:html], str]
  end

  def initialize
    # Flush output immediately.
    $stdout.sync = true
  end

  def handle(msg)
    if msg.source == "Puppet"
      puts colorize(msg.level, "#{msg.level}: #{msg}")
    else
      puts colorize(msg.level, "#{msg.level}: #{msg.source}: #{msg}")
    end
  end
end

Puppet::Util::Log.newdesttype :host do
  def initialize(host)
    Puppet.info "Treating #{host} as a hostname"
    args = {}
    if host =~ /:(\d+)/
      args[:Port] = $1
      args[:Server] = host.sub(/:\d+/, '')
    else
      args[:Server] = host
    end

    @name = host

    @driver = Puppet::Network::Client::LogClient.new(args)
  end

  def handle(msg)
    unless msg.is_a?(String) or msg.remote
      @hostname ||= Facter["hostname"].value
      unless defined?(@domain)
        @domain = Facter["domain"].value
        @hostname += ".#{@domain}" if @domain
      end
      if msg.source =~ /^\//
        msg.source = @hostname + ":#{msg.source}"
      elsif msg.source == "Puppet"
        msg.source = @hostname + " #{msg.source}"
      else
        msg.source = @hostname + " #{msg.source}"
      end
      begin
        #puts "would have sent #{msg}"
        #puts "would have sent %s" %
        #    CGI.escape(YAML.dump(msg))
        begin
          tmp = CGI.escape(YAML.dump(msg))
        rescue => detail
          puts "Could not dump: #{detail}"
          return
        end
        # Add the hostname to the source
        @driver.addlog(tmp)
      rescue => detail
        puts detail.backtrace if Puppet[:trace]
        Puppet.err detail
        Puppet::Util::Log.close(self)
      end
    end
  end
end

# Log to a transaction report.
Puppet::Util::Log.newdesttype :report do
  attr_reader :report

  match "Puppet::Transaction::Report"

  def initialize(report)
    @report = report
  end

  def handle(msg)
    @report << msg
  end
end

# Log to an array, just for testing.
module Puppet::Test
  class LogCollector
    def initialize(logs)
      @logs = logs
    end

    def <<(value)
      @logs << value
    end
  end
end

Puppet::Util::Log.newdesttype :array do
  match "Puppet::Test::LogCollector"

  def initialize(messages)
    @messages = messages
  end

  def handle(msg)
    @messages << msg
  end
end