Class: MongodbLogger::Logger

Inherits:
RailsLogger
  • Object
show all
Includes:
ReplicaSetHelper
Defined in:
lib/mongodb_logger/logger.rb

Constant Summary collapse

DEFAULT_COLLECTION_SIZE =
250.megabytes
CONFIGURATION_FILES =

Looks for configuration files in this order

["mongodb_logger.yml", "mongoid.yml", "database.yml"]
LOG_LEVEL_SYM =
[:debug, :info, :warn, :error, :fatal, :unknown]
ADAPTERS =
[
  ["mongo", Adapers::Mongo],
  ["moped", Adapers::Moped]
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReplicaSetHelper

#rescue_connection_failure

Constructor Details

#initialize(path = nil, level = DEBUG) ⇒ Logger

Returns a new instance of Logger.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mongodb_logger/logger.rb', line 28

def initialize(path = nil, level = DEBUG)
  set_root_and_env
  begin
    path ||= File.join(app_root, "log/#{app_env}.log")
    @level = level
    internal_initialize
  rescue => e
    # should use a config block for this
    "production" == app_env ? (raise e) : (puts "MongodbLogger WARNING: Using Rails Logger due to exception: #{e.message}")
  ensure
    if disable_file_logging?
      @log            = ::Logger.new(STDOUT)
      @log.level      = @level
    else
      super(path, @level)
    end
  end
end

Instance Attribute Details

#app_envObject (readonly)

Returns the value of attribute app_env.



25
26
27
# File 'lib/mongodb_logger/logger.rb', line 25

def app_env
  @app_env
end

#app_rootObject (readonly)

Returns the value of attribute app_root.



25
26
27
# File 'lib/mongodb_logger/logger.rb', line 25

def app_root
  @app_root
end

#db_configurationObject (readonly)

Returns the value of attribute db_configuration.



25
26
27
# File 'lib/mongodb_logger/logger.rb', line 25

def db_configuration
  @db_configuration
end

#excluded_from_logObject



86
87
88
# File 'lib/mongodb_logger/logger.rb', line 86

def excluded_from_log
  @excluded_from_log ||= nil
end

#mongo_adapterObject (readonly)

Returns the value of attribute mongo_adapter.



25
26
27
# File 'lib/mongodb_logger/logger.rb', line 25

def mongo_adapter
  @mongo_adapter
end

Instance Method Details

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



57
58
59
60
61
62
63
64
# File 'lib/mongodb_logger/logger.rb', line 57

def add(severity, message = nil, progname = nil, &block)
  $stdout.puts(message) if ENV['HEROKU_RACK'] # log in stdout on Heroku
  if @level && @level <= severity && (message.present? || progname.present?) && @mongo_record.present?
    add_log_message(severity, message, progname)
  end
  # may modify the original message
  disable_file_logging? ? message : (@level ? super : message)
end

#add_metadata(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/mongodb_logger/logger.rb', line 47

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

#mongoize(options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mongodb_logger/logger.rb', line 66

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

  runtime = Benchmark.measure{ yield }.real if block_given?
rescue Exception => e
  log_raised_error(e)
  # 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
  # error callback
  Base.on_log_exception(@mongo_record) if @mongo_record[:is_exception]
  ensure_write_to_mongodb
end