Sha256: f7af677c7336453cf0c5d0dad3f16ec400f1f73faee01500fa267357501fa5cf

Contents?: true

Size: 1.67 KB

Versions: 18

Compression:

Stored size: 1.67 KB

Contents

class Marty::Log < Marty::Base

  def self.logfile
    @logfile ||= Rails.root.join('log', Rails.env + '.sql').to_s
  end

  establish_connection({
                         adapter:  "sqlite3",
                         database: logfile
                       })
  self.table_name = "log"
  self.primary_key = "id"

  def self.db_init
    db = SQLite3::Database.new(Marty::Log.logfile)
    db.execute <<-SQL
      CREATE TABLE IF NOT EXISTS log (
      id INTEGER PRIMARY KEY,
      message_type TEXT,
      message TEXT,
      timestamp REAL,
      details BLOB )
      SQL
    db
  end

  def self.write_log(type, message, details)
    begin
      @db ||= db_init
      stmt = @db.prepare <<-SQL
          INSERT INTO log (message_type, message, timestamp, details)
            values (?, ?, ?, ?)
          SQL
      stmt.bind_param(1, type.to_s)
      stmt.bind_param(2, message)
      stmt.bind_param(3, Time.zone.now.to_f)
      stmt.bind_param(4, details.pretty_inspect)

      sent = false
      retries = 3
      delay = 0.1
      until sent
        begin
          stmt.execute
          sent = true
        rescue SQLite3::BusyException
          raise if retries == 0
          retries -= 1
          sleep delay
        end
      end

    rescue => e
      Marty::Util.logger.error("Marty::Logger failure: #{e.message}")
    ensure
      stmt.close if stmt rescue nil
    end
  end

  def self.cleanup(days_to_keep)
    raise "Must give numeric value. (Got '#{days_to_keep}')" unless
      (Float(days_to_keep) rescue false)
    @db ||= db_init
    cutoff = Time.zone.now.to_i - days_to_keep.to_i*60*60*24
    @db.execute <<-SQL
       delete from log where timestamp <= #{cutoff}
    SQL
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
marty-1.0.43 app/models/marty/log.rb
marty-1.0.42 app/models/marty/log.rb
marty-1.0.41 app/models/marty/log.rb
marty-1.0.39 app/models/marty/log.rb
marty-1.0.38 app/models/marty/log.rb
marty-1.0.37 app/models/marty/log.rb
marty-1.0.36 app/models/marty/log.rb
marty-1.0.35 app/models/marty/log.rb
marty-1.0.34 app/models/marty/log.rb
marty-1.0.33 app/models/marty/log.rb
marty-1.0.32 app/models/marty/log.rb
marty-1.0.31 app/models/marty/log.rb
marty-1.0.30 app/models/marty/log.rb
marty-1.0.29 app/models/marty/log.rb
marty-1.0.28 app/models/marty/log.rb
marty-1.0.27 app/models/marty/log.rb
marty-1.0.26 app/models/marty/log.rb
marty-1.0.25 app/models/marty/log.rb