rails_semantic_logger 5.3.0 → 5.4.0
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/lib/generators/rails_semantic_logger/install/appenders_block.rb +100 -0
- data/lib/generators/rails_semantic_logger/install/arguments.rb +108 -0
- data/lib/generators/rails_semantic_logger/install/install_generator.rb +214 -0
- data/lib/generators/rails_semantic_logger/install/logger_settings.rb +383 -0
- data/lib/generators/rails_semantic_logger/install/templates/appenders.rb.erb +21 -0
- data/lib/rails_semantic_logger/options.rb +16 -1
- data/lib/rails_semantic_logger/sidekiq/job_logger.rb +1 -1
- data/lib/rails_semantic_logger/solid_queue/log_subscriber.rb +38 -2
- data/lib/rails_semantic_logger/version.rb +1 -1
- data/lib/rails_semantic_logger.rb +19 -7
- metadata +9 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 33c8f93215cf6a0ba8a6aa45540bb4848f02f068cf08b3160bb13439a7724678
|
|
4
|
+
data.tar.gz: e63e274d1ec68eaf6d3deb6207b234672bc77c2b8a2a4ca8b338163f4cf6de3d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6cc5a939a8bfd8c253e1731840f70b0decb571b9ef5f7339661c608e5b803ab871d652e40d8a9369e4397cb0af2e73ca46a510ca9f040323d884e75c805e297a
|
|
7
|
+
data.tar.gz: e8e9b262ec0d0d57fb863bd5524ecf89c432c5aa3cc8855502334b7cb7d1eb2e680b318ec9efe188836149c39b72d7b2eae6328c6211e6aed3306f8816f2adda
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
require "erb"
|
|
2
|
+
require_relative "logger_settings"
|
|
3
|
+
|
|
4
|
+
module RailsSemanticLogger
|
|
5
|
+
module Generators
|
|
6
|
+
# The explicit `config.rails_semantic_logger.appenders` block of one environment, as Ruby source.
|
|
7
|
+
#
|
|
8
|
+
# Its active lines create exactly the appenders that Rails Semantic Logger creates when an
|
|
9
|
+
# application declares none: the log file, config.paths["log"], with the formatter that
|
|
10
|
+
# RailsSemanticLogger.default_file_formatter gives, the screen under `rails server` and Sidekiq,
|
|
11
|
+
# and the screen under `rails console`. Listing them lets the application see, and change, where
|
|
12
|
+
# its logs go. A deployed environment also lists, commented out, the usual alternatives.
|
|
13
|
+
#
|
|
14
|
+
# It carries the deprecated appender options that the application set, as the engine applies them:
|
|
15
|
+
# `format` and `ap_options` become the log file's formatter, `filter` its filter,
|
|
16
|
+
# `add_file_appender = false` comments out the log file, and `console_logger = false` the screen.
|
|
17
|
+
# It lists each appender that the application added with `config.semantic_logger.add_appender`,
|
|
18
|
+
# and, as Rails Semantic Logger did, comments out the screen lines when one of those already
|
|
19
|
+
# writes to the screen.
|
|
20
|
+
class AppendersBlock
|
|
21
|
+
TEMPLATE = File.expand_path("templates/appenders.rb.erb", __dir__)
|
|
22
|
+
|
|
23
|
+
# environment: [String] such as "production", from the name of the environment's file.
|
|
24
|
+
# options: [Hash] the value of each deprecated appender option, by name, as Ruby source (see LoggerSettings).
|
|
25
|
+
# appenders: [Array<String>] the arguments of each appender the application added itself.
|
|
26
|
+
# colorize: [true|false] whether colorized logging is on, as `config.colorize_logging` says.
|
|
27
|
+
# screen: [true|false] whether an appender that stays outside the block, such as one that
|
|
28
|
+
# config/application.rb adds, already writes to the screen.
|
|
29
|
+
def initialize(environment:, options: {}, appenders: [], colorize: true, screen: false)
|
|
30
|
+
@environment = environment
|
|
31
|
+
@options = options
|
|
32
|
+
@appenders = appenders
|
|
33
|
+
@colorize = colorize
|
|
34
|
+
@screen = screen
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Returns [String] the block, indented for the `Rails.application.configure` block of the
|
|
38
|
+
# environment's file, followed by a blank line.
|
|
39
|
+
def to_s
|
|
40
|
+
ERB.new(File.read(TEMPLATE), trim_mode: "-").result(binding)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Whether every line of the block would be commented out. A block that declares no appender
|
|
44
|
+
# declares nothing, so the engine would create its default appenders instead.
|
|
45
|
+
def empty?
|
|
46
|
+
!file? && !screen? && @appenders.empty?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
attr_reader :environment
|
|
52
|
+
|
|
53
|
+
def development? = environment == "development"
|
|
54
|
+
|
|
55
|
+
def test? = environment == "test"
|
|
56
|
+
|
|
57
|
+
# Any environment other than development and test is deployed, such as production or staging.
|
|
58
|
+
def deployed? = !development? && !test?
|
|
59
|
+
|
|
60
|
+
def file? = @options[:add_file_appender] != "false"
|
|
61
|
+
|
|
62
|
+
def screen?
|
|
63
|
+
return false if @screen || @options[:console_logger] == "false"
|
|
64
|
+
|
|
65
|
+
@appenders.none? { |args| LoggerSettings.screen_appender?(args) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def file_arguments
|
|
69
|
+
# The engine's default log file: absolute, under Rails.root, as config/application.rb sets it, and
|
|
70
|
+
# of the environment that runs, also when another environment's file loads this one, such as a
|
|
71
|
+
# staging.rb that starts with `require_relative "production"`.
|
|
72
|
+
arguments = %(file_name: config.paths["log"].first, formatter: #{file_formatter})
|
|
73
|
+
arguments += ", filter: #{@options[:filter]}" if @options[:filter]
|
|
74
|
+
arguments
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The engine formats the log file with `format`, in color with `ap_options` when it is :default.
|
|
78
|
+
def file_formatter
|
|
79
|
+
format = @options[:format]
|
|
80
|
+
return format if format && format != ":default"
|
|
81
|
+
return source(RailsSemanticLogger.default_file_formatter(colorize: @colorize)) unless @options[:ap_options]
|
|
82
|
+
return ":default" unless @colorize
|
|
83
|
+
|
|
84
|
+
"{color: {ap: #{@options[:ap_options]}}}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The Ruby source of a formatter: a Hash of Symbols, or a Symbol, written as an application would.
|
|
88
|
+
def source(value)
|
|
89
|
+
case value
|
|
90
|
+
when Hash then "{#{value.map { |key, item| "#{key}: #{source(item)}" }.join(', ')}}"
|
|
91
|
+
when Symbol then ":#{value}"
|
|
92
|
+
else value.inspect
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Prefixes the line with a comment marker when the appender is turned off.
|
|
97
|
+
def line(text, active: true) = active ? text : "# #{text}"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
require "ripper"
|
|
2
|
+
|
|
3
|
+
module RailsSemanticLogger
|
|
4
|
+
module Generators
|
|
5
|
+
# The Ruby source of the arguments of one method call, such as those of
|
|
6
|
+
# `config.semantic_logger.add_appender(...)`, or the value of a setting.
|
|
7
|
+
#
|
|
8
|
+
# Knows whether they can move to the start of an environment's settings, into its appenders
|
|
9
|
+
# block: only when they are the arguments of one call and nothing else, and read no local
|
|
10
|
+
# variable of the file. It asks Ripper.
|
|
11
|
+
class Arguments
|
|
12
|
+
# Ruby's implicit block parameters, and the nodes that open a block of their own.
|
|
13
|
+
IMPLICIT_PARAMETERS = (%w[it] + (1..9).map { |i| "_#{i}" }).freeze
|
|
14
|
+
BLOCKS = %i[brace_block do_block lambda].freeze
|
|
15
|
+
|
|
16
|
+
def initialize(source)
|
|
17
|
+
@source = source
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Whether the arguments can move into the appenders block.
|
|
21
|
+
def movable?
|
|
22
|
+
one_call? && !local_reference?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def call = "f(#{@source})"
|
|
28
|
+
|
|
29
|
+
# Whether `f(source)` is one call alone, not a call chained onto, assigned to, or followed by
|
|
30
|
+
# another statement, as in `config.semantic_logger.add_appender(appender: :syslog).name = "syslog"`.
|
|
31
|
+
def one_call?
|
|
32
|
+
statements = Ripper.sexp(call)&.[](1)
|
|
33
|
+
statements&.size == 1 && statements[0][0] == :method_add_arg
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Whether the arguments name a local variable, or call a method without a receiver, such as
|
|
37
|
+
# `file_name: log_path` or `log_file("app")`, which the appenders block may not see, since it
|
|
38
|
+
# moves to the start of the environment's settings. That includes `config`: the block runs before
|
|
39
|
+
# the settings below it, so `level: config.log_level` would read the level before it is set.
|
|
40
|
+
#
|
|
41
|
+
# The parameters of a block or lambda in them, such as `log` in
|
|
42
|
+
# `filter: ->(log) { log.level == :error }`, are not local variables of the file.
|
|
43
|
+
def local_reference?
|
|
44
|
+
# The arguments of `f(...)`, without the call to `f` that wraps them to parse them.
|
|
45
|
+
references_local?(Ripper.sexp(call)&.dig(1, 0, 2), [])
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Whether the tree reads a local variable or calls a method without a receiver, other than the
|
|
49
|
+
# parameters of the blocks it is inside of.
|
|
50
|
+
def references_local?(node, parameters)
|
|
51
|
+
return false unless node.is_a?(Array)
|
|
52
|
+
|
|
53
|
+
if BLOCKS.include?(node[0])
|
|
54
|
+
names, defaults = block_parameters(node[1])
|
|
55
|
+
# `it` and `_1` are parameters only of a block that declares none of its own.
|
|
56
|
+
inner = parameters + (names.empty? ? IMPLICIT_PARAMETERS : names)
|
|
57
|
+
# A parameter's default value is read outside the block's own parameters.
|
|
58
|
+
return defaults.any? { |default| references_local?(default, parameters) } ||
|
|
59
|
+
node[2..].any? { |child| references_local?(child, inner) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
return true if local_name?(node, parameters)
|
|
63
|
+
|
|
64
|
+
node.any? { |child| references_local?(child, parameters) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Whether the node reads a local variable, or calls a method without a receiver, other than a
|
|
68
|
+
# block parameter, or Ruby's own `lambda` and `proc`.
|
|
69
|
+
def local_name?(node, parameters)
|
|
70
|
+
ident = node[1] if node[1].is_a?(Array) && node[1][0] == :@ident
|
|
71
|
+
return false unless ident && %i[vcall var_ref fcall command].include?(node[0])
|
|
72
|
+
return false if %i[fcall command].include?(node[0]) && %w[lambda proc].include?(ident[1])
|
|
73
|
+
|
|
74
|
+
!(%i[var_ref vcall].include?(node[0]) && parameters.include?(ident[1]))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Returns [Array<String>, Array] the names of a block's or lambda's parameters, keyword ones and
|
|
78
|
+
# block-local ones (`|x; y|`) included, and the trees of their default values.
|
|
79
|
+
def block_parameters(node)
|
|
80
|
+
block_locals = node.is_a?(Array) && node[0] == :block_var ? identifiers(node[2]) : []
|
|
81
|
+
node = node[1] while node.is_a?(Array) && %i[paren block_var].include?(node[0])
|
|
82
|
+
return [block_locals, []] unless node.is_a?(Array) && node[0] == :params
|
|
83
|
+
|
|
84
|
+
_, required, optional, rest, post, keywords, keyword_rest, block = node
|
|
85
|
+
names = block_locals +
|
|
86
|
+
[required, post, rest, keyword_rest, block].flat_map { |slot| identifiers(slot) }
|
|
87
|
+
defaults = []
|
|
88
|
+
optional&.each do |name, default|
|
|
89
|
+
names.concat(identifiers(name))
|
|
90
|
+
defaults << default
|
|
91
|
+
end
|
|
92
|
+
keywords&.each do |label, default|
|
|
93
|
+
names << label[1].delete_suffix(":")
|
|
94
|
+
defaults << default if default
|
|
95
|
+
end
|
|
96
|
+
[names, defaults]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The names of the identifiers in a parameter slot, such as those of `(a, b)`.
|
|
100
|
+
def identifiers(node)
|
|
101
|
+
return [] unless node.is_a?(Array)
|
|
102
|
+
return [node[1]] if node[0] == :@ident
|
|
103
|
+
|
|
104
|
+
node.flat_map { |child| identifiers(child) }
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
require "rails/generators"
|
|
2
|
+
require "ripper"
|
|
3
|
+
require_relative "logger_settings"
|
|
4
|
+
require_relative "appenders_block"
|
|
5
|
+
|
|
6
|
+
module RailsSemanticLogger
|
|
7
|
+
module Generators
|
|
8
|
+
# Configures an existing Rails application for Rails Semantic Logger:
|
|
9
|
+
#
|
|
10
|
+
# bin/rails generate rails_semantic_logger:install
|
|
11
|
+
#
|
|
12
|
+
# In config/application.rb and each config/environments/*.rb it removes the logger settings that
|
|
13
|
+
# Rails Semantic Logger ignores or refuses (see LoggerSettings), then writes into each environment
|
|
14
|
+
# an explicit `config.rails_semantic_logger.appenders` block (see AppendersBlock) that creates the
|
|
15
|
+
# same appenders as before, so that where the logs go is plain to see, and to change.
|
|
16
|
+
#
|
|
17
|
+
# It leaves alone the appenders of an environment that already declares them. When
|
|
18
|
+
# config/application.rb declares them, it writes no environment block at all, since the
|
|
19
|
+
# declarations of every file add up. It writes each file once, and only when the result is valid
|
|
20
|
+
# Ruby, and reports each setting it removed, and each one it left for the application to move.
|
|
21
|
+
#
|
|
22
|
+
# The generator knows the application's files. What to remove belongs to LoggerSettings, and what
|
|
23
|
+
# to write to AppendersBlock.
|
|
24
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
25
|
+
APPLICATION = "config/application.rb".freeze
|
|
26
|
+
|
|
27
|
+
# What it will write to one environment's file: the text, or nil when the file cannot take its
|
|
28
|
+
# appenders, with the reason, and the cleaning that produced it.
|
|
29
|
+
Plan = Struct.new(:path, :text, :reason, :result, :declared, :inherits, keyword_init: true)
|
|
30
|
+
|
|
31
|
+
NO_CONFIGURE = "found no `.configure do` block to add the appenders to, " \
|
|
32
|
+
"or adding them would not be valid Ruby".freeze
|
|
33
|
+
UNMOVED = "an appenders block would override or repeat the settings left in place: " \
|
|
34
|
+
"move them into config.rails_semantic_logger.appenders".freeze
|
|
35
|
+
LOG_PATH = "it sets config.paths[\"log\"], which its appenders block would read before it is set".freeze
|
|
36
|
+
EMPTY = "its appenders block would declare no appender, so the engine would create its " \
|
|
37
|
+
"default appenders instead".freeze
|
|
38
|
+
|
|
39
|
+
desc "Remove the Rails logger settings that Rails Semantic Logger ignores, and list each " \
|
|
40
|
+
"environment's appenders in config.rails_semantic_logger.appenders."
|
|
41
|
+
|
|
42
|
+
# Plans every file before writing any. The deprecated options and appenders of
|
|
43
|
+
# config/application.rb move into each environment's block, so only when no file declares
|
|
44
|
+
# appenders and every environment can take its block; otherwise they stay where they are.
|
|
45
|
+
def install
|
|
46
|
+
# `rails destroy` cannot put back what was removed, and must not delete the settings files.
|
|
47
|
+
return if behavior == :revoke
|
|
48
|
+
|
|
49
|
+
@application = LoggerSettings.new(read(APPLICATION))
|
|
50
|
+
declared = @application.declares_appenders? ||
|
|
51
|
+
environments.any? { |path| settings(path).declares_appenders? }
|
|
52
|
+
app_result, plans = plan(convert: !declared)
|
|
53
|
+
app_result, plans = plan(convert: false) if !declared && plans.any? { |plan| plan.text.nil? }
|
|
54
|
+
|
|
55
|
+
write(APPLICATION, app_result.text, app_result)
|
|
56
|
+
plans.each { |plan| apply(plan, app_result) }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
# Returns the cleaning of config/application.rb, and the plan of each environment.
|
|
62
|
+
def plan(convert:)
|
|
63
|
+
app_result = @application.clean(convert: convert)
|
|
64
|
+
screen = LoggerSettings.new(app_result.text).adds_screen_appender?
|
|
65
|
+
[app_result, environments.map { |path| plan_environment(path, app_result, screen) }]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def plan_environment(path, app_result, screen)
|
|
69
|
+
settings = settings(path)
|
|
70
|
+
declared = @application.declares_appenders? || settings.declares_appenders?
|
|
71
|
+
# An environment that loads another one gets that one's appenders, and keeps its own.
|
|
72
|
+
inherits = !declared && settings.loads_environment?(environment_names - [File.basename(path, ".rb")])
|
|
73
|
+
result = settings.clean(convert: !declared && !inherits)
|
|
74
|
+
if declared || inherits
|
|
75
|
+
return Plan.new(path: path, text: result.text, result: result, declared: true, inherits: inherits)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
plan_block(Plan.new(path: path, result: result, declared: false), settings, app_result, screen)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Why the environment cannot take an appenders block, if it cannot: settings that stay in place,
|
|
82
|
+
# in its file or another that it shares, would be overridden by the block, or read too late.
|
|
83
|
+
def unlistable(plan, settings, app_result)
|
|
84
|
+
return UNMOVED if (app_result.unmoved + plan.result.unmoved).any?
|
|
85
|
+
return LOG_PATH if settings.sets_log_path?
|
|
86
|
+
|
|
87
|
+
loader, *through = loader_with_own_appenders(plan.path)
|
|
88
|
+
return unless loader
|
|
89
|
+
|
|
90
|
+
loads = through.empty? ? "#{loader} loads it" : "#{loader} loads it (through #{through.join(', ')})"
|
|
91
|
+
if settings(loader).sets_log_path?
|
|
92
|
+
return "#{loads}, and sets config.paths[\"log\"] after its appenders block would read it"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
"#{loads}, and an appenders block would override its settings"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The environment whose file loads this one's and keeps settings that this one's appenders block
|
|
99
|
+
# would override or repeat, if any, such as a staging.rb that requires production.rb and sets
|
|
100
|
+
# `config.rails_semantic_logger.format`.
|
|
101
|
+
# Follows the loads, so that a qa.rb that loads staging.rb, which loads production.rb, counts.
|
|
102
|
+
# Returns [Array<String>|nil] the loader, then the environments it loads this one through, if any,
|
|
103
|
+
# such as ["config/environments/qa.rb", "config/environments/staging.rb"].
|
|
104
|
+
def loader_with_own_appenders(path, seen = [path])
|
|
105
|
+
loaders(path, seen).each do |loader|
|
|
106
|
+
return [loader] if settings(loader).sets_own_appenders?
|
|
107
|
+
|
|
108
|
+
chain = loader_with_own_appenders(loader, seen << loader)
|
|
109
|
+
return chain << loader if chain
|
|
110
|
+
end
|
|
111
|
+
nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The other environments whose file loads this one's.
|
|
115
|
+
def loaders(path, seen)
|
|
116
|
+
name = File.basename(path, ".rb")
|
|
117
|
+
(environments - seen).select { |other| settings(other).loads_environment?([name]) }
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Returns the plan with the text of the environment's file with its appenders block, or with the
|
|
121
|
+
# reason it cannot take one.
|
|
122
|
+
def plan_block(plan, settings, app_result, screen)
|
|
123
|
+
result = plan.result
|
|
124
|
+
reason = unlistable(plan, settings, app_result)
|
|
125
|
+
return plan.tap { plan.reason = reason } if reason
|
|
126
|
+
|
|
127
|
+
block = AppendersBlock.new(
|
|
128
|
+
environment: File.basename(plan.path, ".rb"),
|
|
129
|
+
options: app_result.options.merge(result.options),
|
|
130
|
+
appenders: app_result.appenders + result.appenders,
|
|
131
|
+
colorize: !(@application.colorize_logging_off? || settings.colorize_logging_off?),
|
|
132
|
+
screen: screen || LoggerSettings.new(result.text).adds_screen_appender?
|
|
133
|
+
)
|
|
134
|
+
return plan.tap { plan.reason = EMPTY } if block.empty?
|
|
135
|
+
|
|
136
|
+
text = LoggerSettings.new(result.text).with_block(block.to_s)
|
|
137
|
+
return plan.tap { plan.reason = NO_CONFIGURE } unless text && Ripper.sexp(text)
|
|
138
|
+
|
|
139
|
+
plan.tap { plan.text = text }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def apply(plan, app_result)
|
|
143
|
+
if plan.inherits
|
|
144
|
+
skip(plan, "it loads another environment's settings, appenders included")
|
|
145
|
+
elsif plan.declared
|
|
146
|
+
where = settings(plan.path).declares_appenders? ? plan.path : APPLICATION
|
|
147
|
+
skip(plan, "its appenders are declared in #{where}")
|
|
148
|
+
elsif plan.text
|
|
149
|
+
say_status :appenders, plan.path if write(plan.path, plan.text, plan.result)
|
|
150
|
+
else
|
|
151
|
+
say_status :error, "#{plan.path}: left unchanged: #{plan.reason}", :red
|
|
152
|
+
warn_left(plan.path, plan.result)
|
|
153
|
+
warn_unconverted(plan.path, app_result.options.merge(plan.result.options),
|
|
154
|
+
app_result.appenders + plan.result.appenders)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Writes the cleaned settings of an environment that gets no appenders block of its own.
|
|
159
|
+
def skip(plan, reason)
|
|
160
|
+
say_status :skip, "#{plan.path}: #{reason}", :yellow
|
|
161
|
+
write(plan.path, plan.text, plan.result)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Writes the text, when it changed and is valid Ruby, reporting what was removed, and each setting
|
|
165
|
+
# that was left in place. Returns [String|nil] the text, when it wrote it.
|
|
166
|
+
def write(path, text, result)
|
|
167
|
+
warn_left(path, result)
|
|
168
|
+
return if text == read(path)
|
|
169
|
+
|
|
170
|
+
unless Ripper.sexp(text)
|
|
171
|
+
say_status :error, "#{path}: left unchanged, since the change would not be valid Ruby", :red
|
|
172
|
+
return
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
result.removed.each { |line| say_status :remove, "#{path}: #{line.strip}", :red }
|
|
176
|
+
create_file(path, text, force: true, verbose: false)
|
|
177
|
+
text
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def warn_left(path, result)
|
|
181
|
+
result.left.each do |setting|
|
|
182
|
+
say_status :warn, "#{path}: left in place, move it into the appenders: #{setting.lines.first.strip}", :yellow
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# The deprecated appender options and the added appenders cannot be carried into appenders that the application
|
|
187
|
+
# declares itself.
|
|
188
|
+
def warn_unconverted(path, options, appenders)
|
|
189
|
+
return if options.empty? && appenders.empty?
|
|
190
|
+
|
|
191
|
+
settings = options.keys.map { |name| "rails_semantic_logger.#{name}" } +
|
|
192
|
+
appenders.map { |args| "add_appender(#{args})" }
|
|
193
|
+
say_status :warn, "#{path}: carry these into its appenders: #{settings.join(', ')}", :yellow
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
def settings(path)
|
|
197
|
+
LoggerSettings.new(read(path))
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def environment_names = environments.map { |path| File.basename(path, ".rb") }
|
|
201
|
+
|
|
202
|
+
def environments
|
|
203
|
+
Dir[File.join(destination_root, "config/environments/*.rb")].map do |path|
|
|
204
|
+
path.delete_prefix("#{destination_root}/")
|
|
205
|
+
end.sort
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def read(path)
|
|
209
|
+
file = File.join(destination_root, path)
|
|
210
|
+
File.exist?(file) ? File.read(file) : ""
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
require "ripper"
|
|
2
|
+
require_relative "arguments"
|
|
3
|
+
|
|
4
|
+
module RailsSemanticLogger
|
|
5
|
+
module Generators
|
|
6
|
+
# The logger settings in one Rails configuration file, such as config/environments/production.rb.
|
|
7
|
+
#
|
|
8
|
+
# Knows which of them Rails Semantic Logger ignores or refuses, and removes them:
|
|
9
|
+
# - `config.logger = ...`, which Rails Semantic Logger replaces, including the multi-line form
|
|
10
|
+
# of Rails 7.2 (`ActiveSupport::Logger.new(STDOUT).tap { ... }.then { ... }`);
|
|
11
|
+
# - `config.log_formatter = ...`, which it never reads;
|
|
12
|
+
# - the `if ENV["RAILS_LOG_TO_STDOUT"].present?` block of Rails 6 and 7.0, when it does nothing else;
|
|
13
|
+
# - commented-out examples of another Rails logger, such as Syslog::Logger, and the comments
|
|
14
|
+
# directly above each removed setting;
|
|
15
|
+
# - and, when asked to convert them, the deprecated appender options
|
|
16
|
+
# (Options::DEPRECATED_APPENDER_OPTIONS) and `config.semantic_logger.add_appender(...)`, keeping
|
|
17
|
+
# the value of each, since an application that lists its appenders lists those too.
|
|
18
|
+
#
|
|
19
|
+
# It keeps `config.log_level` and `config.log_tags`, which Rails Semantic Logger reads. It removes
|
|
20
|
+
# only the settings of the file's top level: one inside a condition, or with a condition of its
|
|
21
|
+
# own, applies only sometimes, so it is left in place for the application to move, and reported.
|
|
22
|
+
# It works on the text line by line, and asks Ripper where each statement ends.
|
|
23
|
+
class LoggerSettings
|
|
24
|
+
# text: [String] the text without the settings it removed.
|
|
25
|
+
# removed: [Array<String>] each line it removed.
|
|
26
|
+
# options: [Hash] the value of each deprecated appender option, by name, as Ruby source.
|
|
27
|
+
# appenders: [Array<String>] the arguments of each `config.semantic_logger.add_appender` it removed.
|
|
28
|
+
# left: [Array<String>] each setting it left in place, for the application to move.
|
|
29
|
+
# unmoved: [Array<String>] those of them that an appenders block would override or repeat: a
|
|
30
|
+
# deprecated appender option, which the engine ignores once appenders are declared, or
|
|
31
|
+
# an appender that writes to the screen, as the block's screen lines do.
|
|
32
|
+
Result = Struct.new(:text, :removed, :options, :appenders, :left, :unmoved, keyword_init: true)
|
|
33
|
+
|
|
34
|
+
# The options that turn an appender on or off, converted only when set to a literal true or false.
|
|
35
|
+
SWITCHES = %i[add_file_appender console_logger].freeze
|
|
36
|
+
|
|
37
|
+
IGNORED = /\A\s*config\.(?:logger|log_formatter)\s*(?:\|\|)?=/
|
|
38
|
+
DEPRECATED_OPTION =
|
|
39
|
+
/\A\s*config\.rails_semantic_logger\.(#{Options::DEPRECATED_APPENDER_OPTIONS.join('|')})\s*=\s*(.*)\z/m
|
|
40
|
+
ADD_APPENDER = /\A\s*config\.semantic_logger\.add_appender\b/
|
|
41
|
+
APPENDER_ARGS = /\A\s*config\.semantic_logger\.add_appender\s*(?:\((?<args>.*)\)|\s(?<args>.*))\z/m
|
|
42
|
+
LOG_TO_STDOUT = /\A(\s*)(?:if|unless)\b.*RAILS_LOG_TO_STDOUT/
|
|
43
|
+
COMMENTED_LOGGER = %r{\A\s*#\s*(?:config\.logger\s*=|require\s+["']syslog/logger["'])}
|
|
44
|
+
COMMENTED_CODE = /\A\s*#\s*config\./
|
|
45
|
+
APPENDERS = /config\.rails_semantic_logger\.appenders\b/
|
|
46
|
+
COLORIZE_OFF = /\A\s*config\.colorize_logging\s*=\s*false\b/
|
|
47
|
+
COLORIZE = /\A\s*config\.colorize_logging\s*=/
|
|
48
|
+
# What may set where the log file goes: any use of `paths` (`config.paths`, or `paths`, which the
|
|
49
|
+
# application delegates to its config, on any receiver), such as `paths["log"] = ...`, `.unshift`,
|
|
50
|
+
# `.add("log")`, `send(:paths)`, or `paths` given to a block or a variable, except a read of
|
|
51
|
+
# `paths["log"]`, a use of another named path, a `paths.add` of another one, a `paths:` keyword,
|
|
52
|
+
# and an `@paths` or `$paths` variable. It is matched against the whole file without its comments
|
|
53
|
+
# and without the `paths` of a namespace of the config (CONFIG_NAMESPACE_PATHS), so that a call
|
|
54
|
+
# continued on the next line counts. Erring toward a setting only leaves an environment
|
|
55
|
+
# unchanged, and reported.
|
|
56
|
+
LOG_PATH = /
|
|
57
|
+
(?<![@$])\bpaths\b(?!:(?!:))
|
|
58
|
+
(?!\s*\[\s*["']log["']\s*\]\s*&?\.\s*
|
|
59
|
+
(?:first|last|expanded|existent|existent_directories|to_a|each|glob|paths|children)\b(?!\s*=[^=]))
|
|
60
|
+
(?!\s*\[\s*["'](?!log["'])[^"']*["']\s*\])
|
|
61
|
+
(?!\s*&?\.\s*add\b[ \t]*\(?[ \t]*["'](?!log["']))
|
|
62
|
+
/x
|
|
63
|
+
# The `paths` of a namespace of the config, such as `config.assets.paths` or
|
|
64
|
+
# `Rails.configuration.assets.paths`, which are not the application's paths, so cannot set where
|
|
65
|
+
# the log file goes.
|
|
66
|
+
CONFIG_NAMESPACE_PATHS = /\bconfig(?:uration)?\s*&?\.\s*(?!paths\b)\w+\s*&?\.\s*paths\b/
|
|
67
|
+
# The start of an environment's settings, `Rails.application.configure do` or
|
|
68
|
+
# `MyApp::Application.configure do`, with the comment that Rails writes first, if any.
|
|
69
|
+
CONFIGURE = /
|
|
70
|
+
^[ \t]*(?:Rails\.application|[\w:]+::Application)\.configure\ do(?:[ \t]*\|\w+\|)?[ \t]*(?:\#.*)?\r?\n
|
|
71
|
+
(?<comment>[ \t]*\#\ Settings\ specified\ here\ will\ take\ precedence.*\r?\n)?
|
|
72
|
+
/x
|
|
73
|
+
# A line that loads the settings of another environment, such as `require_relative "production"`.
|
|
74
|
+
LOADS_FILE = %r{\A\s*require(?:_relative)?\b.*["'](?:\./)?(?<name>\w+)(?:\.rb)?["']}
|
|
75
|
+
# The start of the settings of config/application.rb.
|
|
76
|
+
APPLICATION_CLASS = /\A\s*class\s+\S+\s*<\s*(?:::)?Rails::Application\b/
|
|
77
|
+
SCREEN = /\bio:\s*(?:\$stdout|\$stderr|STDOUT|STDERR)\b/
|
|
78
|
+
OPENS_BLOCK = /(?:\bdo(?:\s*\|[^|]*\|)?|\A\s*(?:class|module)\b.*)\s*\z/
|
|
79
|
+
CLOSES_BLOCK = /\A\s*end\b/
|
|
80
|
+
|
|
81
|
+
# What the RAILS_LOG_TO_STDOUT block of Rails 6 and 7.0 holds. A block that holds anything else,
|
|
82
|
+
# such as `config.rails_semantic_logger.add_file_appender = false`, is left in place.
|
|
83
|
+
LOG_TO_STDOUT_LINE = /
|
|
84
|
+
\A\s*(?:\#|end\b|config\.(?:logger|log_formatter)\s*=|logger(?:\.formatter)?\s*=|(?:\$stdout|STDOUT)\.sync\s*=)
|
|
85
|
+
/x
|
|
86
|
+
|
|
87
|
+
# The comment that Rails 8 writes above `config.log_tags` and `config.logger`, which no longer
|
|
88
|
+
# holds once `config.logger` is gone, and what it becomes.
|
|
89
|
+
RAILS_COMMENTS = {
|
|
90
|
+
"# Log to STDOUT with the current request id as a default log tag." =>
|
|
91
|
+
"# Tag every log entry with the current request id."
|
|
92
|
+
}.freeze
|
|
93
|
+
|
|
94
|
+
# The tokens of a comment, `# ...` or `=begin ... =end`.
|
|
95
|
+
COMMENT_TOKENS = %i[on_comment on_embdoc_beg on_embdoc on_embdoc_end].freeze
|
|
96
|
+
|
|
97
|
+
def initialize(text)
|
|
98
|
+
@lines = text.lines
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Whether the file declares appenders through `config.rails_semantic_logger.appenders`.
|
|
102
|
+
def declares_appenders?
|
|
103
|
+
@lines.any? { |line| !comment?(line) && line.match?(APPENDERS) }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Whether the arguments of an appender write to the screen: stdout or stderr.
|
|
107
|
+
def self.screen_appender?(args)
|
|
108
|
+
args.match?(SCREEN)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Whether the file adds an appender that writes to the screen with
|
|
112
|
+
# `config.semantic_logger.add_appender`.
|
|
113
|
+
# Only an unconditional one at the top level counts: one with a condition writes to the screen
|
|
114
|
+
# only sometimes.
|
|
115
|
+
def adds_screen_appender?
|
|
116
|
+
@lines.each_index.any? do |index|
|
|
117
|
+
next false unless @lines[index].match?(ADD_APPENDER) && top_level?(index)
|
|
118
|
+
|
|
119
|
+
statement = without_comments(@lines[index..statement_end(index)].join)
|
|
120
|
+
!conditional?(statement) && self.class.screen_appender?(statement)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Returns [String|nil] the text with the block added at the start of the environment's
|
|
125
|
+
# settings, after the comment that Rails writes there first and the blank line after it, in the
|
|
126
|
+
# file's line endings, or nil when the file has no `.configure do` block.
|
|
127
|
+
def with_block(block)
|
|
128
|
+
text = @lines.join
|
|
129
|
+
start = text.match(CONFIGURE)
|
|
130
|
+
return unless start
|
|
131
|
+
|
|
132
|
+
position = start.end(0)
|
|
133
|
+
if start[:comment]
|
|
134
|
+
blank = text[position..][/\A[ \t]*\r?\n/]
|
|
135
|
+
blank ? position += blank.size : block = "\n#{block}"
|
|
136
|
+
end
|
|
137
|
+
block = block.gsub(/\r?\n/, "\r\n") if start[0].end_with?("\r\n")
|
|
138
|
+
text.insert(position, block)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Whether the file loads the settings of one of the environments, such as a staging.rb that
|
|
142
|
+
# starts with `require_relative "production"`, and so gets that environment's appenders too.
|
|
143
|
+
def loads_environment?(environments)
|
|
144
|
+
@lines.any? do |line|
|
|
145
|
+
name = line.match(LOADS_FILE)&.[](:name)
|
|
146
|
+
!comment?(line) && name && environments.include?(name)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Whether the file decides anything about its appenders: declares them, sets a deprecated appender
|
|
151
|
+
# option, adds an appender that writes to the screen, sets where the log file goes, or sets
|
|
152
|
+
# `config.colorize_logging` to any value, anywhere (even `true` overrides a `false` in the file
|
|
153
|
+
# that it loads).
|
|
154
|
+
# An appenders block in a file that it loads, such as production.rb for a staging.rb that starts
|
|
155
|
+
# with `require_relative "production"`, would add to, override or repeat them.
|
|
156
|
+
def sets_own_appenders?
|
|
157
|
+
declares_appenders? || sets_log_path? ||
|
|
158
|
+
@lines.each_index.any? { |index| appender_setting?(index) || @lines[index].match?(COLORIZE) }
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Whether the file sets where the log file goes, `config.paths["log"]`, which an appenders block
|
|
162
|
+
# above it would read before it is set.
|
|
163
|
+
def sets_log_path?
|
|
164
|
+
without_comments(@lines.join).gsub(CONFIG_NAMESPACE_PATHS, "config").match?(LOG_PATH)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Whether the file turns colorized logging off, which makes the default log file plain text.
|
|
168
|
+
def colorize_logging_off?
|
|
169
|
+
@lines.each_index.any? do |index|
|
|
170
|
+
@lines[index].match?(COLORIZE_OFF) && top_level?(index) &&
|
|
171
|
+
!conditional?(without_comments(@lines[index..statement_end(index)].join))
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Returns [Result] the text without the settings that Rails Semantic Logger ignores or refuses.
|
|
176
|
+
#
|
|
177
|
+
# convert: [true|false] whether the file's appenders will be listed, so that it removes each
|
|
178
|
+
# deprecated appender option and `config.semantic_logger.add_appender`, keeping their values for
|
|
179
|
+
# the list. Otherwise it leaves the options in place, and reports them, since they still apply.
|
|
180
|
+
def clean(convert: false)
|
|
181
|
+
@removed = Array.new(@lines.size, false)
|
|
182
|
+
@result = Result.new(removed: [], options: {}, appenders: [], left: [], unmoved: [])
|
|
183
|
+
@convert = convert
|
|
184
|
+
index = 0
|
|
185
|
+
index = visit(index) + 1 while index < @lines.size
|
|
186
|
+
remove_comments_above_removed
|
|
187
|
+
|
|
188
|
+
kept = kept_lines
|
|
189
|
+
kept = reword_rails_comments(kept) if @removed.any?
|
|
190
|
+
@result.text = tidy_blank_lines(kept).join
|
|
191
|
+
@result.removed = @lines.select.with_index { |_, i| @removed[i] }
|
|
192
|
+
@result
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
private
|
|
196
|
+
|
|
197
|
+
# Handles the setting that starts at the line, if any, and returns the index of its last line.
|
|
198
|
+
def visit(index)
|
|
199
|
+
line = @lines[index]
|
|
200
|
+
if (match = line.match(LOG_TO_STDOUT))
|
|
201
|
+
log_to_stdout(index, match[1])
|
|
202
|
+
elsif line.match?(IGNORED) || line.match?(DEPRECATED_OPTION) || (@convert && line.match?(ADD_APPENDER))
|
|
203
|
+
setting(index)
|
|
204
|
+
elsif line.match?(COLORIZE)
|
|
205
|
+
colorize(index)
|
|
206
|
+
else
|
|
207
|
+
remove(index, index) if line.match?(COMMENTED_LOGGER)
|
|
208
|
+
index
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def log_to_stdout(index, indent)
|
|
213
|
+
last = ((index + 1)...@lines.size).find { |i| @lines[i].match?(/\A#{indent}end\b/) }
|
|
214
|
+
return index unless last
|
|
215
|
+
|
|
216
|
+
only_logger = @lines[(index + 1)..last].all? { |line| blank?(line) || line.match?(LOG_TO_STDOUT_LINE) }
|
|
217
|
+
top_level?(index) && only_logger ? remove(index, last) : leave(index, last)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def setting(index)
|
|
221
|
+
last = statement_end(index)
|
|
222
|
+
statement = without_comments(@lines[index..last].join)
|
|
223
|
+
return leave(index, last) if !top_level?(index) || conditional?(statement)
|
|
224
|
+
|
|
225
|
+
if (option = statement.match(DEPRECATED_OPTION))
|
|
226
|
+
name = option[1].to_sym
|
|
227
|
+
value = option[2].strip
|
|
228
|
+
return leave(index, last) unless @convert && convertible?(name, value)
|
|
229
|
+
|
|
230
|
+
@result.options[name] = value
|
|
231
|
+
elsif statement.match?(ADD_APPENDER)
|
|
232
|
+
args = statement.match(APPENDER_ARGS)&.[](:args)
|
|
233
|
+
return leave(index, last) unless args && Arguments.new(args).movable?
|
|
234
|
+
|
|
235
|
+
@result.appenders << args.strip
|
|
236
|
+
end
|
|
237
|
+
remove(index, last)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# Leaves `config.colorize_logging` in place. One that does not plainly turn colors on or off
|
|
241
|
+
# everywhere, such as `= false if ENV["CI"]`, decides the log file's formatter only sometimes,
|
|
242
|
+
# which the formatter written in the appenders block cannot follow.
|
|
243
|
+
def colorize(index)
|
|
244
|
+
last = statement_end(index)
|
|
245
|
+
statement = without_comments(@lines[index..last].join)
|
|
246
|
+
literal = statement.match?(/\Aconfig\.colorize_logging\s*=\s*(?:true|false)\z/)
|
|
247
|
+
return last if top_level?(index) && !conditional?(statement) && literal
|
|
248
|
+
|
|
249
|
+
leave(index, last)
|
|
250
|
+
@result.unmoved << @result.left.last
|
|
251
|
+
last
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def remove(first, last)
|
|
255
|
+
(first..last).each { |i| @removed[i] = true }
|
|
256
|
+
last
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def leave(first, last)
|
|
260
|
+
setting = @lines[first..last].join.strip
|
|
261
|
+
@result.left << setting
|
|
262
|
+
@result.unmoved << setting if (first..last).any? { |index| appender_setting?(index) }
|
|
263
|
+
last
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Whether the line sets a deprecated appender option, or starts a statement that adds an appender
|
|
267
|
+
# that writes to the screen, either of which an appenders block would override or repeat.
|
|
268
|
+
def appender_setting?(index)
|
|
269
|
+
line = @lines[index]
|
|
270
|
+
return false if comment?(line)
|
|
271
|
+
return true if line.match?(DEPRECATED_OPTION)
|
|
272
|
+
|
|
273
|
+
line.match?(ADD_APPENDER) &&
|
|
274
|
+
self.class.screen_appender?(without_comments(@lines[index..statement_end(index)].join))
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# Whether the value of a deprecated option can be carried into the appenders block: a switch only
|
|
278
|
+
# when it is a literal true or false, and any value only when it names no local variable.
|
|
279
|
+
def convertible?(name, value)
|
|
280
|
+
return %w[true false].include?(value) if SWITCHES.include?(name)
|
|
281
|
+
|
|
282
|
+
Arguments.new(value).movable?
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# The last line of the statement that starts at the line: the first one at which the lines so far
|
|
286
|
+
# parse as Ruby, unless the next line continues it with a method call (`.tap`).
|
|
287
|
+
def statement_end(index)
|
|
288
|
+
(index...@lines.size).each do |last|
|
|
289
|
+
next unless Ripper.sexp(@lines[index..last].join)
|
|
290
|
+
return last unless @lines[last + 1]&.lstrip&.start_with?(".")
|
|
291
|
+
end
|
|
292
|
+
index
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Whether the statement has a condition of its own, such as `... if ENV["JSON"]`.
|
|
296
|
+
def conditional?(statement)
|
|
297
|
+
%i[if_mod unless_mod].include?(Ripper.sexp(statement)&.dig(1, 0, 0))
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# Whether the line is at the top level of the file's settings: as indented as the first line of
|
|
301
|
+
# code inside its `.configure do` block, or its `class Application < Rails::Application`.
|
|
302
|
+
def top_level?(index)
|
|
303
|
+
indentation(@lines[index]) <= top_indentation
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def top_indentation
|
|
307
|
+
@top_indentation ||= begin
|
|
308
|
+
opener = @lines.index { |line| line.match?(CONFIGURE) || line.match?(APPLICATION_CLASS) }
|
|
309
|
+
first = opener && @lines[(opener + 1)..].find { |line| !blank?(line) && !comment?(line) }
|
|
310
|
+
first ? indentation(first) : (@lines.grep(/\A\s*config\./).map { |line| indentation(line) }.min || 0)
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def indentation(line) = line[/\A[ \t]*/].size
|
|
315
|
+
|
|
316
|
+
# The source of a statement, without its comments.
|
|
317
|
+
def without_comments(source)
|
|
318
|
+
Ripper.lex(source).reject { |token| COMMENT_TOKENS.include?(token[1]) }.map { |token| token[2] }.join.strip
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# Removes the comment lines directly above each removed setting, such as "# Log to STDOUT by
|
|
322
|
+
# default" above Rails 7.2's `config.logger`, stopping at a blank line, at code, and at
|
|
323
|
+
# commented-out code, such as `# config.cache_store = :mem_cache_store`.
|
|
324
|
+
def remove_comments_above_removed
|
|
325
|
+
@lines.each_index do |i|
|
|
326
|
+
next unless @removed[i] && (i.zero? || !@removed[i - 1])
|
|
327
|
+
|
|
328
|
+
above = i - 1
|
|
329
|
+
while above >= 0 && comment?(@lines[above]) && !@lines[above].match?(COMMENTED_CODE)
|
|
330
|
+
@removed[above] = true
|
|
331
|
+
above -= 1
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
# The lines it keeps, with a :gap where each run of removed lines was.
|
|
337
|
+
def kept_lines
|
|
338
|
+
@lines.each_with_index.with_object([]) do |(line, i), kept|
|
|
339
|
+
if @removed[i]
|
|
340
|
+
kept << :gap unless kept.last == :gap
|
|
341
|
+
else
|
|
342
|
+
kept << line
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
def reword_rails_comments(lines)
|
|
348
|
+
lines.map do |line|
|
|
349
|
+
reworded = line.is_a?(String) && RAILS_COMMENTS[line.strip]
|
|
350
|
+
reworded ? line.sub(line.strip, reworded) : line
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# Drops a blank line that a removal left beside another blank line, the start of the file, or the
|
|
355
|
+
# start or end of a block, such as where the lines that followed
|
|
356
|
+
# `class Application < Rails::Application` were. Only the blank lines beside a gap: the rest of
|
|
357
|
+
# the file, its heredocs included, is unchanged.
|
|
358
|
+
def tidy_blank_lines(lines)
|
|
359
|
+
lines.each_with_index.with_object([]) do |(line, i), kept|
|
|
360
|
+
next if line == :gap
|
|
361
|
+
next if blank?(line) && i.positive? && lines[i - 1] == :gap && after_gap_redundant?(kept.last)
|
|
362
|
+
next if blank?(line) && lines[i + 1] == :gap && before_gap_redundant?(lines[i + 2])
|
|
363
|
+
|
|
364
|
+
kept << line
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
# Whether a blank line that follows a gap is redundant, given the line kept before the gap.
|
|
369
|
+
def after_gap_redundant?(previous)
|
|
370
|
+
previous.nil? || blank?(previous) || previous.match?(OPENS_BLOCK)
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# Whether a blank line that precedes a gap is redundant, given the line after the gap.
|
|
374
|
+
def before_gap_redundant?(following)
|
|
375
|
+
following.nil? || (following.is_a?(String) && (blank?(following) || following.match?(CLOSES_BLOCK)))
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def blank?(line) = line.strip.empty?
|
|
379
|
+
|
|
380
|
+
def comment?(line) = line.lstrip.start_with?("#")
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Where the logs go. Rails Semantic Logger creates exactly the appenders listed here, so change this
|
|
2
|
+
# block to send them elsewhere. Each line takes the arguments of SemanticLogger.add_appender (see
|
|
3
|
+
# https://logger.reidmorrison.com/appenders.html), and its method says when the appender is created:
|
|
4
|
+
# add: always
|
|
5
|
+
# add_server: only when serving requests: `rails server`, or Sidekiq in server mode
|
|
6
|
+
# add_console: only inside `rails console`
|
|
7
|
+
config.rails_semantic_logger.appenders do |appenders|
|
|
8
|
+
<%= line("appenders.add(#{file_arguments})", active: file?) %>
|
|
9
|
+
<% @appenders.each do |args| -%>
|
|
10
|
+
appenders.add(<%= args %>)
|
|
11
|
+
<% end -%>
|
|
12
|
+
<%= line("appenders.add_server(io: $stdout, formatter: :color)", active: screen?) %>
|
|
13
|
+
<%= line("appenders.add_console(io: $stderr, formatter: :color)", active: screen?) %>
|
|
14
|
+
<% if deployed? -%>
|
|
15
|
+
|
|
16
|
+
# Or, in a container (Docker, Kubernetes), replace the lines above with JSON on stdout, one line
|
|
17
|
+
# per entry, and no log file:
|
|
18
|
+
# appenders.add(io: $stdout, formatter: :json)
|
|
19
|
+
<% end -%>
|
|
20
|
+
end
|
|
21
|
+
|
|
@@ -185,6 +185,16 @@ module RailsSemanticLogger
|
|
|
185
185
|
end
|
|
186
186
|
end
|
|
187
187
|
|
|
188
|
+
# The appender options that `config.rails_semantic_logger.appenders` replaces, deprecated, and
|
|
189
|
+
# removed in v6. `bin/rails generate rails_semantic_logger:install` converts them into appenders.
|
|
190
|
+
DEPRECATED_APPENDER_OPTIONS = %i[format ap_options filter add_file_appender console_logger].freeze
|
|
191
|
+
|
|
192
|
+
# Whether `rails generate` (or `rails destroy`) is running, which loads the application's
|
|
193
|
+
# configuration to run a generator, such as rails_semantic_logger:install.
|
|
194
|
+
def self.generating?
|
|
195
|
+
defined?(::Rails::Command::GenerateCommand) || defined?(::Rails::Command::DestroyCommand) ? true : false
|
|
196
|
+
end
|
|
197
|
+
|
|
188
198
|
# Setup default values
|
|
189
199
|
def initialize
|
|
190
200
|
@semantic = true
|
|
@@ -315,10 +325,15 @@ module RailsSemanticLogger
|
|
|
315
325
|
|
|
316
326
|
private
|
|
317
327
|
|
|
328
|
+
# Not while `rails generate` runs: it loads the application's configuration, and the
|
|
329
|
+
# rails_semantic_logger:install generator that the warning names is what removes the option.
|
|
318
330
|
def deprecate_appender_option(option, via: "appenders.add(...)")
|
|
331
|
+
return if self.class.generating?
|
|
332
|
+
|
|
319
333
|
message =
|
|
320
334
|
"`config.rails_semantic_logger.#{option}=` is deprecated and will be removed in a future release. " \
|
|
321
|
-
"
|
|
335
|
+
"Run `bin/rails generate rails_semantic_logger:install`, which replaces it with the appenders it " \
|
|
336
|
+
"stands for, or declare the destination and formatting directly, via " \
|
|
322
337
|
"`config.rails_semantic_logger.appenders { |appenders| #{via} }`."
|
|
323
338
|
|
|
324
339
|
# Skip this method and the setter that called it, so the warning is attributed
|
|
@@ -20,7 +20,7 @@ module RailsSemanticLogger
|
|
|
20
20
|
|
|
21
21
|
def call(item, queue, &block)
|
|
22
22
|
klass = item["wrapped"] || item["class"]
|
|
23
|
-
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
|
|
23
|
+
logger = klass ? SemanticLogger[klass] : ::Sidekiq.logger
|
|
24
24
|
|
|
25
25
|
SemanticLogger.tagged(queue: queue) do
|
|
26
26
|
if perform_messages_enabled?
|
|
@@ -14,7 +14,30 @@ module RailsSemanticLogger
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def fail_many_claimed(event)
|
|
17
|
-
log_event(event, :warn, "Fail claimed jobs",
|
|
17
|
+
log_event(event, :warn, "Fail claimed jobs", exception: event.payload[:error],
|
|
18
|
+
**event.payload.slice(:job_ids, :process_ids))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Solid Queue 1.7+ batches.
|
|
22
|
+
# Solid Queue logs this event at info even when finishing the batch raised. Log that at error,
|
|
23
|
+
# with the exception, so a failed finish does not look like a successful one.
|
|
24
|
+
def finish_batch(event)
|
|
25
|
+
attributes = event.payload.slice(:batch_id, :total_jobs, :completed_jobs, :failed_jobs)
|
|
26
|
+
if (exception = event.payload[:exception_object])
|
|
27
|
+
log_event(event, :error, "Error finishing batch", exception: exception, **attributes)
|
|
28
|
+
else
|
|
29
|
+
log_event(event, :info, "Finish batch", **attributes)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def sweep_stalled_batches(event)
|
|
34
|
+
log_event(event, :debug, "Sweep stalled batches",
|
|
35
|
+
**event.payload.slice(:stale_executions, :finished_batches, :started_batches))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def batch_progress_error(event)
|
|
39
|
+
log_event(event, :error, "Error updating batch progress", exception: event.payload[:error],
|
|
40
|
+
**event.payload.slice(:batch_id, :job_id))
|
|
18
41
|
end
|
|
19
42
|
|
|
20
43
|
def release_claimed(event)
|
|
@@ -42,7 +65,8 @@ module RailsSemanticLogger
|
|
|
42
65
|
end
|
|
43
66
|
|
|
44
67
|
def release_blocked(event)
|
|
45
|
-
log_event(event, :debug, "Release blocked job",
|
|
68
|
+
log_event(event, :debug, "Release blocked job",
|
|
69
|
+
**event.payload.slice(:job_id, :concurrency_key, :released, :failed))
|
|
46
70
|
end
|
|
47
71
|
|
|
48
72
|
def enqueue_recurring_task(event)
|
|
@@ -153,6 +177,18 @@ module RailsSemanticLogger
|
|
|
153
177
|
end
|
|
154
178
|
end
|
|
155
179
|
|
|
180
|
+
# Solid Queue 1.5.1+.
|
|
181
|
+
def fork_boot_timeout(event)
|
|
182
|
+
process = event.payload[:process]
|
|
183
|
+
attributes = event.payload.slice(:pid).merge(hostname: process.hostname, name: process.name)
|
|
184
|
+
if (exception = event.payload[:exception_object])
|
|
185
|
+
log_event(event, :error, "Error terminating #{process.kind} that failed to boot in time",
|
|
186
|
+
exception: exception, **attributes)
|
|
187
|
+
else
|
|
188
|
+
log_event(event, :warn, "Terminate #{process.kind} that failed to boot in time", **attributes)
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
156
192
|
private
|
|
157
193
|
|
|
158
194
|
def process_attributes(process)
|
|
@@ -152,9 +152,7 @@ module RailsSemanticLogger
|
|
|
152
152
|
SemanticLogger.add_appender(io: $stdout, formatter: :color)
|
|
153
153
|
end
|
|
154
154
|
|
|
155
|
-
options.appenders.server.each
|
|
156
|
-
SemanticLogger.add_appender(**args, &block)
|
|
157
|
-
end
|
|
155
|
+
options.appenders.server.each { |args, block| add_declared_appender(args, &block) }
|
|
158
156
|
end
|
|
159
157
|
|
|
160
158
|
# Console (REPL) counterpart of .add_server_appenders, used by the `rails console`
|
|
@@ -168,9 +166,15 @@ module RailsSemanticLogger
|
|
|
168
166
|
SemanticLogger.add_appender(io: $stderr, formatter: :color)
|
|
169
167
|
end
|
|
170
168
|
|
|
171
|
-
options.appenders.console.each
|
|
172
|
-
|
|
173
|
-
|
|
169
|
+
options.appenders.console.each { |args, block| add_declared_appender(args, &block) }
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Returns [Hash|Symbol] the formatter of the default log file, log/<environment>.log, while the
|
|
173
|
+
# deprecated `format` and `ap_options` options are unset: in color, with single-line Amazing Print,
|
|
174
|
+
# or plain text when the application turned colorized logging off. The
|
|
175
|
+
# rails_semantic_logger:install generator writes it into the appenders it lists.
|
|
176
|
+
def self.default_file_formatter(colorize: true)
|
|
177
|
+
colorize ? {color: {ap: {multiline: false}}} : :default
|
|
174
178
|
end
|
|
175
179
|
|
|
176
180
|
# Create each appender declared via `config.rails_semantic_logger.appenders`.
|
|
@@ -179,12 +183,20 @@ module RailsSemanticLogger
|
|
|
179
183
|
def self.add_appenders(appenders)
|
|
180
184
|
internal_logger = nil
|
|
181
185
|
appenders.each do |args, block|
|
|
182
|
-
appender =
|
|
186
|
+
appender = add_declared_appender(args, &block)
|
|
183
187
|
internal_logger ||= appender if appender.is_a?(SemanticLogger::Appender::File)
|
|
184
188
|
end
|
|
185
189
|
SemanticLogger::Processor.logger = internal_logger if internal_logger
|
|
186
190
|
end
|
|
187
191
|
|
|
192
|
+
# Adds one declared appender, creating the directory of its log file, if any, as the engine does for
|
|
193
|
+
# the default log file, log/<environment>.log, since Semantic Logger's file appender does not.
|
|
194
|
+
def self.add_declared_appender(args, &)
|
|
195
|
+
FileUtils.mkdir_p(File.dirname(args[:file_name].to_s)) if args[:file_name]
|
|
196
|
+
SemanticLogger.add_appender(**args, &)
|
|
197
|
+
end
|
|
198
|
+
private_class_method :add_declared_appender
|
|
199
|
+
|
|
188
200
|
# Swap an existing subscriber with a new one
|
|
189
201
|
def self.swap_subscriber(old_class, new_class, notifier)
|
|
190
202
|
subscribers = ActiveSupport::LogSubscriber.subscribers.grep(old_class)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_semantic_logger
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.
|
|
4
|
+
version: 5.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Reid Morrison
|
|
@@ -41,14 +41,14 @@ dependencies:
|
|
|
41
41
|
name: semantic_logger
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
|
43
43
|
requirements:
|
|
44
|
-
- - "
|
|
44
|
+
- - "~>"
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
46
|
version: '5.3'
|
|
47
47
|
type: :runtime
|
|
48
48
|
prerelease: false
|
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
50
50
|
requirements:
|
|
51
|
-
- - "
|
|
51
|
+
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
53
|
version: '5.3'
|
|
54
54
|
executables: []
|
|
@@ -58,6 +58,11 @@ files:
|
|
|
58
58
|
- LICENSE.txt
|
|
59
59
|
- README.md
|
|
60
60
|
- Rakefile
|
|
61
|
+
- lib/generators/rails_semantic_logger/install/appenders_block.rb
|
|
62
|
+
- lib/generators/rails_semantic_logger/install/arguments.rb
|
|
63
|
+
- lib/generators/rails_semantic_logger/install/install_generator.rb
|
|
64
|
+
- lib/generators/rails_semantic_logger/install/logger_settings.rb
|
|
65
|
+
- lib/generators/rails_semantic_logger/install/templates/appenders.rb.erb
|
|
61
66
|
- lib/rails_semantic_logger.rb
|
|
62
67
|
- lib/rails_semantic_logger/action_controller/log_subscriber.rb
|
|
63
68
|
- lib/rails_semantic_logger/action_mailer/log_subscriber.rb
|
|
@@ -90,7 +95,7 @@ licenses:
|
|
|
90
95
|
metadata:
|
|
91
96
|
bug_tracker_uri: https://github.com/reidmorrison/rails_semantic_logger/issues
|
|
92
97
|
documentation_uri: https://logger.reidmorrison.com
|
|
93
|
-
source_code_uri: https://github.com/reidmorrison/rails_semantic_logger/tree/v5.
|
|
98
|
+
source_code_uri: https://github.com/reidmorrison/rails_semantic_logger/tree/v5.4.0
|
|
94
99
|
changelog_uri: https://github.com/reidmorrison/rails_semantic_logger/blob/main/CHANGELOG.md
|
|
95
100
|
rubygems_mfa_required: 'true'
|
|
96
101
|
rdoc_options: []
|