2
require 'puppet/util/tagging'
3
4
# Pass feedback to the user. Log levels are modeled after syslog's, and it is
4
5
# expected that that will be the most common log destination. Supports
5
6
# multiple destinations, one of which is a remote server.
6
7
class Puppet::Util::Log
9
include Puppet::Util::Tagging
9
11
@levels = [:debug,:info,:notice,:warning,:err,:alert,:emerg,:crit]
244
246
newdesttype :console do
247
PINK = {:console => "[0;31m", :html => "FFA0A0"}
248
GREEN = {:console => "[0;32m", :html => "00CD00"}
249
YELLOW = {:console => "[0;33m", :html => "FFFF60"}
250
SLATE = {:console => "[0;34m", :html => "80A0FF"}
251
ORANGE = {:console => "[0;35m", :html => "FFA500"}
252
BLUE = {:console => "[0;36m", :html => "40FFFF"}
253
RESET = {:console => "[0m", :html => ""}
249
RED = {:console => "[0;31m", :html => "FFA0A0"}
250
GREEN = {:console => "[0;32m", :html => "00CD00"}
251
YELLOW = {:console => "[0;33m", :html => "FFFF60"}
252
BLUE = {:console => "[0;34m", :html => "80A0FF"}
253
PURPLE = {:console => "[0;35m", :html => "FFA500"}
254
CYAN = {:console => "[0;36m", :html => "40FFFF"}
255
WHITE = {:console => "[0;37m", :html => "FFFFFF"}
256
HRED = {:console => "[1;31m", :html => "FFA0A0"}
257
HGREEN = {:console => "[1;32m", :html => "00CD00"}
258
HYELLOW = {:console => "[1;33m", :html => "FFFF60"}
259
HBLUE = {:console => "[1;34m", :html => "80A0FF"}
260
HPURPLE = {:console => "[1;35m", :html => "FFA500"}
261
HCYAN = {:console => "[1;36m", :html => "40FFFF"}
262
HWHITE = {:console => "[1;37m", :html => "FFFFFF"}
263
RESET = {:console => "[0m", :html => ""}
266
276
def colorize(level, str)
462
468
@levels.include?(level)
465
attr_accessor :level, :message, :time, :tags, :remote
471
attr_accessor :level, :message, :time, :remote
466
472
attr_reader :source
468
474
def initialize(args)
469
475
unless args.include?(:level) && args.include?(:message)
470
raise Puppet::DevError, "Puppet::Util::Log called incorrectly"
476
raise ArgumentError, "Puppet::Util::Log called incorrectly"
473
479
if args[:level].class == String
475
481
elsif args[:level].class == Symbol
476
482
@level = args[:level]
478
raise Puppet::DevError,
479
"Level is not a string or symbol: #{args[:level].class}"
484
raise ArgumentError, "Level is not a string or symbol: #{args[:level].class}"
482
# Just return unless we're actually at a level we should send
483
#return unless self.class.sendlevel?(@level)
485
487
@message = args[:message].to_s
487
# this should include the host name, and probly lots of other
488
# stuff, at some point
489
unless self.class.validlevel?(level)
490
raise Puppet::DevError, "Invalid message level #{level}"
493
if args.include?(:tags)
497
if args.include?(:source)
498
self.source = args[:source]
490
raise ArgumentError, "Invalid log level %s" % level unless self.class.validlevel?(level)
492
if tags = args[:tags]
493
tags.each { |t| self.tag(t) }
496
self.source = args[:source] || "Puppet"
498
# Tag myself with my log level
503
501
Log.newmessage(self)
506
# Was the source of this log an object?
508
if defined? @objectsource and @objectsource
515
504
# If they pass a source in to us, we make sure it is a string, and
516
505
# we retrieve any tags we can.
517
506
def source=(source)
519
508
# We can't just check for whether it responds to :path, because
520
509
# plenty of providers respond to that in their normal function.
521
510
if (source.is_a?(Puppet::Type) or source.is_a?(Puppet::Parameter)) and source.respond_to?(:path)
523
511
@source = source.path
525
@objectsource = false
526
513
@source = source.to_s
528
unless defined? @tags and @tags
529
if source.respond_to?(:tags)
515
if source.respond_to?(:tags)
516
source.tags.each { |t| tag(t) }
536
@tags.detect { |t| t.to_s == tag.to_s }
540
521
"%s %s (%s): %s" % [self.time, self.source, self.level, self.to_s]
529
# This is for backward compatibility from when we changed the constant to Puppet::Util::Log
530
# because the reports include the constant name. Apparently the alias was created in
531
# March 2007, should could probably be removed soon.
547
532
Puppet::Log = Puppet::Util::Log