Class: Puppet::Util::Log
- Extended by:
- Puppet::Util, ClassGen
- Includes:
- Puppet::Util, Tagging
- Defined in:
- lib/puppet/util/log.rb
Overview
Pass feedback to the user. Log levels are modeled after syslog’s, and it is expected that that will be the most common log destination. Supports multiple destinations, one of which is a remote server.
Defined Under Namespace
Classes: Destination
Class Attribute Summary collapse
-
.desttypes ⇒ Object
readonly
Returns the value of attribute desttypes.
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#level ⇒ Object
Returns the value of attribute level.
-
#line ⇒ Object
Returns the value of attribute line.
-
#message ⇒ Object
Returns the value of attribute message.
-
#remote ⇒ Object
Returns the value of attribute remote.
-
#source ⇒ Object
Returns the value of attribute source.
-
#time ⇒ Object
Returns the value of attribute time.
Class Method Summary collapse
- .autoflush=(v) ⇒ Object
-
.close(destination) ⇒ Object
Reset log to basics.
- .close_all ⇒ Object
-
.create(hash) ⇒ Object
Create a new log message.
- .destinations ⇒ Object
-
.eachlevel ⇒ Object
Yield each valid level in turn.
-
.flush ⇒ Object
Flush any log destinations that support such operations.
- .flushqueue ⇒ Object
-
.level ⇒ Object
Return the current log level.
-
.level=(level) ⇒ Object
Set the current log level.
- .levels ⇒ Object
-
.newdestination(dest) ⇒ Object
Create a new log destination.
-
.newdesttype(name, options = {}, &block) ⇒ Object
Create a new destination type.
-
.newmessage(msg) ⇒ Object
Route the actual message.
- .queuemessage(msg) ⇒ Object
-
.reopen ⇒ Object
Reopen all of our logs.
- .sendlevel?(level) ⇒ Boolean
-
.validlevel?(level) ⇒ Boolean
Is the passed level a valid log level?.
Instance Method Summary collapse
-
#initialize(args) ⇒ Log
constructor
A new instance of Log.
- #to_report ⇒ Object
- #to_s ⇒ Object
Methods included from Puppet::Util
activerecord_version, benchmark, chuser, classproxy, execfail, execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, threadlock, which, withumask
Methods included from POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from ClassGen
Methods included from MethodHelper
#requiredopts, #set_options, #symbolize_options
Methods included from Tagging
Constructor Details
#initialize(args) ⇒ Log
Returns a new instance of Log.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/puppet/util/log.rb', line 201 def initialize(args) self.level = args[:level] self. = args[:message] self.source = args[:source] || "Puppet" @time = Time.now if = args[:tags] .each { |t| self.tag(t) } end [:file, :line].each do |attr| next unless value = args[attr] send(attr.to_s + "=", value) end Log.(self) end |
Class Attribute Details
.desttypes ⇒ Object (readonly)
Returns the value of attribute desttypes.
44 45 46 |
# File 'lib/puppet/util/log.rb', line 44 def desttypes @desttypes end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
198 199 200 |
# File 'lib/puppet/util/log.rb', line 198 def file @file end |
#level ⇒ Object
Returns the value of attribute level.
199 200 201 |
# File 'lib/puppet/util/log.rb', line 199 def level @level end |
#line ⇒ Object
Returns the value of attribute line.
198 199 200 |
# File 'lib/puppet/util/log.rb', line 198 def line @line end |
#message ⇒ Object
Returns the value of attribute message.
199 200 201 |
# File 'lib/puppet/util/log.rb', line 199 def end |
#remote ⇒ Object
Returns the value of attribute remote.
198 199 200 |
# File 'lib/puppet/util/log.rb', line 198 def remote @remote end |
#source ⇒ Object
Returns the value of attribute source.
198 199 200 |
# File 'lib/puppet/util/log.rb', line 198 def source @source end |
#time ⇒ Object
Returns the value of attribute time.
198 199 200 |
# File 'lib/puppet/util/log.rb', line 198 def time @time end |
Class Method Details
.autoflush=(v) ⇒ Object
70 71 72 73 74 |
# File 'lib/puppet/util/log.rb', line 70 def Log.autoflush=(v) @destinations.each do |type, dest| dest.autoflush = v if dest.respond_to?(:autoflush=) end end |
.close(destination) ⇒ Object
Reset log to basics. Basically just flushes and closes files and undefs other objects.
49 50 51 52 53 54 55 |
# File 'lib/puppet/util/log.rb', line 49 def Log.close(destination) if @destinations.include?(destination) @destinations[destination].flush if @destinations[destination].respond_to?(:flush) @destinations[destination].close if @destinations[destination].respond_to?(:close) @destinations.delete(destination) end end |
.close_all ⇒ Object
57 58 59 60 61 |
# File 'lib/puppet/util/log.rb', line 57 def self.close_all destinations.keys.each { |dest| close(dest) } end |
.create(hash) ⇒ Object
Create a new log message. The primary role of this method is to avoid creating log messages below the loglevel.
78 79 80 81 82 |
# File 'lib/puppet/util/log.rb', line 78 def Log.create(hash) raise Puppet::DevError, "Logs require a level" unless hash.include?(:level) raise Puppet::DevError, "Invalid log level #{hash[:level]}" unless @levels.index(hash[:level]) @levels.index(hash[:level]) >= @loglevel ? Puppet::Util::Log.new(hash) : nil end |
.destinations ⇒ Object
84 85 86 |
# File 'lib/puppet/util/log.rb', line 84 def Log.destinations @destinations end |
.eachlevel ⇒ Object
Yield each valid level in turn
89 90 91 |
# File 'lib/puppet/util/log.rb', line 89 def Log.eachlevel @levels.each { |level| yield level } end |
.flush ⇒ Object
Flush any log destinations that support such operations.
64 65 66 67 68 |
# File 'lib/puppet/util/log.rb', line 64 def Log.flush @destinations.each { |type, dest| dest.flush if dest.respond_to?(:flush) } end |
.flushqueue ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/puppet/util/log.rb', line 160 def Log.flushqueue return unless @destinations.size >= 1 @queued.each do |msg| Log.(msg) end @queued.clear end |
.level ⇒ Object
Return the current log level.
94 95 96 |
# File 'lib/puppet/util/log.rb', line 94 def Log.level @levels[@loglevel] end |
.level=(level) ⇒ Object
Set the current log level.
99 100 101 102 103 104 105 |
# File 'lib/puppet/util/log.rb', line 99 def Log.level=(level) level = level.intern unless level.is_a?(Symbol) raise Puppet::DevError, "Invalid loglevel #{level}" unless @levels.include?(level) @loglevel = @levels.index(level) end |
.levels ⇒ Object
107 108 109 |
# File 'lib/puppet/util/log.rb', line 107 def Log.levels @levels.dup end |
.newdestination(dest) ⇒ Object
Create a new log destination.
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 |
# File 'lib/puppet/util/log.rb', line 112 def Log.newdestination(dest) # Each destination can only occur once. if @destinations.find { |name, obj| obj.name == dest } return end name, type = @desttypes.find do |name, klass| klass.match?(dest) end raise Puppet::DevError, "Unknown destination type #{dest}" unless type begin if type.instance_method(:initialize).arity == 1 @destinations[dest] = type.new(dest) else @destinations[dest] = type.new end flushqueue @destinations[dest] rescue => detail puts detail.backtrace if Puppet[:debug] # If this was our only destination, then add the console back in. newdestination(:console) if @destinations.empty? and (dest != :console and dest != "console") end end |
.newdesttype(name, options = {}, &block) ⇒ Object
Create a new destination type.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/puppet/util/log.rb', line 18 def self.newdesttype(name, = {}, &block) dest = genclass( name, :parent => Puppet::Util::Log::Destination, :prefix => "Dest", :block => block, :hash => @desttypes, :attributes => ) dest.match(dest.name) dest end |
.newmessage(msg) ⇒ Object
Route the actual message. FIXME There are lots of things this method should do, like caching and a bit more. It’s worth noting that there’s a potential for a loop here, if the machine somehow gets the destination set as itself.
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/puppet/util/log.rb', line 144 def Log.(msg) return if @levels.index(msg.level) < @loglevel (msg) if @destinations.length == 0 @destinations.each do |name, dest| threadlock(dest) do dest.handle(msg) end end end |
.queuemessage(msg) ⇒ Object
156 157 158 |
# File 'lib/puppet/util/log.rb', line 156 def Log.(msg) @queued.push(msg) end |
.reopen ⇒ Object
Reopen all of our logs.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/puppet/util/log.rb', line 173 def Log.reopen Puppet.notice "Reopening log files" types = @destinations.keys @destinations.each { |type, dest| dest.close if dest.respond_to?(:close) } @destinations.clear # We need to make sure we always end up with some kind of destination begin types.each { |type| Log.newdestination(type) } rescue => detail if @destinations.empty? Log.newdestination(:syslog) Puppet.err detail.to_s end end end |
.sendlevel?(level) ⇒ Boolean
168 169 170 |
# File 'lib/puppet/util/log.rb', line 168 def Log.sendlevel?(level) @levels.index(level) >= @loglevel end |
.validlevel?(level) ⇒ Boolean
Is the passed level a valid log level?
194 195 196 |
# File 'lib/puppet/util/log.rb', line 194 def self.validlevel?(level) @levels.include?(level) end |