log_method 1.0.0.pre.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bc351505c3fb62a69bc4f0f98a31335a212026dcec15f8644d7a0265bece1d16
4
+ data.tar.gz: 825b0e8dd2243d002e1c2c792e3e3f1b70b780b95d9719d236779e99327bc675
5
+ SHA512:
6
+ metadata.gz: 5ee91a5b361300c00b7fe13d2ff87a7722e54031ddd20c3c16365c940155f8870444485f51ab0406926bea062c3afb19ab2932f9f9b15159798e2687a814e216
7
+ data.tar.gz: 82e25bef7a840a873aae5e0e1afce8bd92d8f4dfa4831479dabe7ebd67f45919f5bf81ac28212ec54949a4522fef7a00a15210a70f33b46aa7cc97b5998a60ac
@@ -0,0 +1,35 @@
1
+ version: 2.1
2
+ orbs:
3
+ # See https://circleci.com/developer/orbs/orb/circleci/ruby
4
+ ruby: circleci/ruby@1.2.0
5
+ jobs: # keyword
6
+ test: # my name for the job
7
+ parameters: # keyword
8
+ ruby-version: # my parameter name
9
+ type: string # type is a keyword
10
+ docker: # keyword
11
+ - image: cimg/base:stable
12
+ steps: # keyword
13
+ - checkout # magic name
14
+ - ruby/install: # ruby/ is from the orb name, install is a command in that orb
15
+ version: << parameters.ruby-version >> # magic nonsense for param subst
16
+ - run:
17
+ command: "bin/setup"
18
+ - run:
19
+ name: "Create the test results directory because you can't just store_test_results with a file and if you do you do not get any sort of error because wtf is with this platform?"
20
+ command: mkdir -p /tmp/test-results
21
+ - run:
22
+ command: bin/ci /tmp/test-results/rspec_results.xml
23
+ - store_test_results: # store_test_results is magic from circle
24
+ path: /tmp/test-results # path is a param to store_test_results and it must be a directory not a file
25
+ - store_artifacts: # store_artifacts is magic from circle
26
+ path: /tmp/test-results # path is the param to store_artifacts
27
+ workflows: # keyword
28
+ all-rubies: # my name for the workflow
29
+ jobs: # keyword
30
+ - test: # my name for the job
31
+ matrix: # keyword
32
+ parameters: # keyword
33
+ # All rubies being maintained per this page:
34
+ # https://www.ruby-lang.org/en/downloads/branches/
35
+ ruby-version: [ "2.5", "2.6", "2.7", "3.0" ]
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at davetron5000 (at) gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,5 @@
1
+ # Contributing
2
+
3
+ * Code changes for style, cleanliness, eleganance, or other aesthetic stuff will not be accepted.
4
+ * All proposed changes must have a clear problem statement and be based on a real-world scenario. Imagined problems will not be solved.
5
+ * Tests are appreciated with all pull requests.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in with_clues.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/LICENSE.md ADDED
@@ -0,0 +1,33 @@
1
+ [log_method] Copyright (2021) (David Copeland)(“Licensor”)
2
+
3
+ Hippocratic License Version Number: 2.1.
4
+
5
+ Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
6
+
7
+ Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
8
+
9
+ * Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
10
+
11
+ * Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
12
+
13
+ * Compliance with Human Rights Principles and Human Rights Laws.
14
+
15
+ 1. Human Rights Principles.
16
+
17
+ (a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
18
+
19
+ (b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
20
+
21
+ Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
22
+
23
+ 2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
24
+
25
+ 3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
26
+
27
+ * Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
28
+
29
+ * Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
30
+
31
+ * Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
32
+
33
+ This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
data/README.md ADDED
@@ -0,0 +1,196 @@
1
+ # log\_method - a better way to log better stuff
2
+
3
+ [![<sustainable-rails>](https://circleci.com/gh/sustainable-rails/log_method.svg?style=shield)](https://app.circleci.com/pipelines/github/sustainable-rails/log_method)
4
+
5
+ Instead of `Rails.logger.info`, use `log «method_name»,«context object»,«message»`, and the following will be logged:
6
+
7
+ * Your log message
8
+ * The class where `log` was called
9
+ * The method name you pass in
10
+ * The class and id of the context object
11
+ * Any trace or request id you have configured
12
+ * Any current user id you have configured
13
+
14
+ This will result in more useful log messages that are easier to construct.
15
+
16
+ ## Install
17
+
18
+ 1. Add to your `Gemfile`:
19
+
20
+ ```ruby
21
+ # Gemfile
22
+ gem "log_method"
23
+ ```
24
+ 2. `bundle install`
25
+ 3. Optionally create a configuration in `config/initializers` (see below)
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ class SomeClass
31
+ include LogMethod::Log
32
+
33
+ def some_method(some_active_record)
34
+ log :some_method, some_active_record, "Beginning the operation"
35
+ end
36
+ end
37
+ ```
38
+
39
+ Assuming you have configured trace ids and current user id (see below for how), this is what your log message will look like (assuming the id of `some_active_record` is 42):
40
+
41
+ ```
42
+ [SomeClass#some_method](via LogMethod::Log) trace_id:7efa5401-08d8-44e3-b101-d5806563a3da current_user_id:42 [User:7889]: Beginnging the operation
43
+ ```
44
+
45
+ Let's break down each part and understand why it's there, which will help you understand why you should use this gem:
46
+
47
+ * `[SomeClass#some_method]` - this gives the class and method where the log statement originated. Super helpful when looking at log output and
48
+ trying to find what code generated that log message.
49
+ * `(via LogMethod::Log)` - This makes it clear that *this* gem produced this output. If you don't see this, it means something else is generating
50
+ log output, too. Very handy for understanding the source of your log statements.
51
+ * `trace_id:7efa5401-08d8-44e3-b101-d5806563a3da` - If you set a trace id at the start of a web request, or when you queue a background job, you
52
+ can then trace all log statements related to that request. SUPER handy for understanding what all happened in a given request you are looking at.
53
+ * `current_user_id:42` - System behavior often depends on who is logged in or who the "current actor" is. So you want this in your log.
54
+ * `[SomeActiveRecord:7889]:` - Almost always, code is operating on some object or operating in the context of some object. It's nice to know
55
+ exactly which one.
56
+ * `Beginnging the operation` - And, of course, your log message
57
+
58
+ ### `log` method, explained
59
+
60
+ There are two ways to call the `log` method:
61
+
62
+ * `log :some_method, "Some string-based message"` - this is the closest to `Rails.logger.info` and will include `:some_method`, the current class, the message you passed, and, if configured, trace id and current user id.
63
+ * `log :some_method, some_object, "Some string-based message"` - In this form, `some_object` is included in the log message. What is included
64
+ depends on what `some_object` is:
65
+ - If you have configured an `external_id` and this object responds to it, the object's class and the value of the external id are included in the log message
66
+ - If `some_object` is an Active Record, its class and the value of its `id` are included.
67
+ - Otherwise, the class and the value of `inspect` are included in the log message
68
+
69
+ The second form is the one you should prefer whenever you have an Active Record in context, because that's how you can automatically get the class
70
+ and ID into the log so you know what data was being operated on.
71
+
72
+ ### Cool, but I gotta put that `include` in every class?
73
+
74
+ I would recommend putting the `include` line in:
75
+
76
+ * `ApplicationController`
77
+ * `ApplicationJob`
78
+ * Whatever base class you use for your service layer (you should be using one).
79
+ * Any other "base" class for logic.
80
+
81
+ If you are putting business logic in your Active Records, you probably want to use the `include` line in `ApplicationRecord` as well. If you are
82
+ not putting business logic in Active Records, there's nothing to log, so I would not include this.
83
+
84
+ ## Why can't I just `Rails.logger.info`?
85
+
86
+ Almost every operation in your Rails app is operating on some piece of data, so it's extremely useful to know what that piece of data was. It's
87
+ also extremely useful to know where in the codebase the message originated. Lastly, the entire point of request ids/trace ids is to put them in
88
+ log messages *and* it's nice to know who was logged in doing the operation.
89
+
90
+ And it's really hard to remember to put all this into the string when calling `Rails.logger.info`.
91
+
92
+ ### But shouldn't we be using Observability or something?
93
+
94
+ Probably, but let's be real: not everyone has the time, bandwidth, money, or expertise to set up a true observability platform. But we *do* have
95
+ the time to deal with logs, because those are pretty easy. Why not make it easier?
96
+
97
+ ## Configuration
98
+
99
+ If you want to change some of the behavior of the `log` method, create `config/initializers/log_method.rb`. Here is an example configuration.
100
+ Following the example is an explanation of each option:
101
+
102
+ ```ruby
103
+ LogMethod.config do |c|
104
+ c.external_identifier_method = :external_id
105
+ c.current_actor_proc = ->() { PaperTrail.request.whodunnit }
106
+ c.current_actor_id_label = "user_id"
107
+ c.after_log_proc = ->(class_thats_logging_name, method_name, object_id, object_class_name, trace_id, current_actor_id) {
108
+ Bugsnag.leave_breadcrumb method_name.to_s[0..29], {
109
+ class: class_thats_logging_name,
110
+ object_id: object_id,
111
+ object_class: object_class_name,
112
+ trace_id: trace_id,
113
+ admin_user_id: current_actor_id
114
+ }, Bugsnag::Breadcrumbs::LOG_BREADCRUMB_TYPE
115
+ }
116
+ c.trace_id_proc = ->() { Thread.current.thread_variable_get("request_id") }
117
+ end
118
+ ```
119
+
120
+ ### Options
121
+
122
+ * `after_log_proc` This is a proc/lambda to be called after each log message has been sent to `Rails.logger.info`. In the example above, we're using this to send a breadcrumb to Bugsnag so that if there is an error with this request, we can see what log messages were logged for that request. It will be given these arguments:
123
+ - `class_thats_logging_name` - The class where `log` was called
124
+ - `method_name` - The method name passed to `log`
125
+ - `object_id` - The id of the object passed to `log`, if it had one
126
+ - `object_class_name` - The class name of the object passed to log, if one was passed
127
+ - `trace_id` - The trace id returned by `trace_id_proc`, or `nil` if that isn't configured.
128
+ - `current_actor_id` - The value returned by; `current_actor_proc`, or `nil` if that isn't configured.
129
+ * `current_actor_id_label` - If a current actor is logged, this is the label that will precede it in the logs
130
+ * `current_actor_proc` - Called to retrieve an identifier of the current actor executing the code, such as the current user.
131
+ * `external_identifier_method` - If you are using external ids on your objects, this is the name of that method. If an object is passed in that responds to this method, it will be used instead of `id` when creating the log message.
132
+ * `trace_id_proc` - returns the current request's request ID, trace ID, or cross-request ID. This is useful to tie various log messages together that were all part of a single request.
133
+
134
+ ### Helper Procs
135
+
136
+ This gem also includes some helper procs to connect `log` with some common gems and uses cases.
137
+
138
+ #### Bugsnag Breadcrumbs
139
+
140
+ In the example above, we use `after_log_proc` to send a breadcrumb to Bugsnag. This means that if we get an error in a request, Bugsnag will show
141
+ all of our log statements as breadcrumbs, which can help understand what data and state was involved in the error.
142
+
143
+ This proc is included in this gem and you can use it like so:
144
+
145
+ ```ruby
146
+ # config/initializers/log_method.rb
147
+ require "log_method/bugsnag_after_log"
148
+ LogMethod.config do |c|
149
+ c.after_log_proc = LogMethod::BugsnagAfterLog
150
+ end
151
+ ```
152
+
153
+ #### PaperTrail whodunnit
154
+
155
+ The [PaperTrail gem](https://github.com/paper-trail-gem/paper_trail) has support for storing an actor or user along with versions created on
156
+ changes to the database. It's common to unify your current user (e.g. from Devise) to the Paper Trail "whodunnit" so that you can always call
157
+ `PaperTrail.request.whodunnit` to get the current actor or user.
158
+
159
+ To use this for logging, this gem includes `LogMethod::PaperTrailCurrentActor` that you can set up like so:
160
+
161
+ ```ruby
162
+ # config/initializers/log_method.rb
163
+ require "log_method/paper_trail_current_actor"
164
+ LogMethod.config do |c|
165
+ c.current_actor_proc = LogMethod::PaperTrailCurrentActor
166
+ end
167
+ ```
168
+
169
+ ### External IDs explained
170
+
171
+ A useful pattern is to have your Active Records manage a unique external ID that is not used by the database for foreign key constraints or other
172
+ lookups. Suppose you call it `external_id`:
173
+
174
+ ```
175
+ > record = SomeRecord.first
176
+ > record.id
177
+ 42
178
+ > record.external_id
179
+ srec_2489089024u893huiefgjlhkdfg
180
+ ```
181
+
182
+ There are many reasons to do this, but if you *do* do this, you will probably want these values in the log and not the database primary keys.
183
+
184
+ ## Philosophy
185
+
186
+ I have tried to avoid excessive meta programming and stack navigation, resulting in something that is, I think, more predictable and easier to
187
+ follow. The code for `log` is relatively straightforward and verbose. I think that's what you want out of your logging and infrastructure code.
188
+
189
+ ## Contributing
190
+
191
+ While I'm interested in fixing bugs and making this library better, I would highly encourage you to run your proposed changes in your production
192
+ environment for a while to make sure they are useful to you and don't cause other issues.
193
+
194
+ If you do that, please open a PR with clear problem statement and I'd love to check it out!
195
+
196
+ Please do not open PRs for things like coding style, because I do not want to change the coding style :)
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "fileutils"
2
+ require "bundler/gem_tasks"
3
+ task :default => :spec
data/bin/ci ADDED
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+
3
+ set -e
4
+ echo "[bin/ci] Running tests"
5
+ if [ -z $1 ]; then
6
+ bin/rspec --format=doc
7
+ else
8
+ echo "[bin/ci] Generating JUnit XML output to $1"
9
+ bin/rspec --format RspecJunitFormatter --out $1 --format=doc
10
+ fi
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "with_clues"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle check || bundle install
@@ -0,0 +1,12 @@
1
+ class LogMethod::BugsnagAfterLog
2
+ def self.call(class_thats_logging_name, method_name, object_id, object_class_name, trace_id, current_actor_id)
3
+ Bugsnag.leave_breadcrumb method_name.to_s[0..29], {
4
+ class: class_thats_logging_name,
5
+ object_id: object_id,
6
+ object_class: object_class_name,
7
+ trace_id: trace_id,
8
+ admin_user_id: current_actor_id
9
+ }, Bugsnag::Breadcrumbs::LOG_BREADCRUMB_TYPE
10
+ end
11
+ end
12
+
@@ -0,0 +1,23 @@
1
+ class LogMethod::Config
2
+ attr_accessor :after_log_proc,
3
+ :current_actor_id_label,
4
+ :current_actor_proc,
5
+ :external_identifier_method,
6
+ :trace_id_proc
7
+
8
+ def initialize
9
+ reset!
10
+ end
11
+
12
+ def reset!
13
+ @after_log_proc = NO_OP
14
+ @current_actor_id_label = "current_actor_id"
15
+ @current_actor_proc = NO_OP
16
+ @external_identifier_method = nil
17
+ @trace_id_proc = NO_OP
18
+ end
19
+
20
+ private
21
+
22
+ NO_OP = ->(*) {}
23
+ end
@@ -0,0 +1,47 @@
1
+ module LogMethod::Log
2
+ def logging_external_identifier_method
3
+ @logging_external_identifier_method ||= LogMethod.config.external_identifier_method
4
+ end
5
+
6
+ def log(method, message_or_object, message=nil)
7
+
8
+ trace_id = LogMethod.config.trace_id_proc.()
9
+ current_actor_id = LogMethod.config.current_actor_proc.()
10
+
11
+ message = if message.nil?
12
+ message_or_object
13
+ else
14
+ object_id,object_class = if !logging_external_identifier_method.nil? &&
15
+ message_or_object.respond_to?(logging_external_identifier_method)
16
+
17
+ [message_or_object.send(logging_external_identifier_method), message_or_object.class]
18
+
19
+ elsif message_or_object.kind_of?(ActiveRecord::Base)
20
+
21
+ [message_or_object.id, message_or_object.class]
22
+
23
+ else
24
+
25
+ [message_or_object.inspect, message_or_object.class]
26
+
27
+ end
28
+ "[#{object_class}/#{object_id}]: #{message}"
29
+ end
30
+
31
+ Rails.logger.info("[#{self.class}##{method}](via LogMethod::Log)#{format_trace_id(trace_id)}#{format_current_actor_id(current_actor_id)}: #{message}")
32
+
33
+ LogMethod.config.after_log_proc.(self.class.name, method, object_id, object_class&.name, trace_id, current_actor_id)
34
+ end
35
+
36
+ private
37
+
38
+ def format_trace_id(trace_id)
39
+ return nil if trace_id.nil?
40
+ " trace_id:#{trace_id} "
41
+ end
42
+
43
+ def format_current_actor_id(current_actor_id)
44
+ return nil if current_actor_id.nil?
45
+ " #{LogMethod.config.current_actor_id_label}:#{current_actor_id} "
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ class LogMethod::PaperTrailCurrentActor
2
+ def self.call
3
+ PaperTrail.request.whodunnit
4
+ end
5
+ end
6
+
@@ -0,0 +1,3 @@
1
+ module LogMethod
2
+ VERSION="1.0.0-beta1"
3
+ end
data/lib/log_method.rb ADDED
@@ -0,0 +1,15 @@
1
+ module LogMethod
2
+ end
3
+
4
+ require_relative "log_method/config"
5
+ require_relative "log_method/log"
6
+
7
+ module LogMethod
8
+ def self.config(&block)
9
+ @config ||= LogMethod::Config.new
10
+ if !block.nil?
11
+ block.(@config)
12
+ end
13
+ @config
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ require_relative "lib/log_method/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "log_method"
5
+ spec.version = LogMethod::VERSION
6
+ spec.authors = ["Dave Copeland"]
7
+ spec.email = ["davec@naildrivin5.com"]
8
+ spec.summary = %q{A nice log method for your Rails app that provides a ton of useful context in each message!}
9
+ spec.homepage = "https://github.com/sustainable-rails/log_method"
10
+ spec.license = "Hippocratic"
11
+
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/sustainable-rails/log_method"
16
+ spec.metadata["changelog_uri"] = "https://github.com/sustainable-rails/log_method/releases"
17
+
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency("rspec")
26
+ spec.add_development_dependency("rspec_junit_formatter")
27
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log_method
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.beta1
5
+ platform: ruby
6
+ authors:
7
+ - Dave Copeland
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-10-24 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec_junit_formatter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - davec@naildrivin5.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".circleci/config.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".ruby-version"
52
+ - CODE_OF_CONDUCT.md
53
+ - CONTRIBUTING.md
54
+ - Gemfile
55
+ - LICENSE.md
56
+ - README.md
57
+ - Rakefile
58
+ - bin/ci
59
+ - bin/console
60
+ - bin/rspec
61
+ - bin/setup
62
+ - lib/log_method.rb
63
+ - lib/log_method/bugsnag_after_log.rb
64
+ - lib/log_method/config.rb
65
+ - lib/log_method/log.rb
66
+ - lib/log_method/paper_trail_current_actor.rb
67
+ - lib/log_method/version.rb
68
+ - log_method.gemspec
69
+ homepage: https://github.com/sustainable-rails/log_method
70
+ licenses:
71
+ - Hippocratic
72
+ metadata:
73
+ homepage_uri: https://github.com/sustainable-rails/log_method
74
+ source_code_uri: https://github.com/sustainable-rails/log_method
75
+ changelog_uri: https://github.com/sustainable-rails/log_method/releases
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.5.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ requirements: []
91
+ rubygems_version: 3.2.29
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A nice log method for your Rails app that provides a ton of useful context
95
+ in each message!
96
+ test_files: []