Class: Termite::Logger
- Inherits:
-
Object
- Object
- Termite::Logger
- Defined in:
- lib/termite.rb
Constant Summary collapse
- LOGGER_SYSLOG_MAP =
Maps Logger severities to syslog(3) severities.
{ :unknown => :alert, :fatal => :crit, :error => :err, :warn => :warning, :info => :info, :debug => :debug, }
- RUBY_LOGGER_SEV_LABELS =
Get Ruby Logger labels for Logger-compatible output
::Logger::SEV_LABEL
- LOGGER_LEVEL_MAP =
Maps Ruby Logger log levels to their numerical values.
{}
- LEVEL_SYSLOG_MAP =
Maps Logger numerical log level values to syslog level names.
{}
- SYSLOG_SEVERITY_MAP =
Maps Syslog level names to their numerical severity levels
{}
- COLORS =
[:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :default]
Instance Attribute Summary collapse
-
#level ⇒ Object
Log level for Logger compatibility.
-
#stderr_level ⇒ Object
readonly
Returns the value of attribute stderr_level.
-
#stdout_level ⇒ Object
readonly
Returns the value of attribute stdout_level.
Class Method Summary collapse
-
.make_methods(meth) ⇒ Object
Builds a methods for level
meth
.
Instance Method Summary collapse
-
#<<(message) ⇒ Object
This isn’t perfect.
- #add(*args) ⇒ Object (also: #log)
- #add_logger(logger, options = {}) ⇒ Object
-
#initialize(logdev = nil, shift_age = nil, shift_size = nil, options = {}) ⇒ Logger
constructor
A new instance of Logger.
- #raw_add(severity, raw_message = nil, data = nil, options = {}, &block) ⇒ Object
-
#silence(temporary_level = ::Logger::ERROR) ⇒ Object
Allows messages of a particular log level to be ignored temporarily.
- #socket ⇒ Object
Constructor Details
#initialize(logdev = nil, shift_age = nil, shift_size = nil, options = {}) ⇒ Logger
Returns a new instance of Logger.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/termite.rb', line 86 def initialize(logdev = nil, shift_age = nil, shift_size = nil, = {}) if logdev.is_a?(Hash) = logdev logdev = nil end @loggers ||= [] @log_filename ||= logdev @shift_age ||= shift_age @shift_size ||= shift_size Ecology.read read_ecology_data() end |
Instance Attribute Details
#level ⇒ Object
Log level for Logger compatibility.
82 83 84 |
# File 'lib/termite.rb', line 82 def level @level end |
#stderr_level ⇒ Object (readonly)
Returns the value of attribute stderr_level.
84 85 86 |
# File 'lib/termite.rb', line 84 def stderr_level @stderr_level end |
#stdout_level ⇒ Object (readonly)
Returns the value of attribute stdout_level.
83 84 85 |
# File 'lib/termite.rb', line 83 def stdout_level @stdout_level end |
Class Method Details
.make_methods(meth) ⇒ Object
Builds a methods for level meth
.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/termite.rb', line 62 def self.make_methods(meth) eval <<-EOM, nil, __FILE__, __LINE__ + 1 def #{meth}(message = nil, data = {}, options = {}, &block) return true if #{LOGGER_LEVEL_MAP[meth]} < @level add(::Logger::#{meth.to_s.upcase}, message, data, options, &block) end def #{meth}? @level <= ::Logger::#{meth.to_s.upcase} end EOM end |
Instance Method Details
#<<(message) ⇒ Object
This isn’t perfect. “Real” Ruby loggers use << to mean “write directly to the underlying store without adding headers or other cruft”. That’s meaningful for file loggers, but not for syslog.
357 358 359 |
# File 'lib/termite.rb', line 357 def << () add(::Logger::INFO, ) end |
#add(*args) ⇒ Object Also known as: log
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/termite.rb', line 340 def add(*args) begin raw_add(*args) rescue Exception => e STDERR.puts("Couldn't log via Termite! Failing! Arguments:") STDERR.puts(args.inspect) STDERR.puts("Exception: #{e.}") STDERR.puts("Backtrace: #{e.backtrace.inspect}") end end |
#add_logger(logger, options = {}) ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/termite.rb', line 266 def add_logger(logger, ={}) if logger.is_a? Hash @loggers << logger else ["logger"] = logger @loggers << end end |
#raw_add(severity, raw_message = nil, data = nil, options = {}, &block) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/termite.rb', line 281 def raw_add(severity, = nil, data = nil, = {}, &block) # Severity is a numerical severity using Ruby Logger's scale severity ||= ::Logger::UNKNOWN return true if severity < @level application = [:application] || @application component = @default_component component = [:component] if .has_key?(:component) combined_app = application + ":" + component if component app_data = {:combined => combined_app, :app => application, :component => component} data ||= {} if data.is_a?(Hash) data = @default_fields.merge(data) data = MultiJson.dump(data) elsif data.is_a?(String) # Can't merge a JSON string with default data raise "Can't merge a JSON string with extra fields!" unless @default_fields.empty? else raise "Unknown data object passed as JSON!" end time = Time.now = if elsif block block.call else "Error! Logger called with no message or block to execute! Trace: #{caller.join(", ")}" end = clean() # Lifted from Logger::Formatter ruby_logger_severity = RUBY_LOGGER_SEV_LABELS[severity] = "%s, [%s#%d] %5s -- %s: %s" % [ruby_logger_severity[0..0], (time.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " % time.usec), $$, ruby_logger_severity, "", ] @loggers.each do |sink| next if (sink["min_level"] && severity < string_to_severity(sink["min_level"])) || (sink["max_level"] && severity > string_to_severity(sink["max_level"])) || sink["logger"].nil? = sink["logger_prefix?"] ? : += " #{data}" if sink["logger_data?"] if sink["logger"].respond_to?(:send_message) sink["logger"].(severity, , app_data, time, data) else += "\n" if sink["newline?"] && sink["newline?"] != "false" if sink["color"] color = (COLORS.include? sink["color"].to_sym) ? sink["color"].to_sym : sink["color"] = .color(color) end sink["logger"] << end rescue nil end true end |
#silence(temporary_level = ::Logger::ERROR) ⇒ Object
Allows messages of a particular log level to be ignored temporarily.
Can you say “Broken Windows”?
366 367 368 369 370 371 372 |
# File 'lib/termite.rb', line 366 def silence(temporary_level = ::Logger::ERROR) old_logger_level = @level @level = temporary_level yield ensure @level = old_logger_level end |
#socket ⇒ Object
275 276 277 |
# File 'lib/termite.rb', line 275 def socket @socket end |