Class: Racknga::LogDatabase
- Inherits:
-
Object
- Object
- Racknga::LogDatabase
- Defined in:
- lib/racknga/log_database.rb
Overview
This is a log database based on groonga. It is used by Racknga::Middleware::Log.
Normally, #purge_old_responses is only used for log maintenance.
Instance Method Summary collapse
- #close_database ⇒ Object
- #ensure_database ⇒ Object
- #entries ⇒ Object
-
#initialize(database_path) ⇒ LogDatabase
constructor
A new instance of LogDatabase.
-
#purge_old_entries(base_time = nil) ⇒ Object
Purges old responses.
Constructor Details
#initialize(database_path) ⇒ LogDatabase
Returns a new instance of LogDatabase.
31 32 33 34 35 |
# File 'lib/racknga/log_database.rb', line 31 def initialize(database_path) @database_path = database_path @context = Groonga::Context.new(:encoding => :none) ensure_database end |
Instance Method Details
#close_database ⇒ Object
50 51 52 |
# File 'lib/racknga/log_database.rb', line 50 def close_database @database.close end |
#ensure_database ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/racknga/log_database.rb', line 41 def ensure_database if File.exist?(@database_path) @database = Groonga::Database.open(@database_path, :context => @context) else create_database end ensure_tables end |
#entries ⇒ Object
37 38 39 |
# File 'lib/racknga/log_database.rb', line 37 def entries @entries ||= @context["Entries"] end |
#purge_old_entries(base_time = nil) ⇒ Object
Purges old responses. To clear old logs, you should call this method. All records created before base_time
are removed.
You can call this method by the different process from your Rack application process. (e.g. cron.) It’s multi process safe.
removed. The default value is 1 day ago.
64 65 66 67 68 69 70 71 72 |
# File 'lib/racknga/log_database.rb', line 64 def purge_old_entries(base_time=nil) base_time ||= Time.now - 60 * 60 * 24 target_entries = entries.select do |record| record.time_stamp < base_time end target_entries.each do |entry| entry.key.delete end end |