Class: MongoidLogger::Base
- Inherits:
-
ActiveSupport::BufferedLogger
- Object
- ActiveSupport::BufferedLogger
- MongoidLogger::Base
- Defined in:
- lib/mongoid_logger/base.rb
Instance Attribute Summary collapse
-
#mongo_collection_names ⇒ Object
readonly
Returns the value of attribute mongo_collection_names.
Class Method Summary collapse
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &blk) ⇒ Object
- #add_metadata(options = {}) ⇒ Object
- #confirm_collection ⇒ Object
- #create_collection(name) ⇒ Object
- #enable_mongoid_logging(controller, options = {}) ⇒ Object
-
#initialize(path, options = {}) ⇒ Base
constructor
A new instance of Base.
- #mongo_collection_name_array ⇒ Object
- #reset_collection ⇒ Object
Constructor Details
#initialize(path, options = {}) ⇒ Base
Returns a new instance of Base.
13 14 15 16 17 18 |
# File 'lib/mongoid_logger/base.rb', line 13 def initialize(path, ={}) @path = path @level = [:level] || DEBUG configure() super(@path, @level) end |
Instance Attribute Details
#mongo_collection_names ⇒ Object (readonly)
Returns the value of attribute mongo_collection_names.
88 89 90 |
# File 'lib/mongoid_logger/base.rb', line 88 def mongo_collection_names @mongo_collection_names end |
Class Method Details
.collection_names ⇒ Object
8 9 10 |
# File 'lib/mongoid_logger/base.rb', line 8 def collection_names @collection_names ||= [] end |
Instance Method Details
#add(severity, message = nil, progname = nil, &blk) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/mongoid_logger/base.rb', line 20 def add(severity, =nil, progname=nil, &blk) if @level <= severity and .present? and @record.present? @record[:messages] ||= [] @record[:messages] << [severity, ] end super end |
#add_metadata(options = {}) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/mongoid_logger/base.rb', line 28 def (={}) if @record .each_pair do |key, value| @record[key] = value end end end |
#confirm_collection ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/mongoid_logger/base.rb', line 73 def confirm_collection mongo_collection_name_array.each do |name| unless @session.collections.find{|col| col.name == name } create_collection(name) end end end |
#create_collection(name) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/mongoid_logger/base.rb', line 63 def create_collection(name) @session.command(create: name, capped: true, size: @db_configuration["capsize"] || 64.megabyte) rescue Exception => e if e. =~ /collection already exists/ internal_log(:warn, "Ignore #{name} creation failure. See following message of #{e.class.name}:\n" << e.) else raise e end end |
#enable_mongoid_logging(controller, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mongoid_logger/base.rb', line 36 def enable_mongoid_logging(controller, ={}) @record = .merge({ :messages => [], :request_time => Time.now.getutc, :application_name => @application_name, :host => Socket.gethostname, :pid => Process.pid, }) st = Time.now yield ed = Time.now @record["runtime"] = (ed - st).to_f @record["status"] = controller ? controller.response.status : 200 rescue Exception if st @record["runtime"] = (Time.now - st).to_f end @record["status"] = 500 add(3, $!. + "\n " + $!.backtrace.join("\n ")) #rescue nil raise ensure begin insert_document(@record) rescue end end |
#mongo_collection_name_array ⇒ Object
90 91 92 |
# File 'lib/mongoid_logger/base.rb', line 90 def mongo_collection_name_array (@mongo_collection_names.values + [@mongo_collection_names.default]).compact.uniq end |
#reset_collection ⇒ Object
81 82 83 84 85 86 |
# File 'lib/mongoid_logger/base.rb', line 81 def reset_collection mongo_collection_name_array.each do |name| @session.command(drop: name) create_collection(name) end end |