Class: CentralLogger::MongoLogger
- Inherits:
-
ActiveSupport::BufferedLogger
- Object
- ActiveSupport::BufferedLogger
- CentralLogger::MongoLogger
- 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
-
#db_configuration ⇒ Object
readonly
Returns the value of attribute db_configuration.
-
#mongo_collection ⇒ Object
readonly
Returns the value of attribute mongo_collection.
-
#mongo_collection_name ⇒ Object
readonly
Returns the value of attribute mongo_collection_name.
-
#mongo_connection ⇒ Object
readonly
Returns the value of attribute mongo_connection.
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
- #add_metadata(options = {}) ⇒ Object
- #authenticated? ⇒ Boolean
-
#initialize(options = {}) ⇒ MongoLogger
constructor
A new instance of MongoLogger.
- #mongoize(options = {}) ⇒ Object
-
#reset_collection ⇒ Object
Drop the capped_collection and recreate it.
Methods included from ReplicaSetHelper
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(={}) path = [:path] || File.join(Rails.root, "log/#{Rails.env}.log") level = [: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.) end |
Instance Attribute Details
#db_configuration ⇒ Object (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_collection ⇒ Object (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_name ⇒ Object (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_connection ⇒ Object (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, = nil, progname = nil, &block) if @level <= severity && .present? && @mongo_record.present? # do not modify the original message used by the buffered logger msg = logging_colorized? ? .gsub(/(\e(\[([\d;]*[mz]?))?)?/, '').strip : @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 (={}) .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
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(={}) @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")) # 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_collection ⇒ Object
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 |