bluepill 0.2.9 → 0.3.2
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 +111 -2
- data/README.md +16 -13
- data/bin/bluepill +21 -8
- data/bluepill.gemspec +7 -2
- data/lib/bluepill/application/lifecycle.rb +48 -35
- data/lib/bluepill/application/listener.rb +2 -4
- data/lib/bluepill/application/previous_daemon.rb +12 -14
- data/lib/bluepill/application.rb +36 -30
- data/lib/bluepill/base_directory.rb +18 -20
- data/lib/bluepill/check_registry.rb +104 -0
- data/lib/bluepill/clock.rb +1 -3
- data/lib/bluepill/condition_watch.rb +46 -54
- data/lib/bluepill/controller/directory.rb +1 -3
- data/lib/bluepill/controller.rb +35 -37
- data/lib/bluepill/daemonize.rb +52 -0
- data/lib/bluepill/dsl/app_proxy.rb +68 -64
- data/lib/bluepill/dsl/attributes.rb +135 -0
- data/lib/bluepill/dsl/process_factory/child_factory.rb +83 -0
- data/lib/bluepill/dsl/process_factory/registry.rb +4 -26
- data/lib/bluepill/dsl/process_factory.rb +85 -84
- data/lib/bluepill/dsl/process_proxy.rb +80 -160
- data/lib/bluepill/dsl.rb +16 -15
- data/lib/bluepill/errors.rb +190 -0
- data/lib/bluepill/group.rb +14 -37
- data/lib/bluepill/logger.rb +65 -99
- data/lib/bluepill/process/attributes.rb +66 -32
- data/lib/bluepill/process/children.rb +6 -7
- data/lib/bluepill/process/pid.rb +43 -35
- data/lib/bluepill/process/state_machine.rb +15 -59
- data/lib/bluepill/process/stop_signals.rb +123 -0
- data/lib/bluepill/process/transition.rb +1 -3
- data/lib/bluepill/process/transition_callbacks.rb +2 -16
- data/lib/bluepill/process/triggers.rb +90 -0
- data/lib/bluepill/process/user_commands.rb +1 -1
- data/lib/bluepill/process/watches.rb +9 -23
- data/lib/bluepill/process.rb +55 -52
- data/lib/bluepill/process_commands/execution.rb +16 -24
- data/lib/bluepill/process_commands/hooks.rb +1 -31
- data/lib/bluepill/process_commands/stop_signals.rb +12 -17
- data/lib/bluepill/process_commands.rb +10 -8
- data/lib/bluepill/process_conditions/always_true.rb +2 -6
- data/lib/bluepill/process_conditions/below_threshold.rb +6 -8
- data/lib/bluepill/process_conditions/cpu_usage.rb +1 -4
- data/lib/bluepill/process_conditions/file_time.rb +8 -9
- data/lib/bluepill/process_conditions/http.rb +29 -67
- data/lib/bluepill/process_conditions/mem_usage.rb +7 -45
- data/lib/bluepill/process_conditions/process_condition.rb +29 -47
- data/lib/bluepill/process_conditions/zombie_process.rb +2 -6
- data/lib/bluepill/process_conditions.rb +5 -14
- data/lib/bluepill/process_journal/cleanup.rb +5 -53
- data/lib/bluepill/process_journal/config.rb +73 -0
- data/lib/bluepill/process_journal/entries.rb +128 -0
- data/lib/bluepill/process_journal/journal.rb +69 -0
- data/lib/bluepill/process_journal/kind.rb +60 -0
- data/lib/bluepill/process_journal/lock.rb +9 -4
- data/lib/bluepill/process_journal.rb +13 -324
- data/lib/bluepill/process_statistics.rb +3 -7
- data/lib/bluepill/socket.rb +19 -17
- data/lib/bluepill/system/command_options.rb +136 -0
- data/lib/bluepill/system/command_result.rb +73 -0
- data/lib/bluepill/system/command_setup.rb +3 -4
- data/lib/bluepill/system/command_spawn.rb +244 -0
- data/lib/bluepill/system/commands.rb +27 -40
- data/lib/bluepill/system/daemon.rb +97 -15
- data/lib/bluepill/system/files.rb +35 -0
- data/lib/bluepill/system/ps.rb +20 -17
- data/lib/bluepill/system/ps_table.rb +7 -9
- data/lib/bluepill/system/usage.rb +175 -0
- data/lib/bluepill/system.rb +7 -192
- data/lib/bluepill/trigger.rb +18 -69
- data/lib/bluepill/triggers/flapping.rb +12 -8
- data/lib/bluepill/units.rb +7 -21
- data/lib/bluepill/version.rb +16 -26
- data/lib/bluepill.rb +1 -1
- data/sig/bluepill/application.rbs +11 -12
- data/sig/bluepill/attributes.rbs +48 -0
- data/sig/bluepill/checks.rbs +40 -40
- data/sig/bluepill/dsl.rbs +100 -59
- data/sig/bluepill/errors.rbs +48 -0
- data/sig/bluepill/logger.rbs +15 -31
- data/sig/bluepill/process.rbs +39 -305
- data/sig/bluepill/process_commands.rbs +235 -0
- data/sig/bluepill/stop_signals.rbs +21 -0
- data/sig/bluepill/system.rbs +191 -101
- data/sig/bluepill/version.rbs +17 -0
- data/sig/bluepill.rbs +2 -17
- data/sig/manifest.yaml +1 -0
- metadata +22 -8
- data/lib/bluepill/deprecation/custom_checks.rb +0 -193
- data/lib/bluepill/deprecation/dsl.rb +0 -141
- data/lib/bluepill/deprecation/options.rb +0 -140
- data/lib/bluepill/deprecation.rb +0 -92
- data/lib/bluepill/dsl/process_factory/children.rb +0 -99
- data/sig/bluepill/deprecation.rbs +0 -87
data/lib/bluepill/dsl.rb
CHANGED
|
@@ -5,7 +5,11 @@ module Bluepill
|
|
|
5
5
|
#
|
|
6
6
|
# The block describes the application's processes. It runs with self set
|
|
7
7
|
# to an {AppProxy}, and also gets the proxy as an argument. Bluepill then
|
|
8
|
-
# loads the application, which starts its daemon.
|
|
8
|
+
# loads the application, which starts its daemon. A {Bluepill::Error},
|
|
9
|
+
# such as a {ConfigError} for a mistake in the config, prints its message
|
|
10
|
+
# and exits with its exit_status, which is 6 for a ConfigError. A
|
|
11
|
+
# ConfigError's message starts with the pill file's line, as in
|
|
12
|
+
# "app.pill.rb:5: Unknown attribute ...".
|
|
9
13
|
#
|
|
10
14
|
# @example
|
|
11
15
|
# Bluepill.application(:my_app, log_file: '/var/log/bluepill.log') do |app|
|
|
@@ -16,10 +20,8 @@ module Bluepill
|
|
|
16
20
|
# end
|
|
17
21
|
#
|
|
18
22
|
# @param [#to_s] app_name the application's name
|
|
19
|
-
# @param [Hash{Symbol => Object}
|
|
20
|
-
# as
|
|
21
|
-
# @param [Hash{Symbol => Object}] keywords the application's options,
|
|
22
|
-
# such as :log_file, :foreground and :base_dir
|
|
23
|
+
# @param [Hash{Symbol => Object}] options the application's options, such
|
|
24
|
+
# as log_file:, foreground: and base_dir:
|
|
23
25
|
#
|
|
24
26
|
# @yield [app] describes the application's processes
|
|
25
27
|
# @yieldparam [AppProxy] app the application's proxy, which is also self
|
|
@@ -27,17 +29,16 @@ module Bluepill
|
|
|
27
29
|
#
|
|
28
30
|
# @return [void]
|
|
29
31
|
#
|
|
30
|
-
# @raise [ArgumentError] for options as a Hash and as keywords
|
|
31
|
-
#
|
|
32
32
|
# @api public
|
|
33
|
-
def self.application(app_name, options
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
Deprecation.check_application_options(options)
|
|
39
|
-
# instance_eval also passes the proxy to a block that takes an argument
|
|
40
|
-
app_proxy.instance_eval(&block)
|
|
33
|
+
def self.application(app_name, **options, &)
|
|
34
|
+
app_proxy = AppProxy.new(app_name, **options)
|
|
35
|
+
# evaluate also passes the proxy to a block that takes an argument, and
|
|
36
|
+
# turns a misspelled attribute into a ConfigError
|
|
37
|
+
app_proxy.evaluate(&)
|
|
41
38
|
app_proxy.app.load
|
|
39
|
+
rescue Error => e
|
|
40
|
+
location = e.location if e.is_a?(ConfigError)
|
|
41
|
+
$stderr.puts(location ? "#{location}: #{e}" : e)
|
|
42
|
+
exit(e.exit_status)
|
|
42
43
|
end
|
|
43
44
|
end
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bluepill
|
|
4
|
+
# An error that stops the bluepill command, with the status it exits with
|
|
5
|
+
#
|
|
6
|
+
# The library raises these, and bin/bluepill and {Bluepill.application}
|
|
7
|
+
# rescue them, print the message, and exit with {#exit_status}.
|
|
8
|
+
#
|
|
9
|
+
# @api public
|
|
10
|
+
class Error < StandardError
|
|
11
|
+
# Return the status that the bluepill command exits with
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# Bluepill::Error.new.exit_status # => 1
|
|
15
|
+
#
|
|
16
|
+
# @return [Integer] 1, unless a subclass says otherwise
|
|
17
|
+
#
|
|
18
|
+
# @api public
|
|
19
|
+
def exit_status = 1
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# A mistake in a pill file, such as a misspelled process attribute or two
|
|
23
|
+
# processes with the same pid file
|
|
24
|
+
#
|
|
25
|
+
# Bluepill.application prints its {#location} before its message.
|
|
26
|
+
#
|
|
27
|
+
# @api public
|
|
28
|
+
class ConfigError < Error
|
|
29
|
+
# The real path of bluepill's lib directory, whose frames in a backtrace
|
|
30
|
+
# aren't the pill file's
|
|
31
|
+
LIBRARY_DIR = "#{File.realpath("../..", __FILE__)}/"
|
|
32
|
+
|
|
33
|
+
# Return the status that the bluepill command exits with
|
|
34
|
+
#
|
|
35
|
+
# @example
|
|
36
|
+
# Bluepill::ConfigError.new.exit_status # => 6
|
|
37
|
+
#
|
|
38
|
+
# @return [Integer] 6
|
|
39
|
+
#
|
|
40
|
+
# @api public
|
|
41
|
+
def exit_status = 6
|
|
42
|
+
|
|
43
|
+
# Return the line of the pill file that the error comes from
|
|
44
|
+
#
|
|
45
|
+
# It's the first frame of the backtrace in a file outside {LIBRARY_DIR},
|
|
46
|
+
# so the pill file's line wherever bluepill raised the error, or a line
|
|
47
|
+
# of a file that the pill file loads. An error raised in place of
|
|
48
|
+
# another, such as the DSL's for a misspelled attribute, comes from where
|
|
49
|
+
# that other error, its cause, was raised, so it looks there first.
|
|
50
|
+
#
|
|
51
|
+
# @example
|
|
52
|
+
# error.location # => "/etc/bluepill/app.pill.rb:5"
|
|
53
|
+
#
|
|
54
|
+
# @return [String, nil] the file and line, or nil for an error that
|
|
55
|
+
# wasn't raised from a file
|
|
56
|
+
#
|
|
57
|
+
# @api public
|
|
58
|
+
def location
|
|
59
|
+
frames = cause&.backtrace_locations || backtrace_locations
|
|
60
|
+
frame = Array(frames).find { |candidate| outside_library?(candidate) }
|
|
61
|
+
"#{frame.path}:#{frame.lineno}" if frame
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
# Return whether a frame is in a file outside {LIBRARY_DIR}
|
|
67
|
+
#
|
|
68
|
+
# A frame of eval'd code, or of Ruby's own internals, such as
|
|
69
|
+
# <internal:kernel> or JRuby's Java classes, isn't in a file. Real paths
|
|
70
|
+
# are compared, so a symlink to bluepill's lib is still its lib.
|
|
71
|
+
#
|
|
72
|
+
# @param [Thread::Backtrace::Location] frame the frame
|
|
73
|
+
#
|
|
74
|
+
# @return [Boolean]
|
|
75
|
+
#
|
|
76
|
+
# @api private
|
|
77
|
+
def outside_library?(frame)
|
|
78
|
+
# Ruby gives every frame a path
|
|
79
|
+
path = frame.path #: String
|
|
80
|
+
File.file?(path) && !File.realpath(path).start_with?(LIBRARY_DIR)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# The daemon couldn't start, such as when it can't write its log file
|
|
85
|
+
#
|
|
86
|
+
# @api public
|
|
87
|
+
class StartupError < Error
|
|
88
|
+
# Return the status that the bluepill command exits with
|
|
89
|
+
#
|
|
90
|
+
# @example
|
|
91
|
+
# Bluepill::StartupError.new.exit_status # => 5
|
|
92
|
+
#
|
|
93
|
+
# @return [Integer] 5
|
|
94
|
+
#
|
|
95
|
+
# @api public
|
|
96
|
+
def exit_status = 5
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# A previous daemon for the application couldn't be stopped
|
|
100
|
+
#
|
|
101
|
+
# @api public
|
|
102
|
+
class PreviousDaemonError < Error
|
|
103
|
+
# Return the status that the bluepill command exits with
|
|
104
|
+
#
|
|
105
|
+
# @example
|
|
106
|
+
# Bluepill::PreviousDaemonError.new.exit_status # => 4
|
|
107
|
+
#
|
|
108
|
+
# @return [Integer] 4
|
|
109
|
+
#
|
|
110
|
+
# @api public
|
|
111
|
+
def exit_status = 4
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# A daemon for the application already answers on its socket
|
|
115
|
+
#
|
|
116
|
+
# @api public
|
|
117
|
+
class AlreadyRunningError < Error
|
|
118
|
+
# Return the status that the bluepill command exits with
|
|
119
|
+
#
|
|
120
|
+
# @example
|
|
121
|
+
# Bluepill::AlreadyRunningError.new.exit_status # => 7
|
|
122
|
+
#
|
|
123
|
+
# @return [Integer] 7
|
|
124
|
+
#
|
|
125
|
+
# @api public
|
|
126
|
+
def exit_status = 7
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# The CLI couldn't reach the daemon, or it didn't answer in time
|
|
130
|
+
#
|
|
131
|
+
# @api public
|
|
132
|
+
class ConnectionError < Error
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# The daemon runs another version of bluepill than the CLI
|
|
136
|
+
#
|
|
137
|
+
# @api public
|
|
138
|
+
class VersionMismatchError < Error
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# The CLI was given a command it doesn't know
|
|
142
|
+
#
|
|
143
|
+
# @api public
|
|
144
|
+
class UnknownCommandError < Error
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# The daemon answered a command with an error that it raised
|
|
148
|
+
#
|
|
149
|
+
# @api public
|
|
150
|
+
class DaemonError < Error
|
|
151
|
+
# The error that the daemon raised, unmarshaled
|
|
152
|
+
#
|
|
153
|
+
# @example
|
|
154
|
+
# Bluepill::DaemonError.new(RuntimeError.new("boom")).daemon_error
|
|
155
|
+
# # => #<RuntimeError: boom>
|
|
156
|
+
#
|
|
157
|
+
# @return [Exception]
|
|
158
|
+
#
|
|
159
|
+
# @api public
|
|
160
|
+
attr_reader :daemon_error
|
|
161
|
+
|
|
162
|
+
# Wrap an error that the daemon raised
|
|
163
|
+
#
|
|
164
|
+
# The message has the daemon's error and its backtrace.
|
|
165
|
+
#
|
|
166
|
+
# @example
|
|
167
|
+
# Bluepill::DaemonError.new(RuntimeError.new("boom")).message
|
|
168
|
+
# # => "Received error from server:\n#<RuntimeError: boom>"
|
|
169
|
+
#
|
|
170
|
+
# @param [Exception] daemon_error the error
|
|
171
|
+
#
|
|
172
|
+
# @return [void]
|
|
173
|
+
#
|
|
174
|
+
# @api public
|
|
175
|
+
def initialize(daemon_error)
|
|
176
|
+
@daemon_error = daemon_error
|
|
177
|
+
super(["Received error from server:", daemon_error.inspect, *daemon_error.backtrace].join("\n"))
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Return the status that the bluepill command exits with
|
|
181
|
+
#
|
|
182
|
+
# @example
|
|
183
|
+
# Bluepill::DaemonError.new(RuntimeError.new("boom")).exit_status # => 8
|
|
184
|
+
#
|
|
185
|
+
# @return [Integer] 8
|
|
186
|
+
#
|
|
187
|
+
# @api public
|
|
188
|
+
def exit_status = 8
|
|
189
|
+
end
|
|
190
|
+
end
|
data/lib/bluepill/group.rb
CHANGED
|
@@ -32,13 +32,6 @@ module Bluepill
|
|
|
32
32
|
# @api private
|
|
33
33
|
attr_accessor :logger
|
|
34
34
|
|
|
35
|
-
# Return a logger for processes, which bluepill doesn't use
|
|
36
|
-
#
|
|
37
|
-
# @return [Object] whatever was set
|
|
38
|
-
#
|
|
39
|
-
# @api private
|
|
40
|
-
attr_accessor :process_logger
|
|
41
|
-
|
|
42
35
|
# The setting that says whether each command runs on a process without
|
|
43
36
|
# waiting for it
|
|
44
37
|
NOBLOCK = {
|
|
@@ -52,39 +45,35 @@ module Bluepill
|
|
|
52
45
|
#
|
|
53
46
|
# @param [String, nil] name the group's name, or nil for the nameless
|
|
54
47
|
# group
|
|
55
|
-
# @param [
|
|
56
|
-
# :logger
|
|
48
|
+
# @param [Logger, nil] logger the group's logger
|
|
57
49
|
#
|
|
58
50
|
# @return [void]
|
|
59
51
|
#
|
|
60
52
|
# @api private
|
|
61
|
-
def initialize(name,
|
|
53
|
+
def initialize(name, logger: nil)
|
|
62
54
|
self.name = name
|
|
63
55
|
self.processes = []
|
|
64
|
-
logger = options[:logger] #: Logger
|
|
65
56
|
self.logger = logger
|
|
66
57
|
end
|
|
67
58
|
|
|
68
|
-
# Add a process
|
|
59
|
+
# Add a process
|
|
60
|
+
#
|
|
61
|
+
# The process has its logger already, made when the process was, with
|
|
62
|
+
# the group's name and its own.
|
|
69
63
|
#
|
|
70
64
|
# @param [Process] process the process
|
|
71
65
|
#
|
|
72
66
|
# @return [Array<Process>] the group's processes
|
|
73
67
|
#
|
|
74
68
|
# @api private
|
|
75
|
-
def add_process(process)
|
|
76
|
-
process.logger = logger.prefix_with(process.name)
|
|
77
|
-
processes << process
|
|
78
|
-
end
|
|
69
|
+
def add_process(process) = processes << process
|
|
79
70
|
|
|
80
71
|
# Tick every process in the group
|
|
81
72
|
#
|
|
82
73
|
# @return [void]
|
|
83
74
|
#
|
|
84
75
|
# @api private
|
|
85
|
-
def tick
|
|
86
|
-
processes.each(&:tick)
|
|
87
|
-
end
|
|
76
|
+
def tick = processes.each(&:tick)
|
|
88
77
|
|
|
89
78
|
# Set each process's state from whether it's running
|
|
90
79
|
#
|
|
@@ -93,9 +82,7 @@ module Bluepill
|
|
|
93
82
|
# @return [void]
|
|
94
83
|
#
|
|
95
84
|
# @api private
|
|
96
|
-
def determine_initial_state
|
|
97
|
-
processes.each(&:determine_initial_state)
|
|
98
|
-
end
|
|
85
|
+
def determine_initial_state = processes.each(&:determine_initial_state)
|
|
99
86
|
|
|
100
87
|
# Start the group's processes, or one of them
|
|
101
88
|
#
|
|
@@ -109,9 +96,7 @@ module Bluepill
|
|
|
109
96
|
# @return [Array<String>] the processes it started, as group:process
|
|
110
97
|
#
|
|
111
98
|
# @api private
|
|
112
|
-
def start(process_name = nil)
|
|
113
|
-
send_user_command(:start, process_name)
|
|
114
|
-
end
|
|
99
|
+
def start(process_name = nil) = send_user_command(:start, process_name)
|
|
115
100
|
|
|
116
101
|
# Stop monitoring the group's processes, or one of them
|
|
117
102
|
#
|
|
@@ -125,9 +110,7 @@ module Bluepill
|
|
|
125
110
|
# @return [Array<String>] the processes it unmonitored, as group:process
|
|
126
111
|
#
|
|
127
112
|
# @api private
|
|
128
|
-
def unmonitor(process_name = nil)
|
|
129
|
-
send_user_command(:unmonitor, process_name)
|
|
130
|
-
end
|
|
113
|
+
def unmonitor(process_name = nil) = send_user_command(:unmonitor, process_name)
|
|
131
114
|
|
|
132
115
|
# Stop the group's processes, or one of them
|
|
133
116
|
#
|
|
@@ -141,9 +124,7 @@ module Bluepill
|
|
|
141
124
|
# @return [Array<String>] the processes it stopped, as group:process
|
|
142
125
|
#
|
|
143
126
|
# @api private
|
|
144
|
-
def stop(process_name = nil)
|
|
145
|
-
send_user_command(:stop, process_name)
|
|
146
|
-
end
|
|
127
|
+
def stop(process_name = nil) = send_user_command(:stop, process_name)
|
|
147
128
|
|
|
148
129
|
# Restart the group's processes, or one of them
|
|
149
130
|
#
|
|
@@ -157,9 +138,7 @@ module Bluepill
|
|
|
157
138
|
# @return [Array<String>] the processes it restarted, as group:process
|
|
158
139
|
#
|
|
159
140
|
# @api private
|
|
160
|
-
def restart(process_name = nil)
|
|
161
|
-
send_user_command(:restart, process_name)
|
|
162
|
-
end
|
|
141
|
+
def restart(process_name = nil) = send_user_command(:restart, process_name)
|
|
163
142
|
|
|
164
143
|
# Return the status lines that bluepill status shows
|
|
165
144
|
#
|
|
@@ -247,9 +226,7 @@ module Bluepill
|
|
|
247
226
|
# @return [Boolean]
|
|
248
227
|
#
|
|
249
228
|
# @api private
|
|
250
|
-
def noblock?(process, event)
|
|
251
|
-
process.public_send(NOBLOCK.fetch(event))
|
|
252
|
-
end
|
|
229
|
+
def noblock?(process, event) = process.public_send(NOBLOCK.fetch(event))
|
|
253
230
|
|
|
254
231
|
# Return a process's status line
|
|
255
232
|
#
|
data/lib/bluepill/logger.rb
CHANGED
|
@@ -16,85 +16,83 @@ module Bluepill
|
|
|
16
16
|
# @api private
|
|
17
17
|
LOG_METHODS = %i[emerg alert crit err warning notice info debug].freeze
|
|
18
18
|
|
|
19
|
-
# Make a logger
|
|
19
|
+
# Make a logger without prefixes
|
|
20
20
|
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
21
|
+
# prefix_with makes the loggers that add prefixes.
|
|
22
|
+
#
|
|
23
|
+
# @param [Object, nil] logger the backend to write to, which answers to
|
|
24
|
+
# the {LOG_METHODS}, or nil for the log file or syslog
|
|
25
|
+
# @param [Boolean] stdout whether to echo messages to stdout too
|
|
26
|
+
# @param [String, nil] log_file the file to write to instead of syslog
|
|
27
|
+
# @param [String] identity the name syslog shows
|
|
28
|
+
# @param [Symbol, String] log_level the least severe priority to log
|
|
29
|
+
# (debug, so everything, by default)
|
|
26
30
|
#
|
|
27
31
|
# @return [void]
|
|
28
32
|
#
|
|
29
|
-
# @raise [ArgumentError] if
|
|
33
|
+
# @raise [ArgumentError] if log_level isn't one of {LOG_METHODS}
|
|
30
34
|
#
|
|
31
35
|
# @api private
|
|
32
|
-
def initialize(
|
|
33
|
-
@
|
|
34
|
-
@
|
|
35
|
-
@
|
|
36
|
+
def initialize(logger: nil, stdout: false, log_file: nil, identity: "bluepilld", log_level: :debug)
|
|
37
|
+
@log_file = log_file
|
|
38
|
+
@identity = identity
|
|
39
|
+
@level = priority(log_level)
|
|
40
|
+
@stdout = stdout
|
|
41
|
+
@names = [] #: Array[String?]
|
|
42
|
+
@label = nil
|
|
43
|
+
# A box, not the backend itself, so that reopen can swap the backend
|
|
44
|
+
# for this logger and every logger that prefix_with makes from it
|
|
45
|
+
@sink = [logger || create_logger]
|
|
36
46
|
@prefixes = {}
|
|
37
|
-
# Set here, and not in settings_from, since Ruby makes room in a new
|
|
38
|
-
# Logger for the instance variables that initialize sets
|
|
39
|
-
@level, @names, @stdout, @sink = settings_from(@logger, priority(options[:log_level] || :debug))
|
|
40
|
-
@label = label(@names)
|
|
41
47
|
end
|
|
42
48
|
|
|
43
|
-
# @!method emerg(msg
|
|
49
|
+
# @!method emerg(msg)
|
|
44
50
|
# Log a message at the emergency priority
|
|
45
51
|
# @param [String] msg the message
|
|
46
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
47
52
|
# @return [void]
|
|
48
53
|
# @api private
|
|
49
|
-
# @!method alert(msg
|
|
54
|
+
# @!method alert(msg)
|
|
50
55
|
# Log a message at the alert priority
|
|
51
56
|
# @param [String] msg the message
|
|
52
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
53
57
|
# @return [void]
|
|
54
58
|
# @api private
|
|
55
|
-
# @!method crit(msg
|
|
59
|
+
# @!method crit(msg)
|
|
56
60
|
# Log a message at the critical priority
|
|
57
61
|
# @param [String] msg the message
|
|
58
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
59
62
|
# @return [void]
|
|
60
63
|
# @api private
|
|
61
|
-
# @!method err(msg
|
|
64
|
+
# @!method err(msg)
|
|
62
65
|
# Log a message at the error priority
|
|
63
66
|
# @param [String] msg the message
|
|
64
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
65
67
|
# @return [void]
|
|
66
68
|
# @api private
|
|
67
|
-
# @!method warning(msg
|
|
69
|
+
# @!method warning(msg)
|
|
68
70
|
# Log a message at the warning priority
|
|
69
71
|
# @param [String] msg the message
|
|
70
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
71
72
|
# @return [void]
|
|
72
73
|
# @api private
|
|
73
|
-
# @!method notice(msg
|
|
74
|
+
# @!method notice(msg)
|
|
74
75
|
# Log a message at the notice priority
|
|
75
76
|
# @param [String] msg the message
|
|
76
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
77
77
|
# @return [void]
|
|
78
78
|
# @api private
|
|
79
|
-
# @!method info(msg
|
|
79
|
+
# @!method info(msg)
|
|
80
80
|
# Log a message at the info priority
|
|
81
81
|
# @param [String] msg the message
|
|
82
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
83
82
|
# @return [void]
|
|
84
83
|
# @api private
|
|
85
|
-
# @!method debug(msg
|
|
84
|
+
# @!method debug(msg)
|
|
86
85
|
# Log a message at the debug priority
|
|
87
86
|
# @param [String] msg the message
|
|
88
|
-
# @param [Array<String, nil>, nil] prefix more prefixes, after this logger's
|
|
89
87
|
# @return [void]
|
|
90
88
|
# @api private
|
|
91
89
|
LOG_METHODS.each_with_index do |method, priority|
|
|
92
90
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
93
|
-
def #{method}(msg
|
|
91
|
+
def #{method}(msg) # def emerg(msg)
|
|
94
92
|
# Below the log level, so dropped before anything is formatted
|
|
95
93
|
return if #{priority} > @level # return if 0 > @level
|
|
96
94
|
|
|
97
|
-
line = "\#{
|
|
95
|
+
line = "\#{@label}\#{msg}"
|
|
98
96
|
if @stdout
|
|
99
97
|
$stdout.puts("[#{method}]: \#{line}") # $stdout.puts("[emerg]: ...")
|
|
100
98
|
$stdout.flush
|
|
@@ -106,86 +104,45 @@ module Bluepill
|
|
|
106
104
|
|
|
107
105
|
# Return a logger that adds a prefix to this one's messages
|
|
108
106
|
#
|
|
109
|
-
# It's
|
|
107
|
+
# It's a copy of this logger, so it has the same level and echoes to
|
|
108
|
+
# stdout if this one does, and it shares the sink. It's made once for
|
|
109
|
+
# each prefix, and kept.
|
|
110
110
|
#
|
|
111
|
-
# @param [String] prefix the prefix, such as a process's name
|
|
111
|
+
# @param [String, nil] prefix the prefix, such as a process's name, or
|
|
112
|
+
# nil for none
|
|
112
113
|
#
|
|
113
114
|
# @return [Logger] the prefixed logger
|
|
114
115
|
#
|
|
115
116
|
# @api private
|
|
116
|
-
def prefix_with(prefix)
|
|
117
|
-
@prefixes[prefix] ||= self.class.new(logger: self, prefix: prefix)
|
|
118
|
-
end
|
|
117
|
+
def prefix_with(prefix) = @prefixes[prefix] ||= dup.add_prefix(prefix)
|
|
119
118
|
|
|
120
119
|
# Open the log file or syslog again, such as after log rotation
|
|
121
120
|
#
|
|
122
|
-
#
|
|
123
|
-
#
|
|
124
|
-
# @api private
|
|
125
|
-
def reopen
|
|
126
|
-
parent = @logger
|
|
127
|
-
if parent.instance_of?(Logger)
|
|
128
|
-
parent.reopen
|
|
129
|
-
else
|
|
130
|
-
@sink.replace([create_logger])
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
# The position in {LOG_METHODS} of the least severe priority to log
|
|
135
|
-
#
|
|
136
|
-
# @return [Integer]
|
|
137
|
-
#
|
|
138
|
-
# @api private
|
|
139
|
-
attr_reader :level
|
|
140
|
-
|
|
141
|
-
# Whether messages are echoed to stdout too
|
|
142
|
-
#
|
|
143
|
-
# @return [Boolean, nil]
|
|
144
|
-
#
|
|
145
|
-
# @api private
|
|
146
|
-
attr_reader :stdout
|
|
147
|
-
|
|
148
|
-
# Where the messages go, which every logger made from this one shares
|
|
149
|
-
#
|
|
150
|
-
# It holds one LoggerAdapter, Syslog, or given logger, and reopen
|
|
151
|
-
# replaces it.
|
|
152
|
-
#
|
|
153
|
-
# @return [Array<Object>]
|
|
154
|
-
#
|
|
155
|
-
# @api private
|
|
156
|
-
attr_reader :sink
|
|
157
|
-
|
|
158
|
-
# This logger's prefixes, its parents' first
|
|
121
|
+
# Every logger made from the same one shares the sink, so they all
|
|
122
|
+
# write to the new one.
|
|
159
123
|
#
|
|
160
|
-
# @return [
|
|
124
|
+
# @return [void]
|
|
161
125
|
#
|
|
162
126
|
# @api private
|
|
163
|
-
|
|
127
|
+
def reopen = @sink.replace([create_logger])
|
|
164
128
|
|
|
165
129
|
protected
|
|
166
130
|
|
|
167
|
-
#
|
|
131
|
+
# Add a prefix to a copy of a logger, which prefix_with made
|
|
168
132
|
#
|
|
169
|
-
#
|
|
170
|
-
#
|
|
171
|
-
# else writes to that, without prefixes.
|
|
133
|
+
# The copy gets its own prefixes and prefixed loggers, so the logger it
|
|
134
|
+
# copies keeps its own.
|
|
172
135
|
#
|
|
173
|
-
# @param [
|
|
174
|
-
# option, or the log file or syslog without one
|
|
175
|
-
# @param [Integer] level the position in {LOG_METHODS} of the least
|
|
176
|
-
# severe priority the :log_level option allows
|
|
136
|
+
# @param [String, nil] prefix the prefix, or nil for none
|
|
177
137
|
#
|
|
178
|
-
# @return [
|
|
179
|
-
# the level, the prefixes, whether to echo to stdout, and the sink
|
|
138
|
+
# @return [Logger] the copy
|
|
180
139
|
#
|
|
181
140
|
# @api private
|
|
182
|
-
def
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
[level, names, @options[:stdout], [parent]]
|
|
188
|
-
end
|
|
141
|
+
def add_prefix(prefix)
|
|
142
|
+
@names += [prefix]
|
|
143
|
+
@label = label(@names)
|
|
144
|
+
@prefixes = {}
|
|
145
|
+
self
|
|
189
146
|
end
|
|
190
147
|
|
|
191
148
|
# Return the prefixes as they start a message, as in "[app:web] "
|
|
@@ -223,12 +180,13 @@ module Bluepill
|
|
|
223
180
|
#
|
|
224
181
|
# @api private
|
|
225
182
|
def create_logger
|
|
226
|
-
|
|
227
|
-
|
|
183
|
+
log_file = @log_file
|
|
184
|
+
if log_file
|
|
185
|
+
LoggerAdapter.new(log_file)
|
|
228
186
|
else
|
|
229
187
|
require_syslog
|
|
230
188
|
Syslog.close if Syslog.opened? # need to explictly close it before reopening it
|
|
231
|
-
Syslog.open(@
|
|
189
|
+
Syslog.open(@identity, Syslog::LOG_PID, Syslog::LOG_LOCAL6)
|
|
232
190
|
end
|
|
233
191
|
end
|
|
234
192
|
|
|
@@ -260,8 +218,16 @@ module Bluepill
|
|
|
260
218
|
# The ::Logger method for each syslog priority
|
|
261
219
|
#
|
|
262
220
|
# @api private
|
|
263
|
-
LOGGER_EQUIVALENTS =
|
|
264
|
-
|
|
221
|
+
LOGGER_EQUIVALENTS = {
|
|
222
|
+
debug: :debug,
|
|
223
|
+
err: :error,
|
|
224
|
+
warning: :warn,
|
|
225
|
+
info: :info,
|
|
226
|
+
emerg: :fatal,
|
|
227
|
+
alert: :warn,
|
|
228
|
+
crit: :fatal,
|
|
229
|
+
notice: :info
|
|
230
|
+
}.freeze
|
|
265
231
|
|
|
266
232
|
LOG_METHODS.each do |method|
|
|
267
233
|
next if method == LOGGER_EQUIVALENTS[method]
|