pg_logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6afdebc43b6ddb32e59f81810ec40be2d41a0691
4
+ data.tar.gz: 7c42740f65d80c3579dbe3dcb3cab6792fe91982
5
+ SHA512:
6
+ metadata.gz: 920058d517e5bee8757e0041dd4c42a6605470c843052f8ded2ceb177a7e69bde24a0c30285e23533bc25ebbdcaaa3509e094f93b527ee7612296f91baa5ea6c
7
+ data.tar.gz: 2084f15a2256d92d34f00935911df87a153ffd25aa82a40152e957aabc74ca96ab2c188986f3481d39053098cff541dbd39543c5398bf2e8ddf9a4a190150d23
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 3.2.0", '< 3.3'
5
+ gem "rdoc", "~> 3.12"
6
+ gem "bundler", "~> 1.0"
7
+ gem "jeweler", "~> 2.0.1"
8
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 WeTransfer
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.
@@ -0,0 +1,24 @@
1
+ # pg_logger
2
+
3
+ Generates a Proc that can be fed to a "pg"-driven PostgreSQL adapter. The proc will convert the PostgreSQL
4
+ messages to the Ruby logger messages of a comparable warning level and send them to your logger.
5
+
6
+ For example, to use via Sequel:
7
+
8
+ db = Sequel.connect('postgres://...', notice_receiver: PgLogger.notice_proc_for_logger(MyApp.logger))
9
+
10
+ ## Contributing to pg_logger
11
+
12
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
13
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
14
+ * Fork the project.
15
+ * Start a feature/bugfix branch.
16
+ * Commit and push until you are happy with your contribution.
17
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
18
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
19
+
20
+ ## Copyright
21
+
22
+ Copyright (c) 2016 WeTransfer. See LICENSE.txt for
23
+ further details.
24
+
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.version = '1.0.0'
18
+ gem.name = "pg_logger"
19
+ gem.homepage = "https://github.com/WeTransfer/pg_logger"
20
+ gem.license = "MIT"
21
+ gem.description = %Q{Convert PostgreSQL notices to Ruby logger messages, in one proc}
22
+ gem.summary = %Q{Convert PostgreSQL notices to Ruby logger messages, in one proc}
23
+ gem.email = "me@julik.nl"
24
+ gem.authors = ["Julik Tarkhanov"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ desc "Code coverage detail"
36
+ task :simplecov do
37
+ ENV['COVERAGE'] = "true"
38
+ Rake::Task['spec'].execute
39
+ end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "pg_logger #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
@@ -0,0 +1,44 @@
1
+ module PgLogger
2
+ # Maps PG notice levels to Ruby Logger levels
3
+ POSTGRES_ERROR_MAPPING = {
4
+ 'DEBUG' => :debug,
5
+ 'LOG' => :info,
6
+ 'INFO' => :info,
7
+ 'NOTICE' => :warn,
8
+ 'WARNING' => :warn,
9
+ 'EXCEPTION' => :error,
10
+ }
11
+
12
+ DEFAULT_FORMAT = '[pg] %s'.freeze
13
+
14
+ # Creates a Proc from the given Logger object. The
15
+ # proc will accept PG::Result objects from notices
16
+ # and forward their contents into the given logger
17
+ #
18
+ # @param logger[Logger] the Ruby Logger object or a compatible
19
+ # @param format_str[String] the default format string
20
+ # @return [Proc] the proc that can be passed to the connection adapter
21
+ def self.notice_proc_for_logger(logger, format_str = DEFAULT_FORMAT)
22
+ ->(*pg_results) {
23
+ pg_results.each do |pg_result|
24
+ # message is a PG::Result, we need to extract the errors from it
25
+ # Only use the PG constants _within_ the proc so that the gem can still be loaded
26
+ # before the "pg" gem.
27
+ severity = pg_result.error_field(PG::Result::PG_DIAG_SEVERITY)
28
+
29
+ message_consts = [
30
+ PG::Result::PG_DIAG_MESSAGE_PRIMARY,
31
+ PG::Result::PG_DIAG_MESSAGE_DETAIL,
32
+ PG::Result::PG_DIAG_MESSAGE_HINT,
33
+ ]
34
+
35
+ message = message_consts.map do |err_const|
36
+ pg_result.error_field(err_const)
37
+ end.join(' ')
38
+
39
+ logger_method_name = POSTGRES_ERROR_MAPPING.fetch(severity)
40
+ logger.public_send(logger_method_name) { format_str % message }
41
+ end
42
+ }
43
+ end
44
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: pg_logger 1.0.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "pg_logger"
9
+ s.version = "1.0.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Julik Tarkhanov"]
14
+ s.date = "2016-02-26"
15
+ s.description = "Convert PostgreSQL notices to Ruby logger messages, in one proc"
16
+ s.email = "me@julik.nl"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ "Gemfile",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "lib/pg_logger.rb",
29
+ "pg_logger.gemspec",
30
+ "spec/pg_logger_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = "https://github.com/WeTransfer/pg_logger"
34
+ s.licenses = ["MIT"]
35
+ s.rubygems_version = "2.2.2"
36
+ s.summary = "Convert PostgreSQL notices to Ruby logger messages, in one proc"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 4
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
43
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
46
+ else
47
+ s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
48
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
49
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2.0"])
54
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
57
+ end
58
+ end
59
+
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module PG
4
+ module Result
5
+ PG_DIAG_SEVERITY = :PG_DIAG_SEVERITY
6
+ PG_DIAG_MESSAGE_PRIMARY = :PG_DIAG_MESSAGE_DETAIL
7
+ PG_DIAG_MESSAGE_DETAIL = :PG_DIAG_MESSAGE_DETAIL
8
+ PG_DIAG_MESSAGE_HINT = :PG_DIAG_MESSAGE_HINT
9
+ end
10
+ end
11
+
12
+ describe "PgLogger" do
13
+ it "converts the messages and sends them to the logger" do
14
+ out = StringIO.new
15
+ logger = double('Logger')
16
+
17
+ proc = PgLogger.notice_proc_for_logger(logger)
18
+ expect(proc).to respond_to(:call)
19
+
20
+ fake_notice = double('PG::Result')
21
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_SEVERITY) { 'NOTICE' }
22
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_MESSAGE_PRIMARY) { 'Something bad happened' }
23
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_MESSAGE_DETAIL) { 'in a detailed way' }
24
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_MESSAGE_HINT) { 'may be fixable' }
25
+
26
+ expect(logger).to receive(:warn) {|&blk|
27
+ expect(blk.call).to eq('[pg] Something bad happened in a detailed way may be fixable')
28
+ }
29
+
30
+ proc.call(fake_notice)
31
+ end
32
+
33
+ it "honors the format string" do
34
+ out = StringIO.new
35
+ logger = double('Logger')
36
+
37
+ proc = PgLogger.notice_proc_for_logger(logger, 'PostgreSQL said %s')
38
+ expect(proc).to respond_to(:call)
39
+
40
+ fake_notice = double('PG::Result')
41
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_SEVERITY) { 'NOTICE' }
42
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_MESSAGE_PRIMARY) { 'Something bad happened' }
43
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_MESSAGE_DETAIL) { 'in a detailed way' }
44
+ expect(fake_notice).to receive(:error_field).with(PG::Result::PG_DIAG_MESSAGE_HINT) { 'may be fixable' }
45
+
46
+ expect(logger).to receive(:warn) {|&blk|
47
+ expect(blk.call).to eq('PostgreSQL said Something bad happened in a detailed way may be fixable')
48
+ }
49
+
50
+ proc.call(fake_notice)
51
+ end
52
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'pg_logger'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Julik Tarkhanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "<"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 3.2.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rdoc
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.12'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.12'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: jeweler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 2.0.1
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 2.0.1
75
+ description: Convert PostgreSQL notices to Ruby logger messages, in one proc
76
+ email: me@julik.nl
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files:
80
+ - LICENSE.txt
81
+ - README.md
82
+ files:
83
+ - ".document"
84
+ - ".rspec"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - lib/pg_logger.rb
90
+ - pg_logger.gemspec
91
+ - spec/pg_logger_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/WeTransfer/pg_logger
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.2.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Convert PostgreSQL notices to Ruby logger messages, in one proc
117
+ test_files: []