Class: CentralLogger::MongoLogger

Inherits:
ActiveSupport::BufferedLogger
  • Object
show all
Includes:
ReplicaSetHelper
Defined in:
lib/central_logger/mongo_logger.rb

Constant Summary collapse

PRODUCTION_COLLECTION_SIZE =
250.megabytes
DEFAULT_COLLECTION_SIZE =
100.megabytes
CONFIGURATION_FILES =

Looks for configuration files in this order

["central_logger.yml", "mongoid.yml", "database.yml"]
LOG_LEVEL_SYM =
[:debug, :info, :warn, :error, :fatal, :unknown]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReplicaSetHelper

#rescue_connection_failure

Constructor Details

#initialize(options = {}) ⇒ MongoLogger

Returns a new instance of MongoLogger.



19
20
21
22
23
24
25
26
27
# File 'lib/central_logger/mongo_logger.rb', line 19

def initialize(options={})
  path = options[:path] || File.join(Rails.root, "log/#{Rails.env}.log")
  level = options[:level] || DEBUG
  super(path, level)
  internal_initialize
rescue => e
  # should use a config block for this
  Rails.env.production? ? (raise e) : (puts "Using BufferedLogger due to exception: " + e.message)
end

Instance Attribute Details

#db_configurationObject (readonly)

Returns the value of attribute db_configuration.



17
18
19
# File 'lib/central_logger/mongo_logger.rb', line 17

def db_configuration
  @db_configuration
end

#mongo_collectionObject (readonly)

Returns the value of attribute mongo_collection.



17
18
19
# File 'lib/central_logger/mongo_logger.rb', line 17

def mongo_collection
  @mongo_collection
end

#mongo_collection_nameObject (readonly)

Returns the value of attribute mongo_collection_name.



17
18
19
# File 'lib/central_logger/mongo_logger.rb', line 17

def mongo_collection_name
  @mongo_collection_name
end

#mongo_connectionObject (readonly)

Returns the value of attribute mongo_connection.



17
18
19
# File 'lib/central_logger/mongo_logger.rb', line 17

def mongo_connection
  @mongo_connection
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/central_logger/mongo_logger.rb', line 39

def add(severity, message = nil, progname = nil, &block)
  if @level <= severity && message.present? && @mongo_record.present?
    # do not modify the original message used by the buffered logger
    msg = logging_colorized? ? message.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip : message
    @mongo_record[:messages][LOG_LEVEL_SYM[severity]] << msg
  end
  # may modify the original message
  super
end

#add_metadata(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/central_logger/mongo_logger.rb', line 29

def (options={})
  options.each_pair do |key, value|
    unless [:messages, :request_time, :ip, :runtime, :application_name].include?(key.to_sym)
      @mongo_record[key] = value
    else
      raise ArgumentError, ":#{key} is a reserved key for the central logger. Please choose a different key"
    end
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/central_logger/mongo_logger.rb', line 79

def authenticated?
  @authenticated
end

#mongoize(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/central_logger/mongo_logger.rb', line 55

def mongoize(options={})
  @mongo_record = options.merge({
    :messages => Hash.new { |hash, key| hash[key] = Array.new },
    :request_time => Time.now.getutc,
    :application_name => @application_name
  })

  runtime = Benchmark.measure{ yield }.real if block_given?
rescue Exception => e
  add(3, e.message + "\n" + e.backtrace.join("\n"))
  # Reraise the exception for anyone else who cares
  raise e
ensure
  # In case of exception, make sure runtime is set
  @mongo_record[:runtime] = ((runtime ||= 0) * 1000).ceil
  begin
    @insert_block.call
  rescue
    # do extra work to inpect (and flatten)
    force_serialize @mongo_record
    @insert_block.call rescue nil
  end
end

#reset_collectionObject

Drop the capped_collection and recreate it



50
51
52
53
# File 'lib/central_logger/mongo_logger.rb', line 50

def reset_collection
  @mongo_collection.drop
  create_collection
end