maiha-rows_logger 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README +104 -0
- data/Rakefile +22 -0
- data/adapters/mysql.rb +6 -0
- data/adapters/postgresql.rb +10 -0
- data/init.rb +65 -0
- data/install.rb +1 -0
- data/lib/rows_logger.rb +1 -0
- data/tasks/rows_logger_tasks.rake +4 -0
- data/test/rows_logger_test.rb +8 -0
- metadata +62 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
RowsLogger
|
2
|
+
==========
|
3
|
+
|
4
|
+
This plugin offers rich information about result sets to AR logs.
|
5
|
+
|
6
|
+
|
7
|
+
Usage
|
8
|
+
=====
|
9
|
+
|
10
|
+
Just enjoy Rails as usual.
|
11
|
+
|
12
|
+
|
13
|
+
Example
|
14
|
+
=======
|
15
|
+
|
16
|
+
Consider some read operations like this.
|
17
|
+
|
18
|
+
Member.count
|
19
|
+
Member.find(:all)
|
20
|
+
|
21
|
+
That usually makes following log.
|
22
|
+
|
23
|
+
SQL (0.000300) SELECT count(*) AS count_all FROM members
|
24
|
+
Member Load (0.000482) SELECT * FROM members
|
25
|
+
|
26
|
+
RowsLogger appends information about rows count to the log.
|
27
|
+
|
28
|
+
SQL (0.000301) (1 Row) SELECT count(*) AS count_all FROM members
|
29
|
+
Member Load (0.000415) (3 Rows) SELECT * FROM members
|
30
|
+
|
31
|
+
|
32
|
+
For Developpers
|
33
|
+
===============
|
34
|
+
|
35
|
+
This plugin modfies following methods.
|
36
|
+
'ConnectionAdapters::AbstractAdapter#log'
|
37
|
+
'ConnectionAdapters::AbstractAdapter#log_info'
|
38
|
+
|
39
|
+
<before>
|
40
|
+
ConnectionAdapters::AbstractAdapter#log
|
41
|
+
--> log_info(sql, name, seconds)
|
42
|
+
|
43
|
+
<after>
|
44
|
+
ConnectionAdapters::AbstractAdapter#log
|
45
|
+
--> log_info(sql, name, seconds, result = nil)
|
46
|
+
--> log_result_info(result)
|
47
|
+
--> ConcreteAdapter#count_result(result)
|
48
|
+
|
49
|
+
|
50
|
+
<an example for concrete adapter>
|
51
|
+
ConnectionAdapters::MysqlAdapter#count_result
|
52
|
+
protected
|
53
|
+
def count_result(result)
|
54
|
+
result.num_rows
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
Count Result Method
|
59
|
+
===================
|
60
|
+
|
61
|
+
The 'count_result' method of Adapter class should return count of result set
|
62
|
+
from 'result' object, where 'result' is an object generated by 'log' method.
|
63
|
+
This is used as result information.
|
64
|
+
|
65
|
+
For exmaple, although this is nonsencial definition,
|
66
|
+
|
67
|
+
def count_result(result)
|
68
|
+
0
|
69
|
+
end
|
70
|
+
|
71
|
+
this code always appends "(0 Rows)" to the log. The returned value is directly
|
72
|
+
used even if it is not a numeric value. But no information will be appended
|
73
|
+
in following cases.
|
74
|
+
|
75
|
+
1) when 'count_result' method returns nil
|
76
|
+
2) when 'count_result' method is not defined in current adapter
|
77
|
+
|
78
|
+
|
79
|
+
Note
|
80
|
+
====
|
81
|
+
|
82
|
+
'count_result' method should be defined as 'protected' or 'public' because
|
83
|
+
we check whether it is implemented or not in current adapter by using
|
84
|
+
'respond_to?' method.
|
85
|
+
|
86
|
+
|
87
|
+
Supported Databases
|
88
|
+
===================
|
89
|
+
|
90
|
+
Currently these databases are supported.
|
91
|
+
|
92
|
+
* PostgreSQL
|
93
|
+
* MySQL
|
94
|
+
|
95
|
+
|
96
|
+
For Other Databases
|
97
|
+
===================
|
98
|
+
|
99
|
+
Put a new adapter file that contains 'count_result' method for
|
100
|
+
your database adapter into adatpers directory.
|
101
|
+
|
102
|
+
|
103
|
+
Copyright (c) 2008 maiha@wota.jp, released under the MIT license
|
104
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the rows_logger plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the rows_logger plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'RowsLogger'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/adapters/mysql.rb
ADDED
data/init.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Include hook code here
|
2
|
+
|
3
|
+
Dir["#{ File.dirname(__FILE__) }/adapters/*.rb"].each do |path|
|
4
|
+
adapter = File.basename(path, '.rb')
|
5
|
+
if ActiveRecord::Base.respond_to?("#{adapter}_connection")
|
6
|
+
ActiveRecord::Base.logger.debug "RowsLogger plugin enables #{adapter}"
|
7
|
+
require path
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
ActiveRecord::Base.class_eval do
|
13
|
+
cattr_accessor :disable_cache_logging
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
|
17
|
+
protected
|
18
|
+
def log(sql, name)
|
19
|
+
if block_given?
|
20
|
+
if @logger and @logger.level <= Logger::INFO
|
21
|
+
result = nil
|
22
|
+
seconds = Benchmark.realtime { result = yield }
|
23
|
+
@runtime += seconds
|
24
|
+
log_info(sql, name, seconds, result)
|
25
|
+
result
|
26
|
+
else
|
27
|
+
yield
|
28
|
+
end
|
29
|
+
else
|
30
|
+
log_info(sql, name, 0)
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
rescue Exception => e
|
34
|
+
# Log message and raise exception.
|
35
|
+
# Set last_verfication to 0, so that connection gets verified
|
36
|
+
# upon reentering the request loop
|
37
|
+
@last_verification = 0
|
38
|
+
message = "#{e.class.name}: #{e.message}: #{sql}"
|
39
|
+
log_info(message, name, 0)
|
40
|
+
raise ActiveRecord::StatementInvalid, message
|
41
|
+
end
|
42
|
+
|
43
|
+
def log_info(sql, name, runtime, result = nil)
|
44
|
+
return unless @logger
|
45
|
+
return if name == "CACHE" and ActiveRecord::Base.disable_cache_logging
|
46
|
+
|
47
|
+
@logger.debug(
|
48
|
+
format_log_entry(
|
49
|
+
"#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})#{log_result_info(result)}",
|
50
|
+
sql.gsub(/ +/, " ")
|
51
|
+
)
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def log_result_info(result)
|
56
|
+
return nil if result.nil? or !respond_to?(:count_result)
|
57
|
+
|
58
|
+
count = count_result(result) rescue '?' or return nil
|
59
|
+
unit = (count == 1) ? 'Row' : 'Rows'
|
60
|
+
" (%s %s)" % [count, unit]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/rows_logger.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# RowsLogger
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: maiha-rows_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- maiha
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-11-10 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: maiha@wota.jp
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README
|
27
|
+
- Rakefile
|
28
|
+
- adapters/mysql.rb
|
29
|
+
- adapters/postgresql.rb
|
30
|
+
- init.rb
|
31
|
+
- install.rb
|
32
|
+
- lib/rows_logger.rb
|
33
|
+
- tasks/rows_logger_tasks.rake
|
34
|
+
- test/rows_logger_test.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/maiha/rows_logger
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: ""
|
61
|
+
test_files: []
|
62
|
+
|