custom_log_space 0.1.2 → 0.1.4
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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +3 -4
- data/custom_log_space.gemspec +1 -1
- data/lib/custom_log_space/base_helper/log_writer.rb +79 -0
- data/lib/custom_log_space/{base_subscriber.rb → subscribers/base_subscriber.rb} +3 -3
- data/lib/custom_log_space/version.rb +1 -1
- data/lib/custom_log_space.rb +3 -3
- metadata +9 -9
- data/lib/custom_log_space/log_writer.rb +0 -107
- /data/lib/custom_log_space/{log_formatter.rb → base_helper/log_formatter.rb} +0 -0
- /data/lib/custom_log_space/{thread_manager.rb → base_helper/thread_manager.rb} +0 -0
- /data/lib/custom_log_space/{sql_subscriber.rb → subscribers/sql_subscriber.rb} +0 -0
- /data/lib/custom_log_space/{view_subscriber.rb → subscribers/view_subscriber.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58dddf2fc7026b74b4f4afea69035a4a3c94f6d0b1e356a033c8c7368919d530
|
4
|
+
data.tar.gz: '0826fdfdb8537d4935182fb05bb7155e5de1162a357fcdf333a89ede1c78900b'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6a1dcbf9c647fa2402aa11564af5875b0c6718ebf6ee59c8316cddd1a0072e27539ff9e8ea91a5602aced4625d7b098b18778b20b19a8fc2f9e22d977d63a16
|
7
|
+
data.tar.gz: 938eb0064c5156508e350ec591a5bca3be50bb3955ec087753dd78fb8f8e90842dd290301492099272616c80800b00c6c2d665447b4a8b5ade3612857079db94
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.1.4] - 2023-09-15
|
4
|
+
### Changed
|
5
|
+
Simplify the gem description
|
6
|
+
|
7
|
+
## [0.1.3] - 2023-09-15
|
8
|
+
### Changed
|
9
|
+
The retention period for logs within the date directory has been changed from 3 days to 2 days.
|
10
|
+
|
3
11
|
## [0.1.2] - 2023-09-15
|
4
12
|
### Changed
|
5
13
|
Modified the file path structure for logs. New structure: log/custom_log_space/#{controller_name}/#{action_name}/#{date}/#{time}.log.
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# CustomLogSpace
|
2
2
|
|
3
|
-
The CustomLogSpace gem
|
4
|
-
|
5
|
-
Thanks to this gem, developers are freed from the hassle of constantly starting the rails server to check logs every time an action in a controller is executed.
|
3
|
+
The CustomLogSpace gem organizes Rails logs by controller and action.
|
4
|
+
With it, developers no longer need to start the rails server repeatedly just to check logs.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -53,7 +52,7 @@ user log % tree
|
|
53
52
|
|
54
53
|
## Retention Policy
|
55
54
|
|
56
|
-
To prevent excessive disk usage, logs within the `date` directory are retained for only
|
55
|
+
To prevent excessive disk usage, logs within the `date` directory are retained for only 2 days. Any logs older than this retention period will be automatically deleted, starting with the oldest. Ensure that you archive or backup logs if you need them for longer periods.
|
57
56
|
|
58
57
|
## Ignoring Logs in Git
|
59
58
|
If needed, add `/log/custom_log_space/*` to your `.gitignore` to ensure the logs aren't committed to your repository.
|
data/custom_log_space.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.email = ["tatsunishitatsu@gmail.com"]
|
10
10
|
|
11
11
|
spec.summary = "A Rails logger extension that organizes logs by controller and action into dedicated folders."
|
12
|
-
spec.description = "
|
12
|
+
spec.description = "This gem organizes Rails logs by controller and action, eliminating the need to start the server for log checks."
|
13
13
|
spec.homepage = "https://github.com/nishikawa1031/custom_log_space.git"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 2.6.0"
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CustomLogSpace
|
4
|
+
# The LogWriter module provides methods for writing log messages to custom log files.
|
5
|
+
# It allows the creation of log directories, handling file errors, and appending messages to log files.
|
6
|
+
module LogWriter
|
7
|
+
private
|
8
|
+
|
9
|
+
def current_controller
|
10
|
+
Thread.current[:current_controller]
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_action
|
14
|
+
Thread.current[:current_action]
|
15
|
+
end
|
16
|
+
|
17
|
+
def log_message(message)
|
18
|
+
return unless current_controller && current_action
|
19
|
+
|
20
|
+
write_to_custom_log(message) do |file|
|
21
|
+
write_header_information(file)
|
22
|
+
end
|
23
|
+
|
24
|
+
cleanup_old_directories
|
25
|
+
end
|
26
|
+
|
27
|
+
def cleanup_old_directories
|
28
|
+
return unless Dir.exist?(base_directory_path)
|
29
|
+
|
30
|
+
# If there are more than 2 date-directories, remove the oldest ones
|
31
|
+
remove_oldest_directory while all_directories.size > 2
|
32
|
+
end
|
33
|
+
|
34
|
+
def all_directories
|
35
|
+
@all_directories ||= Dir.entries(base_directory_path).select do |entry|
|
36
|
+
File.directory?(File.join(base_directory_path, entry)) && entry !~ /^\./
|
37
|
+
end.sort
|
38
|
+
end
|
39
|
+
|
40
|
+
def remove_oldest_directory
|
41
|
+
directory_to_remove = all_directories.shift
|
42
|
+
path_to_remove = File.join(base_directory_path, directory_to_remove)
|
43
|
+
FileUtils.rm_rf(path_to_remove)
|
44
|
+
end
|
45
|
+
|
46
|
+
def base_directory_path
|
47
|
+
File.join(Rails.root, "log", "custom_log_space", current_controller.underscore, current_action)
|
48
|
+
end
|
49
|
+
|
50
|
+
def write_to_custom_log(message)
|
51
|
+
directory_path = File.join(base_directory_path, Time.now.strftime("%Y-%m-%d"))
|
52
|
+
FileUtils.mkdir_p(directory_path) unless Dir.exist?(directory_path)
|
53
|
+
custom_log_path = "#{directory_path}/#{Time.now.strftime("%H:%M")}.log"
|
54
|
+
|
55
|
+
File.open(custom_log_path, "a") do |file|
|
56
|
+
yield(file) # Header or other info can be passed and written here
|
57
|
+
file.puts(message)
|
58
|
+
end
|
59
|
+
rescue SystemCallError, IOError => e
|
60
|
+
error_prefix = e.is_a?(SystemCallError) ? "Error" : "IO Error"
|
61
|
+
puts "#{error_prefix}: #{e.message}"
|
62
|
+
end
|
63
|
+
|
64
|
+
# rubocop:disable Metrics/AbcSize
|
65
|
+
def write_header_information(file)
|
66
|
+
return if Thread.current[:header_written]
|
67
|
+
|
68
|
+
file.puts("") # Add a blank line for better readability.
|
69
|
+
file.puts "Started GET \"#{Thread.current[:path]}\" for ::1 at #{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}"
|
70
|
+
file.puts "Processing by #{current_controller}##{current_action} as HTML"
|
71
|
+
|
72
|
+
params = Thread.current[:params] || {}
|
73
|
+
file.puts "Parameters: #{params.inspect}" unless params.empty?
|
74
|
+
|
75
|
+
Thread.current[:header_written] = true
|
76
|
+
end
|
77
|
+
# rubocop:enable Metrics/AbcSize
|
78
|
+
end
|
79
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "custom_log_space/log_formatter"
|
4
|
-
require "custom_log_space/thread_manager"
|
5
|
-
require "custom_log_space/log_writer"
|
3
|
+
require "custom_log_space/base_helper/log_formatter"
|
4
|
+
require "custom_log_space/base_helper/thread_manager"
|
5
|
+
require "custom_log_space/base_helper/log_writer"
|
6
6
|
|
7
7
|
module CustomLogSpace
|
8
8
|
# CustomLogSpace::Subscriber is a class for handling custom logging in Rails applications.
|
data/lib/custom_log_space.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "custom_log_space/base_subscriber"
|
4
|
-
require "custom_log_space/sql_subscriber"
|
5
|
-
require "custom_log_space/view_subscriber"
|
3
|
+
require "custom_log_space/subscribers/base_subscriber"
|
4
|
+
require "custom_log_space/subscribers/sql_subscriber"
|
5
|
+
require "custom_log_space/subscribers/view_subscriber"
|
6
6
|
|
7
7
|
CustomLogSpace::BaseSubscriber.attach_to :action_controller
|
8
8
|
SQLSubscriber.attach_to :active_record
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_log_space
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nishikawa1031
|
@@ -30,8 +30,8 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '8.0'
|
33
|
-
description:
|
34
|
-
|
33
|
+
description: This gem organizes Rails logs by controller and action, eliminating the
|
34
|
+
need to start the server for log checks.
|
35
35
|
email:
|
36
36
|
- tatsunishitatsu@gmail.com
|
37
37
|
executables: []
|
@@ -47,13 +47,13 @@ files:
|
|
47
47
|
- Rakefile
|
48
48
|
- custom_log_space.gemspec
|
49
49
|
- lib/custom_log_space.rb
|
50
|
-
- lib/custom_log_space/
|
51
|
-
- lib/custom_log_space/
|
52
|
-
- lib/custom_log_space/
|
53
|
-
- lib/custom_log_space/
|
54
|
-
- lib/custom_log_space/
|
50
|
+
- lib/custom_log_space/base_helper/log_formatter.rb
|
51
|
+
- lib/custom_log_space/base_helper/log_writer.rb
|
52
|
+
- lib/custom_log_space/base_helper/thread_manager.rb
|
53
|
+
- lib/custom_log_space/subscribers/base_subscriber.rb
|
54
|
+
- lib/custom_log_space/subscribers/sql_subscriber.rb
|
55
|
+
- lib/custom_log_space/subscribers/view_subscriber.rb
|
55
56
|
- lib/custom_log_space/version.rb
|
56
|
-
- lib/custom_log_space/view_subscriber.rb
|
57
57
|
- sig/custom_log_space.rbs
|
58
58
|
homepage: https://github.com/nishikawa1031/custom_log_space.git
|
59
59
|
licenses:
|
@@ -1,107 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CustomLogSpace
|
4
|
-
# The LogWriter module provides methods for writing log messages to custom log files.
|
5
|
-
# It allows the creation of log directories, handling file errors, and appending messages to log files.
|
6
|
-
module LogWriter
|
7
|
-
private
|
8
|
-
|
9
|
-
def log_message(message)
|
10
|
-
current_controller = Thread.current[:current_controller]
|
11
|
-
current_action = Thread.current[:current_action]
|
12
|
-
|
13
|
-
return unless current_controller && current_action
|
14
|
-
|
15
|
-
write_to_custom_log(message) do |file|
|
16
|
-
write_header_information(file)
|
17
|
-
end
|
18
|
-
|
19
|
-
cleanup_old_directories
|
20
|
-
end
|
21
|
-
|
22
|
-
def cleanup_old_directories
|
23
|
-
return unless Dir.exist?(action_directory)
|
24
|
-
|
25
|
-
# If there are more than 3 date-directories, remove the oldest ones
|
26
|
-
remove_oldest_directory while all_directories.size > 3
|
27
|
-
end
|
28
|
-
|
29
|
-
def all_directories
|
30
|
-
@all_directories ||= Dir.entries(action_directory).select do |entry|
|
31
|
-
File.directory?(File.join(action_directory, entry)) && entry !~ /^\./
|
32
|
-
end.sort
|
33
|
-
end
|
34
|
-
|
35
|
-
def remove_oldest_directory
|
36
|
-
directory_to_remove = all_directories.shift
|
37
|
-
path_to_remove = File.join(action_directory, directory_to_remove)
|
38
|
-
FileUtils.rm_rf(path_to_remove)
|
39
|
-
end
|
40
|
-
|
41
|
-
def action_directory
|
42
|
-
@action_directory ||= begin
|
43
|
-
controller_name = Thread.current[:current_controller].underscore
|
44
|
-
action_name = Thread.current[:current_action]
|
45
|
-
File.join(Rails.root, "log", "custom_log_space", controller_name, action_name)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def write_to_custom_log(message)
|
50
|
-
directory_path = log_directory_based_on_format
|
51
|
-
FileUtils.mkdir_p(directory_path) unless Dir.exist?(directory_path)
|
52
|
-
custom_log_path = custom_log_file_path(directory_path)
|
53
|
-
|
54
|
-
File.open(custom_log_path, "a") do |file|
|
55
|
-
yield(file) # Header or other info can be passed and written here
|
56
|
-
file.puts(message)
|
57
|
-
end
|
58
|
-
rescue SystemCallError, IOError => e
|
59
|
-
handle_file_error(e)
|
60
|
-
end
|
61
|
-
|
62
|
-
def handle_file_error(error)
|
63
|
-
error_prefix = error.is_a?(SystemCallError) ? "Error" : "IO Error"
|
64
|
-
puts "#{error_prefix}: #{error.message}"
|
65
|
-
end
|
66
|
-
|
67
|
-
def write_header_information(file)
|
68
|
-
return if Thread.current[:header_written]
|
69
|
-
|
70
|
-
current_controller = Thread.current[:current_controller]
|
71
|
-
current_action = Thread.current[:current_action]
|
72
|
-
|
73
|
-
file.puts("") # Add a blank line for better readability.
|
74
|
-
write_request_info(file)
|
75
|
-
write_processing_info(file, current_controller, current_action)
|
76
|
-
write_parameters_info(file)
|
77
|
-
Thread.current[:header_written] = true
|
78
|
-
end
|
79
|
-
|
80
|
-
def write_request_info(file)
|
81
|
-
formatted_time = Time.now.strftime("%Y-%m-%d %H:%M:%S %z")
|
82
|
-
file.puts "Started GET \"#{Thread.current[:path]}\" for ::1 at #{formatted_time}"
|
83
|
-
end
|
84
|
-
|
85
|
-
def write_processing_info(file, current_controller, current_action)
|
86
|
-
file.puts "Processing by #{current_controller}##{current_action} as HTML"
|
87
|
-
end
|
88
|
-
|
89
|
-
def write_parameters_info(file)
|
90
|
-
params = Thread.current[:params] || {}
|
91
|
-
file.puts "Parameters: #{params.inspect}" unless params.empty?
|
92
|
-
end
|
93
|
-
|
94
|
-
def custom_log_file_path(directory_path)
|
95
|
-
time = Time.now.strftime("%H:%M")
|
96
|
-
"#{directory_path}/#{time}.log"
|
97
|
-
end
|
98
|
-
|
99
|
-
def log_directory_based_on_format
|
100
|
-
controller_name = Thread.current[:current_controller].underscore
|
101
|
-
action_name = Thread.current[:current_action]
|
102
|
-
date = Time.now.strftime("%Y-%m-%d")
|
103
|
-
|
104
|
-
File.join(Rails.root, "log", "custom_log_space", controller_name, action_name, date)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|