Class: Termite::Logger

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  if logdev.is_a?(Hash)
    options = logdev
    logdev = nil
  end

  @loggers ||= []
  @log_filename ||= logdev
  @shift_age ||= shift_age
  @shift_size ||= shift_size

  Ecology.read
  read_ecology_data(options)
end

Instance Attribute Details

#levelObject

Log level for Logger compatibility.



82
83
84
# File 'lib/termite.rb', line 82

def level
  @level
end

#stderr_levelObject (readonly)

Returns the value of attribute stderr_level.



84
85
86
# File 'lib/termite.rb', line 84

def stderr_level
  @stderr_level
end

#stdout_levelObject (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 << (message)
  add(::Logger::INFO, message)
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.message}")
    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, options={})
  if logger.is_a? Hash
    @loggers << logger
  else
    options["logger"] = logger
    @loggers << options
  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, raw_message = nil, data = nil, options = {}, &block)
  # Severity is a numerical severity using Ruby Logger's scale
  severity ||= ::Logger::UNKNOWN
  return true if severity < @level

  application =  options[:application] || @application
  component = @default_component
  component = options[:component] if options.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
  full_message = if raw_message
    raw_message
  elsif block
    block.call
  else
    "Error! Logger called with no message or block to execute! Trace: #{caller.join(", ")}"
  end
  full_message = clean(full_message)
  # Lifted from Logger::Formatter
  ruby_logger_severity = RUBY_LOGGER_SEV_LABELS[severity]
  ruby_logger_message = "%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, "", full_message]

  @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?
    message = sink["logger_prefix?"] ? ruby_logger_message : full_message
    message += " #{data}" if sink["logger_data?"]
    if sink["logger"].respond_to?(:send_message)
      sink["logger"].send_message(severity, message, app_data, time, data)
    else
      message += "\n" if sink["newline?"] && sink["newline?"] != "false"
      if sink["color"]
        color = (COLORS.include? sink["color"].to_sym) ? sink["color"].to_sym : sink["color"]
        message = message.color(color)
      end
      sink["logger"] << message
    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

#socketObject



275
276
277
# File 'lib/termite.rb', line 275

def socket
  @socket
end