object_context_logger 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +23 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +93 -0
- data/Rakefile +12 -0
- data/lib/generators/object_context_logger_initializer_generator.rb +12 -0
- data/lib/object_context_logger/version.rb +3 -0
- data/lib/object_context_logger.rb +64 -0
- data/object_context_logger.gemspec +33 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9c60c8e6f07f7663d2ca51237cd090911b8e336fccfa8e76d6eda5e2a05c2785
|
4
|
+
data.tar.gz: 5652ab2439a59e1cc1d8aaed2d5a7f732af7d6821666ca759b3e32535974264e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75aeb98838e3ac10b8b6eb3552190b8b7224018526411b899adf70153fa387a8e214c55fc1e64e4c9b89c0df1b916019de68784f1635a30ff20d628a5633b003
|
7
|
+
data.tar.gz: 542fc2c2b45c117f80acf68d0f6df8293844d8fe26d9714c11d12d7aeed26d218cff033131a6b29ce8b3b011adeedbed627a157e0d3adca17d8f8f56d46ae73f
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
object_context_logger
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.2.2
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
object_context_logger (0.1.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
awesome_print (1.9.2)
|
10
|
+
minitest (5.19.0)
|
11
|
+
rake (13.0.6)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
x86_64-darwin-20
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
awesome_print
|
18
|
+
minitest (~> 5.0)
|
19
|
+
object_context_logger!
|
20
|
+
rake (~> 13.0)
|
21
|
+
|
22
|
+
BUNDLED WITH
|
23
|
+
2.4.10
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Daniel P. Zepeda
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Daniel P Zepeda
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# ObjectContextLogger
|
2
|
+
|
3
|
+
ObjectContextLogger is as simple utility that automatically prepends a given log message with the context that it was
|
4
|
+
called from.
|
5
|
+
|
6
|
+
This utility was born out of the frustration of reading logs to debug a problem, finding a useful message,
|
7
|
+
then wishing I knew which instance of a class/object or other context it was called from.
|
8
|
+
Being lazy, instead of trying to remember to always include that
|
9
|
+
context in the log message, I wrote this little bit of code.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
### Bundler
|
14
|
+
|
15
|
+
gem 'error_extractor', '~> 0.1.0'
|
16
|
+
|
17
|
+
### Gem install
|
18
|
+
|
19
|
+
$ gem install object_context_logger
|
20
|
+
|
21
|
+
## Configuration
|
22
|
+
|
23
|
+
### Rails
|
24
|
+
|
25
|
+
If you'll be using this in a Rails app, then you can generate the configuration initializer with this:
|
26
|
+
|
27
|
+
$ rails generate object_context_logger_initializer
|
28
|
+
|
29
|
+
### Manually
|
30
|
+
|
31
|
+
If you'll be using this outside of a Rails app, you'll need to proved the configuration manually:
|
32
|
+
|
33
|
+
```
|
34
|
+
ObjectContextLogger.configure do |config|
|
35
|
+
# config.logger = Rails.logger # must respond to logger methods like :info, :debug
|
36
|
+
# config.default_log_method = :info
|
37
|
+
# config.default_object_identifier_method = :to_gid
|
38
|
+
# config.log_to_stdout = false
|
39
|
+
end
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
### Configuration Items
|
44
|
+
|
45
|
+
* `logger` - The only required setting, provide a logger object that must at least respond to `:info`
|
46
|
+
* `default_log_method` - This is the method sent to the logger object to register a log.
|
47
|
+
* `default_object_identifier_method` - By default, ObjectContextLogger uses [GlobalID](https://github.com/rails/globalid),
|
48
|
+
if you are outside of a Rails project or dealing with objects that do not respond to `to_gid` by default, you'll
|
49
|
+
want to use a different default identifier method, like `to_s`, appropriately defined on the objects you are logging
|
50
|
+
from.
|
51
|
+
* `log_to_stdout` - Sometimes useful when debugging scripts.
|
52
|
+
|
53
|
+
## Usage
|
54
|
+
|
55
|
+
The whole idea of this utility is to make logging with context simple:
|
56
|
+
|
57
|
+
### In the context of an instance of User (id 1)
|
58
|
+
|
59
|
+
ctx_log "Your very informative log message"
|
60
|
+
#=> gid://gem-host/User/1: Your very informative message
|
61
|
+
|
62
|
+
If you want to use a particular logger facility, you can specify it:
|
63
|
+
|
64
|
+
ctx_log "Your very informative log message", :debug
|
65
|
+
#=> gid://gem-host/User/1: Your very informative message
|
66
|
+
|
67
|
+
If the object/class you are logging from requires some special identifier, you can specify it:
|
68
|
+
|
69
|
+
ctx_log "Your very informative log message", object_identifier: "some string or method call"
|
70
|
+
#=> some string or method call: Your very informative log message
|
71
|
+
|
72
|
+
### In the context of a class User
|
73
|
+
|
74
|
+
ctx_log "Your very informative log message"
|
75
|
+
#=> User: Your very informative message
|
76
|
+
|
77
|
+
Finally, there is a helper method to use if you want to add context to exceptions:
|
78
|
+
|
79
|
+
raise "#{ctx_log_prefix}: Your very informative exception message"
|
80
|
+
#=> User: Your very informative exception message
|
81
|
+
|
82
|
+
## Development
|
83
|
+
|
84
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
85
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/duskhacker/object-context-logger.
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
class ObjectContextLoggerInitializerGenerator < Rails::Generators::Base
|
2
|
+
def create_initializer_file
|
3
|
+
create_file "config/initializers/object_context_logger.rb", <<~RUBY
|
4
|
+
ObjectContextLogger.configure do |config|
|
5
|
+
# config.logger = Rails.logger # must respond to logger methods like :info, :debug
|
6
|
+
# config.default_log_method = :info
|
7
|
+
# config.default_object_identifier_method = :to_gid
|
8
|
+
# config.log_to_stdout = false
|
9
|
+
end
|
10
|
+
RUBY
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require_relative "object_context_logger/version"
|
2
|
+
|
3
|
+
module ObjectContextLogger
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
def ctx_log(message, method = :info, object_identifier: nil)
|
11
|
+
self.class.ctx_log(
|
12
|
+
message,
|
13
|
+
method,
|
14
|
+
object_identifier: object_identifier || obj_id
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def ctx_log_prefix(object_identifier = nil)
|
19
|
+
self.class.ctx_log_prefix(object_identifier || obj_id)
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def ctx_log(message, method = nil, object_identifier: nil)
|
24
|
+
config = ObjectContextLogger.configuration
|
25
|
+
m = "#{ctx_log_prefix(object_identifier)}: #{message}"
|
26
|
+
puts m if config.log_to_stdout
|
27
|
+
if config.logger.nil? || config.logger == ""
|
28
|
+
raise "ObjectContextLogger: logger is not configured, use ObjectContextLogger.configure to set it"
|
29
|
+
end
|
30
|
+
config.logger.send(method || config.default_log_method, m)
|
31
|
+
end
|
32
|
+
|
33
|
+
def ctx_log_prefix(object_identifier = nil)
|
34
|
+
return object_identifier unless object_identifier.nil? || object_identifier.to_s == ""
|
35
|
+
"#{self.class == Class ? self : self.class}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class << self
|
40
|
+
attr_accessor :configuration
|
41
|
+
|
42
|
+
def configure
|
43
|
+
self.configuration ||= Configuration.new
|
44
|
+
yield(configuration)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Configuration
|
49
|
+
attr_accessor :logger, :log_to_stdout, :default_object_identifier_method, :default_log_method
|
50
|
+
|
51
|
+
def initialize
|
52
|
+
@default_object_identifier_method = :to_gid
|
53
|
+
@default_log_method = :info
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def obj_id
|
60
|
+
send(ObjectContextLogger.configuration.default_object_identifier_method.to_s) rescue nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
include ObjectContextLogger
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/object_context_logger/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "object_context_logger"
|
7
|
+
spec.version = ObjectContextLogger::VERSION
|
8
|
+
spec.authors = ["Daniel P Zepeda"]
|
9
|
+
spec.email = ["daniel@zepeda.ws"]
|
10
|
+
|
11
|
+
spec.summary = "Logging helper that prefixes the object context it was called from into the log message"
|
12
|
+
spec.description = "Logging helper that prefixes the object context it was called from into the log message"
|
13
|
+
spec.homepage = "https://github.com/duskhacker/object-context-logger"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(__dir__) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: object_context_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel P Zepeda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Logging helper that prefixes the object context it was called from into
|
14
|
+
the log message
|
15
|
+
email:
|
16
|
+
- daniel@zepeda.ws
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".ruby-gemset"
|
22
|
+
- ".ruby-version"
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- lib/generators/object_context_logger_initializer_generator.rb
|
30
|
+
- lib/object_context_logger.rb
|
31
|
+
- lib/object_context_logger/version.rb
|
32
|
+
- object_context_logger.gemspec
|
33
|
+
homepage: https://github.com/duskhacker/object-context-logger
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
homepage_uri: https://github.com/duskhacker/object-context-logger
|
38
|
+
source_code_uri: https://github.com/duskhacker/object-context-logger
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.6.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.0.3.1
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Logging helper that prefixes the object context it was called from into the
|
58
|
+
log message
|
59
|
+
test_files: []
|