Class: MongodbLogger::Logger
- Inherits:
-
RailsLogger
- Object
- ActiveSupport::BufferedLogger
- RailsLogger
- MongodbLogger::Logger
- Includes:
- ReplicaSetHelper
- Defined in:
- lib/mongodb_logger/logger.rb
Direct Known Subclasses
ServerConfig::ServerLogger, Utils::Migrate::MongoMigrateLogger
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
-
#app_env ⇒ Object
readonly
Returns the value of attribute app_env.
-
#app_root ⇒ Object
readonly
Returns the value of attribute app_root.
-
#db_configuration ⇒ Object
readonly
Returns the value of attribute db_configuration.
- #excluded_from_log ⇒ Object
-
#mongo_adapter ⇒ Object
readonly
Returns the value of attribute mongo_adapter.
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
- #add_metadata(options = {}) ⇒ Object
-
#initialize(path = nil, level = DEBUG) ⇒ Logger
constructor
A new instance of Logger.
- #mongoize(options = {}) ⇒ Object
Methods included from ReplicaSetHelper
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.}") ensure if disable_file_logging? @log = ::Logger.new(STDOUT) @log.level = @level else super(path, @level) end end end |
Instance Attribute Details
#app_env ⇒ Object (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_root ⇒ Object (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_configuration ⇒ Object (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_log ⇒ Object
86 87 88 |
# File 'lib/mongodb_logger/logger.rb', line 86 def excluded_from_log @excluded_from_log ||= nil end |
#mongo_adapter ⇒ Object (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, = nil, progname = nil, &block) $stdout.puts() if ENV['HEROKU_RACK'] # log in stdout on Heroku if @level && @level <= severity && (.present? || progname.present?) && @mongo_record.present? (severity, , progname) end # may modify the original message disable_file_logging? ? : (@level ? super : ) end |
#add_metadata(options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/mongodb_logger/logger.rb', line 47 def ( = {}) .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( = {}) @mongo_record = .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 |