Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

queryable_logs

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

queryable_logs

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

QueryableLogs

Query your logs using good old ActiveRecord.

Installation

Add this line to your application's Gemfile:

gem 'queryable_logs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install queryable_logs

Run rails g queryable_logs.

This will generate a migration file.

class CreateTrailLogs < ActiveRecord::Migration
  def change
    create_table :trail_logs do |t|
      t.integer :user_id
      t.string :ip_address
      t.string :controller
      t.string :action
      t.string :format
      t.string :http_verb
      t.text :params_hash
      t.datetime :logged_at
      t.string :response_code
      t.string :request_url
      t.string :sig

      t.timestamps null: false
    end
  end
end

and an initializer file.

class Trail
  cattr_accessor :current_user_method, :logger, :saving_logs
  LogFile = Rails.root.join('log', 'trail.log')
  delegate :debug, :info, :warn, :error, :fatal, :to => :logger
end

Trail.logger = Logger.new(Trail::LogFile)
Trail.logger.level = 'info' # could be debug, info, warn, error or fatal
Trail.current_user_method = :current_user

queryable_logs also logs the current user id. Let the gem know which method you are using to get the current user. Default is set to current_user.

Finally, include the QueryableLogs::WriteLog in the base controller, typically the ApplicationController.

class ApplicationController < ActionController::Base
  include QueryableLogs::WriteLog
end

Enter the following task to your crontab rake parse:logs_to_db

eg: 0 * * * * cd /Users/akshaytakkar/sample_rails_app && /Users/akshaytakkar/.rvm/wrappers/ruby-3.1.0/rake db:parse_log_and_save_trails >> /Users/akshaytakkar/sample_rails_app/log/worker.log 2>&1

This will run the rake parse:logs_to_db rake task every hour and log any errors or output from the task to worker.log file.

Usage

Use QueryableLogs::TrailLog like any ActiveRecord object. eg: query how many requests you got for the posts controller index action. QueryableLogs::TrailLog.where(controller: 'posts', action: 'index').count

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the QueryableLogs project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

FAQs

Package last updated on 13 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc