custom_log_space 0.1.0

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: 98b901d882ede4d8684e25e13741a5e65eb6f61cbbae5b4e8bb597c15781d23e
4
+ data.tar.gz: bf6f8ccf000f73f92d4b94853847d7f04cfb575b6460bbb791702a1f4ed8514d
5
+ SHA512:
6
+ metadata.gz: dc4463dfe438bf6d25b200177211506c5e34c3011199caf737013083f121ceb0004d633901d3bf859a89037137b99e9bef5907e1ccdc35ee4e096a8b6e9da096
7
+ data.tar.gz: 173effc0824a52f8cf383d91c15c027157b03c2c0ffbf46b99368cb0b9a841a05eba2f37131083f61e880bc95ffb5c1fc4b9fa9183f46f1358c08d910ff3d852
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 150
14
+
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - 'spec/**/*'
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-09-12
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tatsunishitatsu@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 nishikawa1031
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,47 @@
1
+ # CustomLogSpace
2
+
3
+ The CustomLogSpace gem allows Rails developers to direct Rails logs to files, organized by each controller and action. This organization simplifies debugging and analysis.
4
+
5
+ ## Installation
6
+
7
+ To begin, add the gem to your application's Gemfile:
8
+
9
+ ```ruby
10
+ group :development do
11
+ gem 'custom_log_space'
12
+ end
13
+ ```
14
+
15
+ Next, run:
16
+
17
+ ```bash
18
+ $ bundle install
19
+ ```
20
+
21
+ Alternatively, you can install it directly using:
22
+
23
+ ```bash
24
+ $ gem install custom_log_space
25
+ ```
26
+
27
+ ## Usage
28
+ Logs are saved in the `log/custom_log_space/[date]/[time]/[controller_name]/` directory. The filenames follow the pattern: "[action_name].log".
29
+
30
+ <img width="492" alt="スクリーンショット 2023-09-12 8 37 43" src="https://github.com/nishikawa1031/custom_log_space/assets/53680568/95cf44c8-e256-44d0-b0cb-9d6367601985">
31
+
32
+
33
+ ## Supported environments
34
+ - Rails 7
35
+ - Ruby 3
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/custom_log_space. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/custom_log_space/blob/main/CODE_OF_CONDUCT.md).
40
+
41
+ ## License
42
+
43
+ This gem is open-sourced under the [MIT License](https://opensource.org/licenses/MIT) terms.
44
+
45
+ ## Code of Conduct
46
+
47
+ All participants in the CustomLogSpace project, whether they're interacting with codebases, issue trackers, chat rooms, or mailing lists, are expected to follow the [code of conduct](https://github.com/nishikawa1031/custom_logger/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/custom_log_space/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "custom_log_space"
7
+ spec.version = CustomLogSpace::VERSION
8
+ spec.authors = ["nishikawa1031"]
9
+ spec.email = ["tatsunishitatsu@gmail.com"]
10
+
11
+ spec.summary = "A Rails logger extension that organizes logs by controller and action into dedicated folders."
12
+ spec.description = "CustomLogSpace refines Rails logs by categorizing them by controller and action. It provides a clearer view."
13
+ spec.homepage = "https://github.com/nishikawa1031/custom_log_space.git"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/nishikawa1031/custom_log_space.git"
21
+ spec.metadata["changelog_uri"] = "https://github.com/nishikawa1031/custom_log_space/blob/main/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) ||
28
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ # Uncomment to register a new dependency of your gem
36
+ # spec.add_dependency "example-gem", "~> 1.0"
37
+ spec.add_runtime_dependency "activesupport", ">= 6.0", "< 8.0"
38
+
39
+ # For more information and examples about making a new gem, check out our
40
+ # guide at: https://bundler.io/guides/creating_gem.html
41
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CustomLogSpace
4
+ # CustomLogSpace::Subscriber is a class for handling custom logging in Rails applications.
5
+ # It provides methods for processing different types of log events and organizing log messages.
6
+ class BaseSubscriber < ActiveSupport::LogSubscriber
7
+ def start_processing(event)
8
+ setup_thread_variables(event.payload)
9
+ end
10
+
11
+ def process_action(event)
12
+ payload = event.payload
13
+ status = payload[:status]
14
+ duration = event.duration.round(2)
15
+ view_runtime = payload[:view_runtime]&.round(2)
16
+ db_runtime = payload[:db_runtime]&.round(2)
17
+ allocations = event.allocations
18
+
19
+ message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{duration}ms " \
20
+ "(Views: #{view_runtime}ms | ActiveRecord: #{db_runtime}ms | Allocations: #{allocations})"
21
+
22
+ log_message(message)
23
+ clear_thread_variables
24
+ end
25
+
26
+ private
27
+
28
+ def setup_thread_variables(payload)
29
+ Thread.current[:current_controller] = payload[:controller]
30
+ Thread.current[:current_action] = payload[:action]
31
+ Thread.current[:path] = payload[:path]
32
+ Thread.current[:params] = payload[:params].except(:controller, :action)
33
+ Thread.current[:header_written] = false
34
+ end
35
+
36
+ def clear_thread_variables
37
+ Thread.current[:current_controller] = nil
38
+ Thread.current[:current_action] = nil
39
+ Thread.current[:path] = nil
40
+ Thread.current[:params] = nil
41
+ Thread.current[:header_written] = nil
42
+ end
43
+
44
+ def log_message(message)
45
+ current_controller = Thread.current[:current_controller]
46
+ current_action = Thread.current[:current_action]
47
+
48
+ return unless current_controller && current_action
49
+
50
+ FileUtils.mkdir_p(controller_log_directory) unless Dir.exist?(controller_log_directory)
51
+ write_to_custom_log(message)
52
+ end
53
+
54
+ def custom_log_directory
55
+ today = Time.now.strftime("%Y%m%d")
56
+ time = Time.now.strftime("%H%M")
57
+ File.join(Rails.root, "log", "custom_log_space", today, time)
58
+ end
59
+
60
+ def controller_log_directory
61
+ controller_name = Thread.current[:current_controller].underscore
62
+ File.join(custom_log_directory, controller_name)
63
+ end
64
+
65
+ def custom_log_file_path
66
+ action_name = Thread.current[:current_action]
67
+ log_file_name = "#{action_name}.log"
68
+ File.join(controller_log_directory, log_file_name)
69
+ end
70
+
71
+ def write_to_custom_log(message)
72
+ custom_log_path = custom_log_file_path
73
+
74
+ File.open(custom_log_path, "a") do |file|
75
+ write_header_information(file)
76
+ file.puts(message)
77
+ end
78
+ rescue SystemCallError, IOError => e
79
+ handle_file_error(e)
80
+ end
81
+
82
+ def handle_file_error(error)
83
+ error_prefix = error.is_a?(SystemCallError) ? "Error" : "IO Error"
84
+ puts "#{error_prefix}: #{error.message}"
85
+ end
86
+
87
+ def write_header_information(file)
88
+ return if Thread.current[:header_written]
89
+
90
+ current_controller = Thread.current[:current_controller]
91
+ current_action = Thread.current[:current_action]
92
+
93
+ file.puts("") # Add a blank line for better readability.
94
+ write_request_info(file)
95
+ write_processing_info(file, current_controller, current_action)
96
+ write_parameters_info(file)
97
+ Thread.current[:header_written] = true
98
+ end
99
+
100
+ def write_request_info(file)
101
+ formatted_time = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
102
+ file.puts "Started GET \"#{Thread.current[:path]}\" for ::1 at #{formatted_time}"
103
+ end
104
+
105
+ def write_processing_info(file, current_controller, current_action)
106
+ file.puts "Processing by #{current_controller}##{current_action} as HTML"
107
+ end
108
+
109
+ def write_parameters_info(file)
110
+ params = Thread.current[:params] || {}
111
+ file.puts "Parameters: #{params.inspect}" unless params.empty?
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SQLSubscriber logs SQL related events for CustomLogSpace.
4
+ # It extends BaseSubscriber to make use of its logging capabilities.
5
+ class SQLSubscriber < CustomLogSpace::BaseSubscriber
6
+ def sql(event)
7
+ payload = event.payload
8
+ name = payload[:name]
9
+ sql = payload[:sql]
10
+ duration = event.duration.round(1)
11
+ message = "#{name} (#{duration}ms) #{sql}"
12
+ log_message(message)
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CustomLogSpace
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # ViewSubscriber logs view rendering events for CustomLogSpace.
4
+ # It tracks events like template rendering, partial rendering, and collection rendering.
5
+ class ViewSubscriber < CustomLogSpace::BaseSubscriber
6
+ def render_template(event)
7
+ identifier = event.payload[:identifier]
8
+ duration = event.duration.round(2)
9
+ allocations = event.allocations
10
+ message = "Rendered #{identifier} (Duration: #{duration}ms | Allocations: #{allocations})"
11
+
12
+ log_message(message)
13
+ end
14
+
15
+ def render_partial(event)
16
+ identifier = event.payload[:identifier]
17
+ duration = event.duration.round(2)
18
+ allocations = event.allocations
19
+ message = "Rendered partial #{identifier} (Duration: #{duration}ms | Allocations: #{allocations})"
20
+
21
+ log_message(message)
22
+ end
23
+
24
+ def render_collection(event)
25
+ identifier = event.payload[:identifier]
26
+ count = event.payload[:count]
27
+ duration = event.duration.round(2)
28
+ allocations = event.allocations
29
+ message = "Rendered collection #{identifier} (#{count} items) (Duration: #{duration}ms | Allocations: #{allocations})"
30
+
31
+ log_message(message)
32
+ end
33
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "custom_log_space/base_subscriber"
4
+ require "custom_log_space/sql_subscriber"
5
+ require "custom_log_space/view_subscriber"
6
+
7
+ CustomLogSpace::BaseSubscriber.attach_to :action_controller
8
+ SQLSubscriber.attach_to :active_record
9
+ ViewSubscriber.attach_to :action_view
@@ -0,0 +1,4 @@
1
+ module CustomLogSpace
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,4 @@
1
+ module CustomLogSpace
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: custom_log_space
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nishikawa1031
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '6.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.0'
33
+ description: CustomLogSpace refines Rails logs by categorizing them by controller
34
+ and action. It provides a clearer view.
35
+ email:
36
+ - tatsunishitatsu@gmail.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - ".rspec"
42
+ - ".rubocop.yml"
43
+ - CHANGELOG.md
44
+ - CODE_OF_CONDUCT.md
45
+ - LICENSE.txt
46
+ - README.md
47
+ - Rakefile
48
+ - custom_log_space.gemspec
49
+ - lib/custom_log_space.rb
50
+ - lib/custom_log_space/base_subscriber.rb
51
+ - lib/custom_log_space/sql_subscriber.rb
52
+ - lib/custom_log_space/version.rb
53
+ - lib/custom_log_space/view_subscriber.rb
54
+ - sig/change_rails_log_path.rbs
55
+ - sig/custom_log_space.rbs
56
+ homepage: https://github.com/nishikawa1031/custom_log_space.git
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ homepage_uri: https://github.com/nishikawa1031/custom_log_space.git
62
+ source_code_uri: https://github.com/nishikawa1031/custom_log_space.git
63
+ changelog_uri: https://github.com/nishikawa1031/custom_log_space/blob/main/CHANGELOG.md
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.6.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.4.19
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A Rails logger extension that organizes logs by controller and action into
83
+ dedicated folders.
84
+ test_files: []