Git Product home page Git Product logo

lograge-sql's Introduction

Lograge::Sql

Gem Version Build Status

Lograge::Sql is an extension to the famous Lograge gem, which adds SQL queries to the Lograge Event and disable default ActiveRecord logging. This is extremely useful if you're using Lograge together with the ELK stack.

Installation

Add this line to your application's Gemfile:

gem 'lograge-sql'

Usage

In order to enable SQL logging in your application, you'll simply need to add this on top of your lograge initializer:

# config/initializers/lograge
require 'lograge/sql/extension'

Customization

By default, the format is a string concatenation of the query name, the query duration and the query itself joined by \n newline:

method=GET path=/mypath format=html ...
Object Load (0.42) SELECT "objects.*" FROM "objects"
Associations Load (0.42) SELECT "associations.*" FROM "associations" WHERE "associations"."object_id" = "$1"

However, having Lograge::Formatters::Json.new, the relevant output is

{
    "sql_queries": "name1 ({duration1}) {query1}\nname2 ({duration2}) query2 ...",
    "sql_queries_count": 3
}

To customize the output:

# config/initializers/lograge.rb
Rails.application.configure do

  # Instead of extracting event as Strings, extract as Hash. You can also extract
  # additional fields to add to the formatter
  config.lograge_sql.extract_event = Proc.new do |event|
    { name: event.payload[:name], duration: event.duration.to_f.round(2), sql: event.payload[:sql] }
  end
  # Format the array of extracted events
  config.lograge_sql.formatter = Proc.new do |sql_queries|
    sql_queries
  end
end

Thread-safety

Depending on the web server in your project you might benefit from improved thread-safety by adding request_store to your Gemfile. It will be automatically picked up by lograge-sql.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/iMacTia/lograge-sql.

lograge-sql's People

Contributors

imactia avatar trammel avatar al-un avatar gkopylov avatar giovannibonetti avatar hugopeixoto avatar jamestjw avatar webuilder240 avatar silviusimeria avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.