Class: MongodbLogger::Logger
- Inherits:
-
ActiveSupport::BufferedLogger
- Object
- ActiveSupport::BufferedLogger
- MongodbLogger::Logger
- Includes:
- ReplicaSetHelper
- Defined in:
- lib/mongodb_logger/logger.rb
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]
Instance Attribute Summary (collapse)
-
- (Object) db_configuration
readonly
Returns the value of attribute db_configuration.
-
- (Object) mongo_collection
readonly
Returns the value of attribute mongo_collection.
-
- (Object) mongo_collection_name
readonly
Returns the value of attribute mongo_collection_name.
-
- (Object) mongo_connection
readonly
Returns the value of attribute mongo_connection.
Instance Method Summary (collapse)
- - (Object) add(severity, message = nil, progname = nil, &block)
- - (Object) add_metadata(options = {})
- - (Boolean) authenticated?
-
- (Logger) initialize(options = {})
constructor
A new instance of Logger.
- - (Object) mongoize(options = {})
-
- (Object) reset_collection
Drop the capped_collection and recreate it.
Methods included from ReplicaSetHelper
Constructor Details
- (Logger) initialize(options = {})
A new instance of Logger
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mongodb_logger/logger.rb', line 18 def initialize(={}) path = [:path] || File.join(Rails.root, "log/#{Rails.env}.log") level = [:level] || DEBUG internal_initialize rescue => e # should use a config block for this Rails.env.production? ? (raise e) : (puts "MongodbLogger WARNING: Using BufferedLogger due to exception: " + e.) ensure if disable_file_logging? @level = level @buffer = {} @auto_flushing = 1 @guard = Mutex.new else super(path, level) end end |
Instance Attribute Details
- (Object) db_configuration (readonly)
Returns the value of attribute db_configuration
16 17 18 |
# File 'lib/mongodb_logger/logger.rb', line 16 def db_configuration @db_configuration end |
- (Object) mongo_collection (readonly)
Returns the value of attribute mongo_collection
16 17 18 |
# File 'lib/mongodb_logger/logger.rb', line 16 def mongo_collection @mongo_collection end |
- (Object) mongo_collection_name (readonly)
Returns the value of attribute mongo_collection_name
16 17 18 |
# File 'lib/mongodb_logger/logger.rb', line 16 def mongo_collection_name @mongo_collection_name end |
- (Object) mongo_connection (readonly)
Returns the value of attribute mongo_connection
16 17 18 |
# File 'lib/mongodb_logger/logger.rb', line 16 def mongo_connection @mongo_connection end |
Instance Method Details
- (Object) add(severity, message = nil, progname = nil, &block)
46 47 48 49 50 51 52 53 54 |
# File 'lib/mongodb_logger/logger.rb', line 46 def add(severity, = nil, progname = nil, &block) if @level && @level <= severity && .present? && @mongo_record.present? # do not modify the original message used by the buffered logger msg = logging_colorized? ? .to_s.gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip : @mongo_record[:messages][LOG_LEVEL_SYM[severity]] << msg end # may modify the original message disable_file_logging? ? : (@level ? super : ) end |
- (Object) add_metadata(options = {})
36 37 38 39 40 41 42 43 44 |
# File 'lib/mongodb_logger/logger.rb', line 36 def (={}) .each do |key, value| unless [:messages, :request_time, :ip, :runtime, :application_name, :is_exception, :params, :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 end |
- (Boolean) authenticated?
90 91 92 |
# File 'lib/mongodb_logger/logger.rb', line 90 def authenticated? @authenticated end |
- (Object) mongoize(options = {})
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mongodb_logger/logger.rb', line 64 def mongoize(={}) @mongo_record = .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. + "\n" + e.backtrace.join("\n")) # log exceptions @mongo_record[:is_exception] = true # 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 |
- (Object) reset_collection
Drop the capped_collection and recreate it
57 58 59 60 61 62 |
# File 'lib/mongodb_logger/logger.rb', line 57 def reset_collection if @mongo_connection && @mongo_collection @mongo_collection.drop create_collection end end |