Class: MongodbLogger::Logger

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

Direct Known Subclasses

ServerConfig::ServerLogger, Utils::Migrate::MongoMigrateLogger

Constant Summary

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

- (Logger) initialize(path = nil, level = DEBUG)

Returns a new instance of Logger



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

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

- (Object) app_env (readonly)

Returns the value of attribute app_env



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

def app_env
  @app_env
end

- (Object) app_root (readonly)

Returns the value of attribute app_root



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

def app_root
  @app_root
end

- (Object) db_configuration (readonly)

Returns the value of attribute db_configuration



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

def db_configuration
  @db_configuration
end

- (Object) excluded_from_log



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

def excluded_from_log
  @excluded_from_log ||= nil
end

- (Object) mongo_adapter (readonly)

Returns the value of attribute mongo_adapter



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

def mongo_adapter
  @mongo_adapter
end

Instance Method Details

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



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

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

- (Object) add_metadata(options = {})



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

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

- (Object) mongoize(options = {})



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

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