lograge-sql 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f057e0f18be0a66527bf7bed2c8c217ca95e731304bbaae193efd24e0a764db3
4
- data.tar.gz: e84b14ff93214088f99de92228be072a727e834727b362aa4c772afd93ab5fb8
3
+ metadata.gz: 6c781f369bfdc3b5ee7fa14fe03146c929e67a90b3558933e88d520d1908324e
4
+ data.tar.gz: c498398d6d5089d9f8362e1f0ab0fec680cfdd80e397195ce75ce6a6860ab951
5
5
  SHA512:
6
- metadata.gz: 7388da0b428389ab33f38476fd717452b5a144ece741531397c43a6c43a97a0eb00235020c89441d34a6977dc6507d6c29678e45d5ecbb30f345f6d80c0cd7a7
7
- data.tar.gz: 92c0643b0a37b3f0928be58115ada58d3691b0d2505806f9a97e1226fb3fb7f07e59de7ddcc1a09cad0648f08c11b033d7202d19bb652f205954580b2cb63366
6
+ metadata.gz: 265b36d72f2b0a9f6fcdd0f4af6cd07e04088df14be819e3499351230ff314cec3efa07443a2d24d7b0136df9389fb16c0041fae73b1930b01eaa4466c6c8254
7
+ data.tar.gz: 623aeb11fcb1b9633b72b54e5d001d84ac558f2fea2fee1fdb7eb43e662e0b756ab2dfdfc99abb55749a657844b51caec94ea8609e2a4122d7cd503289a50d9a
@@ -1,4 +1,14 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.2
4
- before_install: gem install bundler -v 1.11.2
3
+ - 2.5
4
+ before_install: gem install bundler -v 1.16.2
5
+ deploy:
6
+ provider: rubygems
7
+ api_key:
8
+ secure: xlc4BM+5ljo4cO2FC71KbhbVGVYd3NMxhZPat/Y37vo6b5/I01D+Z+K84azMvhIZa+O17cA56FGqT6P0XYfcjZVnYzixpyRlbffKBGNPcIFj2UklWk2BDOojezHPEQnbR2f0jE2+8jHtAb9qyoBTbTlvsW3+svd76+53TxMW1PgGF9+1xdtlEumF1q+IYRL+5TzDLk9GyQ+3RmVd9KBVvQIxxUpn4COJohmQWnv98ZzluX4I/QUt3k4o+etUINPewyg3S+VNP0u+K9HGkY3TL8OpUbCS3Syr3OydwzgpXIBlW+kWVez5/qZn3pd6oSpNEUF9Mh3NhaVsDacFSh6XgGNgww+isazyAROsledK2GsEOH0Hi6gpia/Ny5cD5TFAiGshMcUIb2K4roI3AW9kJ2e59hoC+QvPy64IybO/dhaKcxwX8F4qq3Fk/8EzQV5eOTq0Iai5aYNJaGZUqHV/X4DiMwDk/JoyQVYE+2CMF/PlZIFV4LuegDArVXYh5w3Qo9vYDfNp7Hfv0w5XgpEqAhk+Dabl+BlVhzSM/Cf28i6OVAhLURnGMNKhQEi3/4EusaM/cOQlM5QKWweAPfOwDKMFUoj9+Rtr0FfSyNzerG9dlGuzEKOlqq+XuOQ7RfDUV4+bn2PwbYo5XwMKz1Y8FWUZTz6l14miaw8Yq7U0GU8=
9
+ gem: lograge-sql
10
+ on:
11
+ tags: true
12
+ repo: iMacTia/lograge-sql
13
+ rvm: 2.5
14
+
data/README.md CHANGED
@@ -22,6 +22,44 @@ In order to enable SQL logging in your application, you'll simply need to add th
22
22
  require 'lograge/sql/extension'
23
23
  ```
24
24
 
25
+ ## Customization
26
+
27
+ By default, the format is a string concatenation of the query name, the query duration and the query itself joined by `\n` newline:
28
+
29
+ ```
30
+ method=GET path=/mypath format=html ...
31
+ Object Load (0.42) SELECT "objects.*" FROM "objects"
32
+ Associations Load (0.42) SELECT "associations.*" FROM "associations" WHERE "associations"."object_id" = "$1"
33
+ ```
34
+
35
+ However, having `Lograge::Formatters::Json.new`, the relevant output is
36
+
37
+ ```json
38
+ {
39
+ "sql_queries": "name1 ({duration1}) {query1}\nname2 ({duration2}) query2 ...",
40
+ "sql_queries_count": 3
41
+ }
42
+ ```
43
+
44
+ To customize the output:
45
+
46
+ ```ruby
47
+ # config/initializers/lograge.rb
48
+ Rails.application.configure do
49
+
50
+ # Instead of extracting event as Strings, extract as Hash. You can also extract
51
+ # additional fields to add to the formatter
52
+ config.lograge_sql.extract_event = Proc.new do |event|
53
+ { name: event.payload[:name], duration: event.duration.to_f.round(2), sql: event.payload[:sql] }
54
+ end
55
+ # Format the array of extracted events
56
+ config.lograge_sql.formatter = Proc.new do |sql_queries|
57
+ sql_queries
58
+ end
59
+ end
60
+ ```
61
+
62
+
25
63
  ## Contributing
26
64
 
27
65
  Bug reports and pull requests are welcome on GitHub at https://github.com/iMacTia/lograge-sql.
@@ -2,6 +2,41 @@ require 'lograge/sql/version'
2
2
 
3
3
  module Lograge
4
4
  module Sql
5
+
6
+ class << self
7
+
8
+ # Format SQL log
9
+ attr_accessor :formatter
10
+ # Extract information from SQL event
11
+ attr_accessor :extract_event
12
+
13
+ # Initialise configuration with fallback to default values
14
+ def setup(config)
15
+ Lograge::Sql.formatter = config.formatter || default_formatter
16
+ Lograge::Sql.extract_event = config.extract_event || default_extract_event
17
+ end
18
+
19
+ private
20
+
21
+ # By default, the output is a concatenated string of all extracted events
22
+ def default_formatter
23
+ Proc.new do |sql_queries|
24
+ %('#{sql_queries.join("\n")}')
25
+ end
26
+ end
27
+
28
+ # By default, only extract values required for the default_formatter and
29
+ # already convert to a string
30
+ def default_extract_event
31
+ Proc.new do |event|
32
+ "#{event.payload[:name]} (#{event.duration.to_f.round(2)}) #{event.payload[:sql]}"
33
+ end
34
+ end
35
+
36
+ end
5
37
 
6
38
  end
7
39
  end
40
+
41
+ # Rails specific configuration
42
+ require 'lograge/sql/railtie' if defined?(Rails)
@@ -11,7 +11,7 @@ module Lograge
11
11
 
12
12
  Thread.current[:lograge_sql_queries] = nil
13
13
  {
14
- sql_queries: %('#{sql_queries.join("\n")}'),
14
+ sql_queries: Lograge::Sql.formatter.call(sql_queries),
15
15
  sql_queries_count: sql_queries.length
16
16
  }
17
17
  end
@@ -25,7 +25,7 @@ module Lograge
25
25
  ActiveRecord::LogSubscriber.runtime += event.duration
26
26
  return if event.payload[:name] == 'SCHEMA'
27
27
  Thread.current[:lograge_sql_queries] ||= []
28
- Thread.current[:lograge_sql_queries] << ("#{event.payload[:name]} (#{event.duration.to_f.round(2)}) #{event.payload[:sql]}")
28
+ Thread.current[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
29
29
  end
30
30
  end
31
31
  end
@@ -0,0 +1,15 @@
1
+ require 'rails/railtie'
2
+ require 'active_support/ordered_options'
3
+
4
+ module Lograge
5
+ module Sql
6
+ class Railtie < Rails::Railtie
7
+ # To ensure that configuration is not nil when initialise Lograge::Sql.setup
8
+ config.lograge_sql = ActiveSupport::OrderedOptions.new
9
+
10
+ config.after_initialize do |app|
11
+ Lograge::Sql.setup(app.config.lograge_sql)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  module Lograge
2
2
  module Sql
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lograge-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Giuffrida
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-07 00:00:00.000000000 Z
11
+ date: 2018-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lograge
@@ -103,6 +103,7 @@ files:
103
103
  - bin/setup
104
104
  - lib/lograge/sql.rb
105
105
  - lib/lograge/sql/extension.rb
106
+ - lib/lograge/sql/railtie.rb
106
107
  - lib/lograge/sql/version.rb
107
108
  - lograge-sql.gemspec
108
109
  homepage: https://github.com/iMacTia/lograge-sql
@@ -125,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
126
  version: '0'
126
127
  requirements: []
127
128
  rubyforge_project:
128
- rubygems_version: 2.7.7
129
+ rubygems_version: 2.7.8
129
130
  signing_key:
130
131
  specification_version: 4
131
132
  summary: An extension for Lograge to log SQL queries