quickbase_logger 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1e71162078aadd4bff3657c696a5d96abc24f539
4
+ data.tar.gz: dc323756fda5cc2604587a0742b702eae3592fe2
5
+ SHA512:
6
+ metadata.gz: 648f1962ee519233a3bfa08d19bd9063fc5b692e626a0a26c57a11c34e975f1f81ca7d04222eac962cd40a3cb59c25f905e5451e4ca978266dca005545005f79
7
+ data.tar.gz: f8deae68d77bb5a83fb3a459e387840564681568008a23034444b19ff00bd87f97f2e0f7bc53d1544db3e8e5a8e46d4dd542889d077c029dca3e601c2b176827
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/config.yml
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ *.DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in quickbase_logger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Cullen Jett
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # QuickbaseLogger
2
+
3
+ "QuickbaseLogger offers a configurable way to use the Intuit QuickBase platform as a way to log Ruby script information."
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'quickbase_logger'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install quickbase_logger
20
+
21
+ ## What is it?
22
+ QuickbaseLogger offers a way to wrap your code in a begin/rescue block that will create QuickBase records after the block is finished executing. In addition, it will create a standard ruby Logger instance and write logs to a .log file. It offers a similar API as the standard ruby Logger (i.e. #info, #warn, #error).
23
+
24
+
25
+ ## Usage
26
+
27
+ ### Configuration
28
+ QuickbaseLogger relies on a certain architechture for the QuickBase application you write logs to -- as of version 0.0.1 you must have **two** tables, a parent Scripts table and a child table for the Script Logs (obviously you can name them whatever you want, it's only the parent-child relationship that's important). In the Script Logs child table there are a handful of fields that QuickbaseLogger will write to (i.e. you need these fields in your Script Logs child table):
29
+ - 'Start' -- A DateTime field that defines when the script started running.
30
+ - 'End' -- A DateTime field that defines when the script finished running.
31
+ - 'Logs' -- A Multi-line Text field that will capture any .info(message) calls and the backtrace for exceptions.
32
+ - 'Status' -- A Text field that will either be populated with 'Success' or 'Failure' based on if any exceptions are thrown during the script's execution.
33
+ - 'Related Script' -- The [Record ID#] of the parent script.
34
+
35
+ **Note that the names you give the fields in the .define_fields configuration block need to match the names above (the lowercase version, of course). See below for an example.**
36
+
37
+ QuickbaseLogger is built on top of the wonderful [QuickbaseRecord](https://github.com/cullenjett/quickbase_record) gem, and follows a similiar pattern for configuration.
38
+
39
+ ```ruby
40
+ # config/initializers/quickbase_logger.rb
41
+
42
+ QuickbaseLogger.configure do |config|
43
+ # same config as QuickbaseRecord
44
+ config.realm = "quickbase_app_realm_name"
45
+ config.username = "valid_username"
46
+ config.password = "valid_password"
47
+ config.token = "quickbase_app_token_if_applicable"
48
+
49
+ # new config options specific to QuickbaseLogger
50
+ # .logger_path -- the path to where text logs will write to.
51
+ # NOTE: this is required as of version 0.0.1 (it will default to /log, but your app may not follow that pattern and this could cause some headache if not defined here)
52
+ config.logger_path = "/log"
53
+
54
+ # .define_fields -- just a proxy for the method of the same name in QuickbaseRecord.
55
+ # This creates the map of field IDs to field names.
56
+ # NOTE: with the exception of :id, your field names (i.e. ':start', ':end', etc.) must match those below.
57
+ # (a :primary_key field is also required)
58
+ config.define_fields do |t|
59
+ t.dbid 'bkd86zn87'
60
+ t.number :id, 3, :primary_key, :read_only
61
+ t.date :start, 6
62
+ t.date :end, 7
63
+ t.string :log, 8
64
+ t.string :status, 9
65
+ t.number :related_script, 10
66
+ end
67
+ end
68
+ ```
69
+
70
+ ### Methods and API
71
+ Using QuickbaseLogger is as simple as instantiating an instance of QuickbaseLogger::Logger and wrapping any code you want logged to your Script Logs QuickBase table in the #log_to_quickbase method on that instance.
72
+
73
+ QuickbaseLogger::Logger.new accepts a :related_script argument that is the [Record ID#] of the parent Script record, and an optional :file_name argument for the name of the text log file. If no :file_name argument is given it will default to "quickbase_logger_default"
74
+
75
+ ```ruby
76
+ qb_logger = QuickbaseLogger::Logger.new(related_script: 123, file_name: 'my_awesome_log_file')
77
+ ```
78
+
79
+ The instance returned from QuickbaseLogger::Logger.new has a method named #log_to_quickbase that accepts a block of code you want to capture in you logs. While inside the scope of the #log_to_quickbase method, you have access to #info(message), #warn(message), and #error(message) methods on the logger instance that simply log the given message to the :log field on the Script Logs table in QuickBase as well as the text logger. Each of these methods will capture the time of the log automatically.
80
+
81
+ ```ruby
82
+ qb_logger = QuickbaseLogger::Logger.new(related_script: 123, file_name: 'my_awesome_log_file')
83
+ qb_logger.log_to_quickbase do
84
+ # any code within the scope of this block is fair game
85
+ end
86
+ ```
87
+
88
+ Note that the logs are automatically prepended with "START" and appended with "END", so you don't need to denote a start and end of the script's current run.
89
+
90
+
91
+ ```ruby
92
+
93
+ class SomeAwesomeScript
94
+ def self.perform
95
+ qb_logger = QuickbaseLogger::Logger.new(related_script: 123, file_name: 'some_awesome_script')
96
+
97
+ # Any exceptions thrown in the scope of this block
98
+ # will be rescued and sent to QuickBase.
99
+ qb_logger.log_to_quickbase do
100
+ # qb_logger.info("START -- #{Time.now}") -- don't do this, it's automatically done for you.
101
+ open_orders = Order.where(status: 'open')
102
+ qb_logger.info("found #{open_orders.length} open orders.")
103
+
104
+ open_orders.each do |order|
105
+ order.close
106
+ end
107
+ end
108
+ end
109
+ end
110
+ ```
111
+
112
+ Here's how the #log_to_quickbase method is structured (for your reference):
113
+
114
+ ```ruby
115
+ def log_to_quickbase
116
+ begin
117
+ yield
118
+ log_success_to_text_file
119
+ log_success_to_quickbase
120
+ rescue => err
121
+ log_failure_to_text_file(err)
122
+ log_failure_to_quickbase(err)
123
+ raise err
124
+ end
125
+ end
126
+ ```
127
+
128
+ ### The QuickBase Records
129
+ Here's an example record for a log for a script that ran without throwing any exceptions (the 'Error' in the log is from a call to #error(message), not a ruby exception):
130
+
131
+ ![successful_record_example](images/without_errors.png)
132
+
133
+ And one where an error occurred (this is from a manually raised StandardError, hence the 'YOU BLEW UP THE CODE' message. Typically this will be one of the many ruby execption messages you're familiar with):
134
+
135
+ ![unsuccessful_record_example](images/with_errors.png)
136
+
137
+ ## Contributing
138
+
139
+ 1. Fork it ( https://github.com/[my-github-username]/quickbase_logger/fork )
140
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
141
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
142
+ 4. Push to the branch (`git push origin my-new-feature`)
143
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
Binary file
Binary file
@@ -0,0 +1,27 @@
1
+ require "quickbase_record"
2
+ require "quickbase_logger/version"
3
+ require "quickbase_logger/logger"
4
+ require "quickbase_logger/configuration"
5
+
6
+ module QuickbaseLogger
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.configure
16
+ yield configuration
17
+
18
+ QuickbaseRecord.configure do |config|
19
+ config.realm = configuration.realm
20
+ config.username = configuration.username
21
+ config.password = configuration.password
22
+ config.token = configuration.token
23
+ end
24
+
25
+ Logger.define_fields(&configuration.fields_definition)
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ module QuickbaseLogger
2
+ class Configuration
3
+ attr_accessor :realm, :username, :password, :token, :logger_path, :fields_definition
4
+
5
+ def initialize
6
+ end
7
+
8
+ def define_fields(&block)
9
+ self.fields_definition = block
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,106 @@
1
+ module QuickbaseLogger
2
+ class Logger
3
+ include QuickbaseRecord::Model
4
+
5
+ attr_accessor :text_logger
6
+
7
+ def initialize(options={})
8
+ raise ArgumentError.new("QuickbaseLogger::Logger.new must receive a :related_script argument.") unless options[:related_script]
9
+
10
+ @log = []
11
+ @start = "#{formatted_date} #{formatted_time}"
12
+
13
+ file_name = options.fetch(:file_name, 'quickbase_logger_default')
14
+ @text_logger = ::Logger.new("#{formatted_logger_path}#{file_name}.log", "monthly") # standard ruby Logger instance
15
+ super
16
+ end
17
+
18
+ def log_to_quickbase
19
+ raise ArgumentError.new("#log_to_quickbase() must be given a block. Code run inside that block will be logged to the QuickBase application.") unless block_given?
20
+
21
+ begin
22
+ yield
23
+ log_success_to_text_file
24
+ log_success_to_quickbase
25
+ rescue => err
26
+ log_failure_to_text_file(err)
27
+ log_failure_to_quickbase(err)
28
+ raise err
29
+ end
30
+ end
31
+
32
+ def info(message)
33
+ log << "Info [#{formatted_time}]: #{message}"
34
+ end
35
+
36
+ def warn(message)
37
+ log << "Warn [#{formatted_time}]: #{message}"
38
+ end
39
+
40
+ def error(message)
41
+ log << "Error [#{formatted_time}]: #{message}"
42
+ end
43
+
44
+ private
45
+
46
+ def log_success_to_text_file
47
+ joined_logs = self.log.join("\n")
48
+ text_logger.info("LOGS:\n#{joined_logs}")
49
+ text_logger.info("END")
50
+ end
51
+
52
+ def log_failure_to_text_file(err)
53
+ joined_logs = self.log.join("\n")
54
+ text_logger.info("logs:\n#{joined_logs}")
55
+
56
+ text_logger.error("ERROR: #{err}")
57
+ text_logger.error("BACKTRACE:\n\t#{err.backtrace.slice(0, 10).join("\n\t")}")
58
+ end
59
+
60
+ def log_success_to_quickbase
61
+ self.status = "Success"
62
+ self.end = "#{formatted_date} #{formatted_time}"
63
+ self.log = self.log.join("\n")
64
+
65
+ begin
66
+ save
67
+ rescue StandardError => err
68
+ text_logger.error("-- COULD NOT WRITE SUCCESS TO QUICKBASE --")
69
+ text_logger.error(err)
70
+ text_logger.error("BACKTRACE:\n\t#{err.backtrace.slice(0, 10).join("\n\t")}")
71
+ raise err
72
+ end
73
+ end
74
+
75
+ def log_failure_to_quickbase(err)
76
+ self.end = "#{formatted_date} #{formatted_time}"
77
+ self.status = "Failure"
78
+
79
+ self.log = self.log.join("\n")
80
+ self.log << "\nERROR: #{err} \n"
81
+ self.log << "BACKTRACE:\n\t#{err.backtrace.slice(0, 10).join("\n\t")}"
82
+
83
+ begin
84
+ save
85
+ rescue StandardError => err
86
+ text_logger.error("-- COULD NOT WRITE FAILURE TO QUICKBASE --")
87
+ text_logger.error(err)
88
+ text_logger.error("BACKTRACE:\n\t#{err.backtrace.slice(0, 10).join("\n\t")}")
89
+ raise err
90
+ end
91
+ end
92
+
93
+ def formatted_date
94
+ DateTime.now.strftime("%m/%d/%Y").strip
95
+ end
96
+
97
+ def formatted_time
98
+ DateTime.now.strftime("%l:%M:%S %p").strip
99
+ end
100
+
101
+ def formatted_logger_path
102
+ path = QuickbaseLogger.configuration.logger_path || "/log"
103
+ path[-1] =~ /\// ? path : "#{path}/"
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module QuickbaseLogger
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quickbase_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quickbase_logger"
8
+ spec.version = QuickbaseLogger::VERSION
9
+ spec.authors = ["Cullen Jett"]
10
+ spec.email = ["cullenjett@gmail.com"]
11
+ spec.summary = "Use Quickbase tables as a logging platform"
12
+ spec.description = "QuickbaseLogger offers a configurable way to use the Intuit QuickBase platform as a way to log Ruby script information."
13
+ spec.homepage = "https://github.com/cullenjett/quickbase_logger"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+
25
+ spec.add_runtime_dependency "quickbase_record", ">= 0.4.5"
26
+ end
File without changes
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'yaml'
3
+
4
+ creds = YAML.load_file("spec/config.yml")
5
+
6
+ RSpec.describe QuickbaseLogger do
7
+ describe '.configure' do
8
+ it "passes it's block to QuickbaseRecord.configure()" do
9
+ expect(QuickbaseRecord).to receive(:configure)
10
+ QuickbaseLogger.configure do |config|
11
+ config.realm = "ais"
12
+ config.username = creds["username"]
13
+ config.password = creds["password"]
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+
20
+ RSpec.describe QuickbaseLogger::Logger do
21
+ describe '.initialize' do
22
+ it "defines a parent script that all log records will be related to" do
23
+ logger = QuickbaseLogger::Logger.new(related_script: 123)
24
+ expect(logger.related_script).to eq(123)
25
+ end
26
+ end
27
+
28
+ describe '#log_to_quickbase' do
29
+ it "calls #save to create a new record in QuickBase" do
30
+ qb_logger = QuickbaseLogger::Logger.new(related_script: 1)
31
+
32
+ expect(qb_logger).to receive(:save)
33
+
34
+ qb_logger.log_to_quickbase do
35
+ qb_logger.info('Hello, world!')
36
+ qb_logger.warn('Danger ahead...')
37
+ qb_logger.error('OH NO!!!')
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ require 'quickbase_record'
2
+ require 'quickbase_logger'
3
+ require 'yaml'
4
+
5
+ creds = YAML.load_file("spec/config.yml")
6
+
7
+ QuickbaseLogger.configure do |config|
8
+ config.realm = "ais"
9
+ config.username = creds["username"]
10
+ config.password = creds["password"]
11
+
12
+ config.logger_path = "./spec/log"
13
+ config.define_fields do |t|
14
+ t.dbid 'bkd86zn87'
15
+ t.number :id, 3, :primary_key, :read_only
16
+ t.date :start, 6
17
+ t.date :end, 7
18
+ t.string :log, 8
19
+ t.string :status, 9
20
+ t.number :related_script, 10
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quickbase_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cullen Jett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: quickbase_record
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.4.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.4.5
69
+ description: QuickbaseLogger offers a configurable way to use the Intuit QuickBase
70
+ platform as a way to log Ruby script information.
71
+ email:
72
+ - cullenjett@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - images/with_errors.png
84
+ - images/without_errors.png
85
+ - lib/quickbase_logger.rb
86
+ - lib/quickbase_logger/configuration.rb
87
+ - lib/quickbase_logger/logger.rb
88
+ - lib/quickbase_logger/version.rb
89
+ - quickbase_logger.gemspec
90
+ - spec/log/quickbase_logger_default.log
91
+ - spec/quickbase_logger_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/cullenjett/quickbase_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.4.5
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Use Quickbase tables as a logging platform
117
+ test_files:
118
+ - spec/log/quickbase_logger_default.log
119
+ - spec/quickbase_logger_spec.rb
120
+ - spec/spec_helper.rb