cmdx 1.0.0 → 1.1.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/.cursor/prompts/rspec.md +20 -0
- data/.cursor/prompts/yardoc.md +8 -0
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +101 -49
- data/README.md +2 -1
- data/docs/ai_prompts.md +10 -0
- data/docs/basics/call.md +11 -2
- data/docs/basics/chain.md +10 -1
- data/docs/basics/context.md +9 -0
- data/docs/basics/setup.md +9 -0
- data/docs/callbacks.md +14 -37
- data/docs/configuration.md +68 -27
- data/docs/getting_started.md +11 -0
- data/docs/internationalization.md +148 -0
- data/docs/interruptions/exceptions.md +10 -1
- data/docs/interruptions/faults.md +11 -2
- data/docs/interruptions/halt.md +9 -0
- data/docs/logging.md +14 -4
- data/docs/middlewares.md +53 -43
- data/docs/outcomes/result.md +9 -0
- data/docs/outcomes/states.md +9 -0
- data/docs/outcomes/statuses.md +9 -0
- data/docs/parameters/coercions.md +58 -38
- data/docs/parameters/defaults.md +10 -1
- data/docs/parameters/definitions.md +9 -0
- data/docs/parameters/namespacing.md +9 -0
- data/docs/parameters/validations.md +8 -67
- data/docs/testing.md +22 -13
- data/docs/tips_and_tricks.md +9 -0
- data/docs/workflows.md +14 -4
- data/lib/cmdx/.DS_Store +0 -0
- data/lib/cmdx/callback.rb +36 -56
- data/lib/cmdx/callback_registry.rb +82 -73
- data/lib/cmdx/chain.rb +65 -122
- data/lib/cmdx/chain_inspector.rb +22 -115
- data/lib/cmdx/chain_serializer.rb +17 -148
- data/lib/cmdx/coercion.rb +49 -0
- data/lib/cmdx/coercion_registry.rb +94 -0
- data/lib/cmdx/coercions/array.rb +18 -36
- data/lib/cmdx/coercions/big_decimal.rb +21 -33
- data/lib/cmdx/coercions/boolean.rb +21 -40
- data/lib/cmdx/coercions/complex.rb +18 -31
- data/lib/cmdx/coercions/date.rb +20 -39
- data/lib/cmdx/coercions/date_time.rb +22 -39
- data/lib/cmdx/coercions/float.rb +19 -32
- data/lib/cmdx/coercions/hash.rb +22 -41
- data/lib/cmdx/coercions/integer.rb +20 -33
- data/lib/cmdx/coercions/rational.rb +20 -32
- data/lib/cmdx/coercions/string.rb +23 -31
- data/lib/cmdx/coercions/time.rb +24 -40
- data/lib/cmdx/coercions/virtual.rb +14 -31
- data/lib/cmdx/configuration.rb +57 -171
- data/lib/cmdx/context.rb +22 -165
- data/lib/cmdx/core_ext/hash.rb +42 -67
- data/lib/cmdx/core_ext/module.rb +35 -79
- data/lib/cmdx/core_ext/object.rb +63 -98
- data/lib/cmdx/correlator.rb +40 -156
- data/lib/cmdx/error.rb +37 -202
- data/lib/cmdx/errors.rb +165 -202
- data/lib/cmdx/fault.rb +55 -158
- data/lib/cmdx/faults.rb +26 -137
- data/lib/cmdx/immutator.rb +22 -109
- data/lib/cmdx/lazy_struct.rb +103 -187
- data/lib/cmdx/log_formatters/json.rb +14 -40
- data/lib/cmdx/log_formatters/key_value.rb +14 -40
- data/lib/cmdx/log_formatters/line.rb +14 -48
- data/lib/cmdx/log_formatters/logstash.rb +14 -57
- data/lib/cmdx/log_formatters/pretty_json.rb +14 -50
- data/lib/cmdx/log_formatters/pretty_key_value.rb +13 -46
- data/lib/cmdx/log_formatters/pretty_line.rb +16 -54
- data/lib/cmdx/log_formatters/raw.rb +19 -49
- data/lib/cmdx/logger.rb +20 -82
- data/lib/cmdx/logger_ansi.rb +18 -75
- data/lib/cmdx/logger_serializer.rb +24 -114
- data/lib/cmdx/middleware.rb +38 -60
- data/lib/cmdx/middleware_registry.rb +81 -77
- data/lib/cmdx/middlewares/correlate.rb +41 -226
- data/lib/cmdx/middlewares/timeout.rb +46 -185
- data/lib/cmdx/parameter.rb +120 -198
- data/lib/cmdx/parameter_evaluator.rb +231 -0
- data/lib/cmdx/parameter_inspector.rb +25 -56
- data/lib/cmdx/parameter_registry.rb +59 -84
- data/lib/cmdx/parameter_serializer.rb +23 -74
- data/lib/cmdx/railtie.rb +24 -107
- data/lib/cmdx/result.rb +254 -260
- data/lib/cmdx/result_ansi.rb +19 -85
- data/lib/cmdx/result_inspector.rb +27 -68
- data/lib/cmdx/result_logger.rb +18 -81
- data/lib/cmdx/result_serializer.rb +28 -132
- data/lib/cmdx/rspec/matchers.rb +28 -0
- data/lib/cmdx/rspec/result_matchers/be_executed.rb +42 -0
- data/lib/cmdx/rspec/result_matchers/be_failed_task.rb +94 -0
- data/lib/cmdx/rspec/result_matchers/be_skipped_task.rb +94 -0
- data/lib/cmdx/rspec/result_matchers/be_state_matchers.rb +59 -0
- data/lib/cmdx/rspec/result_matchers/be_status_matchers.rb +57 -0
- data/lib/cmdx/rspec/result_matchers/be_successful_task.rb +87 -0
- data/lib/cmdx/rspec/result_matchers/have_bad_outcome.rb +51 -0
- data/lib/cmdx/rspec/result_matchers/have_caused_failure.rb +58 -0
- data/lib/cmdx/rspec/result_matchers/have_chain_index.rb +59 -0
- data/lib/cmdx/rspec/result_matchers/have_context.rb +86 -0
- data/lib/cmdx/rspec/result_matchers/have_empty_metadata.rb +54 -0
- data/lib/cmdx/rspec/result_matchers/have_good_outcome.rb +52 -0
- data/lib/cmdx/rspec/result_matchers/have_metadata.rb +114 -0
- data/lib/cmdx/rspec/result_matchers/have_preserved_context.rb +66 -0
- data/lib/cmdx/rspec/result_matchers/have_received_thrown_failure.rb +64 -0
- data/lib/cmdx/rspec/result_matchers/have_runtime.rb +78 -0
- data/lib/cmdx/rspec/result_matchers/have_thrown_failure.rb +76 -0
- data/lib/cmdx/rspec/task_matchers/be_well_formed_task.rb +62 -0
- data/lib/cmdx/rspec/task_matchers/have_callback.rb +85 -0
- data/lib/cmdx/rspec/task_matchers/have_cmd_setting.rb +68 -0
- data/lib/cmdx/rspec/task_matchers/have_executed_callbacks.rb +92 -0
- data/lib/cmdx/rspec/task_matchers/have_middleware.rb +46 -0
- data/lib/cmdx/rspec/task_matchers/have_parameter.rb +181 -0
- data/lib/cmdx/task.rb +213 -425
- data/lib/cmdx/task_deprecator.rb +55 -0
- data/lib/cmdx/task_processor.rb +245 -0
- data/lib/cmdx/task_serializer.rb +22 -70
- data/lib/cmdx/utils/ansi_color.rb +13 -89
- data/lib/cmdx/utils/log_timestamp.rb +13 -42
- data/lib/cmdx/utils/monotonic_runtime.rb +13 -63
- data/lib/cmdx/utils/name_affix.rb +21 -71
- data/lib/cmdx/validator.rb +48 -0
- data/lib/cmdx/validator_registry.rb +86 -0
- data/lib/cmdx/validators/exclusion.rb +55 -94
- data/lib/cmdx/validators/format.rb +31 -85
- data/lib/cmdx/validators/inclusion.rb +65 -110
- data/lib/cmdx/validators/length.rb +117 -133
- data/lib/cmdx/validators/numeric.rb +123 -130
- data/lib/cmdx/validators/presence.rb +38 -79
- data/lib/cmdx/version.rb +1 -7
- data/lib/cmdx/workflow.rb +46 -339
- data/lib/cmdx.rb +1 -1
- data/lib/generators/cmdx/install_generator.rb +14 -31
- data/lib/generators/cmdx/task_generator.rb +39 -55
- data/lib/generators/cmdx/templates/install.rb +61 -11
- data/lib/generators/cmdx/workflow_generator.rb +41 -66
- data/lib/locales/ar.yml +35 -0
- data/lib/locales/cs.yml +35 -0
- data/lib/locales/da.yml +35 -0
- data/lib/locales/de.yml +35 -0
- data/lib/locales/el.yml +35 -0
- data/lib/locales/en.yml +19 -20
- data/lib/locales/es.yml +19 -20
- data/lib/locales/fi.yml +35 -0
- data/lib/locales/fr.yml +35 -0
- data/lib/locales/he.yml +35 -0
- data/lib/locales/hi.yml +35 -0
- data/lib/locales/it.yml +35 -0
- data/lib/locales/ja.yml +35 -0
- data/lib/locales/ko.yml +35 -0
- data/lib/locales/nl.yml +35 -0
- data/lib/locales/no.yml +35 -0
- data/lib/locales/pl.yml +35 -0
- data/lib/locales/pt.yml +35 -0
- data/lib/locales/ru.yml +35 -0
- data/lib/locales/sv.yml +35 -0
- data/lib/locales/th.yml +35 -0
- data/lib/locales/tr.yml +35 -0
- data/lib/locales/vi.yml +35 -0
- data/lib/locales/zh.yml +35 -0
- metadata +57 -8
- data/lib/cmdx/parameter_validator.rb +0 -81
- data/lib/cmdx/parameter_value.rb +0 -244
- data/lib/cmdx/parameters_inspector.rb +0 -72
- data/lib/cmdx/parameters_serializer.rb +0 -115
- data/lib/cmdx/rspec/result_matchers.rb +0 -917
- data/lib/cmdx/rspec/task_matchers.rb +0 -570
- data/lib/cmdx/validators/custom.rb +0 -102
@@ -2,72 +2,29 @@
|
|
2
2
|
|
3
3
|
module CMDx
|
4
4
|
module LogFormatters
|
5
|
-
# Logstash log formatter
|
5
|
+
# Logstash log formatter that outputs structured log entries in Logstash JSON format.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# @example Basic usage with global logger configuration
|
12
|
-
# CMDx.configure do |config|
|
13
|
-
# config.logger = Logger.new($stdout, formatter: CMDx::LogFormatters::Logstash.new)
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# @example Task-specific formatter configuration for ELK stack
|
17
|
-
# class ProcessOrderTask < CMDx::Task
|
18
|
-
# task_settings!(log_format: CMDx::LogFormatters::Logstash.new)
|
19
|
-
#
|
20
|
-
# def call
|
21
|
-
# logger.info "Processing order #{order_id}"
|
22
|
-
# end
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# @example Sample Logstash JSON output
|
26
|
-
# {"@version":"1","@timestamp":"2022-07-17T18:43:15.000000","severity":"INFO","pid":1234,"index":0,"chain_id":"018c2b95-b764-7615","type":"Task","class":"ProcessOrderTask","id":"018c2b95-b764-7615","tags":[],"state":"complete","status":"success","outcome":"success","metadata":{},"runtime":15,"origin":"CMDx"}
|
27
|
-
#
|
28
|
-
# @example Logstash configuration for CMDx logs
|
29
|
-
# input {
|
30
|
-
# file {
|
31
|
-
# path => "/var/log/cmdx/*.log"
|
32
|
-
# codec => "json"
|
33
|
-
# }
|
34
|
-
# }
|
35
|
-
# filter {
|
36
|
-
# if [origin] == "CMDx" {
|
37
|
-
# # Process CMDx-specific fields
|
38
|
-
# }
|
39
|
-
# }
|
40
|
-
#
|
41
|
-
# @see CMDx::LogFormatters::Json For standard JSON formatting without Logstash fields
|
42
|
-
# @see CMDx::LoggerSerializer For details on serialized data structure
|
43
|
-
# @see https://www.elastic.co/guide/en/logstash/current/event-api.html Logstash Event API
|
7
|
+
# This formatter converts log entries into Logstash-compatible JSON format, including
|
8
|
+
# required Logstash fields such as @version and @timestamp, along with metadata
|
9
|
+
# such as severity and process ID. Each log entry is output as a single line of
|
10
|
+
# JSON followed by a newline character.
|
44
11
|
class Logstash
|
45
12
|
|
46
13
|
# Formats a log entry as a Logstash-compatible JSON string.
|
47
14
|
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
15
|
+
# @param severity [String] the log severity level (e.g., "INFO", "ERROR")
|
16
|
+
# @param time [Time] the timestamp when the log entry was created
|
17
|
+
# @param task [Object] the task object associated with the log entry
|
18
|
+
# @param message [String] the log message content
|
51
19
|
#
|
52
|
-
# @
|
53
|
-
# @param time [Time] Timestamp when the log entry was created
|
54
|
-
# @param task [CMDx::Task] Task instance being logged
|
55
|
-
# @param message [Object] Log message or data to be included
|
20
|
+
# @return [String] the formatted Logstash JSON log entry with trailing newline
|
56
21
|
#
|
57
|
-
# @
|
22
|
+
# @raise [JSON::GeneratorError] if the log data cannot be serialized to JSON
|
58
23
|
#
|
59
|
-
# @example
|
24
|
+
# @example Formatting a log entry
|
60
25
|
# formatter = CMDx::LogFormatters::Logstash.new
|
61
|
-
#
|
62
|
-
# # => {"@version"
|
63
|
-
#
|
64
|
-
# @example Error log entry with failure chain for ELK analysis
|
65
|
-
# output = formatter.call("ERROR", Time.now, task, error_details)
|
66
|
-
# # => {"@version":"1","severity":"ERROR","caused_failure":{...},"threw_failure":{...},...}\n
|
67
|
-
#
|
68
|
-
# @note The @version field is always set to "1" following Logstash conventions
|
69
|
-
# @note The @timestamp field uses ISO 8601 format for Elasticsearch compatibility
|
70
|
-
# @note All CMDx logs include an "origin" field set to "CMDx" for easy filtering
|
26
|
+
# result = formatter.call("INFO", Time.now, task_object, "Task completed")
|
27
|
+
# # => "{\"severity\":\"INFO\",\"pid\":12345,\"@version\":\"1\",\"@timestamp\":\"2024-01-01T12:00:00.000Z\",\"message\":\"Task completed\"}\n"
|
71
28
|
def call(severity, time, task, message)
|
72
29
|
m = LoggerSerializer.call(severity, time, task, message).merge!(
|
73
30
|
severity:,
|
@@ -2,65 +2,29 @@
|
|
2
2
|
|
3
3
|
module CMDx
|
4
4
|
module LogFormatters
|
5
|
-
# Pretty JSON log formatter
|
5
|
+
# Pretty JSON log formatter that outputs structured log entries as formatted JSON.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
# indentation and
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# @example Basic usage with global logger configuration
|
12
|
-
# CMDx.configure do |config|
|
13
|
-
# config.logger = Logger.new($stdout, formatter: CMDx::LogFormatters::PrettyJson.new)
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# @example Task-specific formatter configuration
|
17
|
-
# class ProcessOrderTask < CMDx::Task
|
18
|
-
# task_settings!(log_format: CMDx::LogFormatters::PrettyJson.new)
|
19
|
-
#
|
20
|
-
# def call
|
21
|
-
# logger.info "Processing order #{order_id}"
|
22
|
-
# end
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# @example Sample pretty JSON output
|
26
|
-
# {
|
27
|
-
# "severity": "INFO",
|
28
|
-
# "pid": 1234,
|
29
|
-
# "timestamp": "2022-07-17T18:43:15.000000",
|
30
|
-
# "index": 0,
|
31
|
-
# "chain_id": "018c2b95-b764-7615",
|
32
|
-
# "type": "Task",
|
33
|
-
# "class": "ProcessOrderTask",
|
34
|
-
# "state": "complete",
|
35
|
-
# "status": "success",
|
36
|
-
# "outcome": "success"
|
37
|
-
# }
|
38
|
-
#
|
39
|
-
# @see CMDx::LogFormatters::Json For compact single-line JSON formatting
|
40
|
-
# @see CMDx::LoggerSerializer For details on serialized data structure
|
7
|
+
# This formatter converts log entries into pretty-printed JSON format with proper
|
8
|
+
# indentation and line breaks, including metadata such as severity, process ID,
|
9
|
+
# and timestamp. Each log entry is output as a multi-line JSON structure followed
|
10
|
+
# by a newline character, making it human-readable while maintaining structure.
|
41
11
|
class PrettyJson
|
42
12
|
|
43
13
|
# Formats a log entry as a pretty-printed JSON string.
|
44
14
|
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
15
|
+
# @param severity [String] the log severity level (e.g., "INFO", "ERROR")
|
16
|
+
# @param time [Time] the timestamp when the log entry was created
|
17
|
+
# @param task [Object] the task object associated with the log entry
|
18
|
+
# @param message [String] the log message content
|
48
19
|
#
|
49
|
-
# @
|
50
|
-
# @param time [Time] Timestamp when the log entry was created
|
51
|
-
# @param task [CMDx::Task] Task instance being logged
|
52
|
-
# @param message [Object] Log message or data to be included
|
20
|
+
# @return [String] the formatted pretty JSON log entry with trailing newline
|
53
21
|
#
|
54
|
-
# @
|
22
|
+
# @raise [JSON::GeneratorError] if the log data cannot be serialized to JSON
|
55
23
|
#
|
56
|
-
# @example
|
24
|
+
# @example Formatting a log entry
|
57
25
|
# formatter = CMDx::LogFormatters::PrettyJson.new
|
58
|
-
#
|
59
|
-
# # =>
|
60
|
-
#
|
61
|
-
# @example Error log entry with nested metadata
|
62
|
-
# output = formatter.call("ERROR", Time.now, task, error_details)
|
63
|
-
# # => Pretty-formatted JSON with nested failure information
|
26
|
+
# result = formatter.call("INFO", Time.now, task_object, "Task completed")
|
27
|
+
# # => "{\n \"severity\": \"INFO\",\n \"pid\": 12345,\n \"timestamp\": \"2024-01-01T12:00:00Z\",\n \"message\": \"Task completed\"\n}\n"
|
64
28
|
def call(severity, time, task, message)
|
65
29
|
m = LoggerSerializer.call(severity, time, task, message).merge!(
|
66
30
|
severity:,
|
@@ -2,59 +2,26 @@
|
|
2
2
|
|
3
3
|
module CMDx
|
4
4
|
module LogFormatters
|
5
|
-
# Pretty key-value log formatter
|
5
|
+
# Pretty key-value log formatter that outputs structured log entries as human-readable key=value pairs.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# @example Basic usage with global logger configuration
|
12
|
-
# CMDx.configure do |config|
|
13
|
-
# config.logger = Logger.new($stdout, formatter: CMDx::LogFormatters::PrettyKeyValue.new)
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# @example Task-specific formatter configuration
|
17
|
-
# class ProcessOrderTask < CMDx::Task
|
18
|
-
# task_settings!(log_format: CMDx::LogFormatters::PrettyKeyValue.new)
|
19
|
-
#
|
20
|
-
# def call
|
21
|
-
# logger.info "Processing order #{order_id}"
|
22
|
-
# end
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# @example Sample pretty key-value output (with ANSI colors)
|
26
|
-
# severity=INFO pid=1234 timestamp=2022-07-17T18:43:15.000000 index=0 chain_id=018c2b95-b764-7615 type=Task class=ProcessOrderTask state=complete status=success outcome=success runtime=15
|
27
|
-
# # Colors applied: severity levels, status values, class names, etc.
|
28
|
-
#
|
29
|
-
# @see CMDx::LogFormatters::KeyValue For plain key-value formatting without colors
|
30
|
-
# @see CMDx::LoggerAnsi For ANSI color definitions
|
31
|
-
# @see CMDx::LoggerSerializer For details on serialized data structure
|
7
|
+
# This formatter converts log entries into a space-separated key=value format with ANSI coloring
|
8
|
+
# for enhanced readability in terminal output. Each log entry includes metadata such as severity,
|
9
|
+
# process ID, and timestamp, with each entry terminated by a newline character.
|
32
10
|
class PrettyKeyValue
|
33
11
|
|
34
|
-
# Formats a log entry as
|
35
|
-
#
|
36
|
-
# Combines task execution metadata with severity, process ID, and timestamp
|
37
|
-
# information to create a visually enhanced key-value log entry with ANSI
|
38
|
-
# color codes for improved readability in terminal environments.
|
12
|
+
# Formats a log entry as a colorized key=value string.
|
39
13
|
#
|
40
|
-
# @param severity [String]
|
41
|
-
# @param time [Time]
|
42
|
-
# @param task [
|
43
|
-
# @param message [
|
14
|
+
# @param severity [String] the log severity level (e.g., "INFO", "ERROR")
|
15
|
+
# @param time [Time] the timestamp when the log entry was created
|
16
|
+
# @param task [Object] the task object associated with the log entry
|
17
|
+
# @param message [String] the log message content
|
44
18
|
#
|
45
|
-
# @return [String]
|
19
|
+
# @return [String] the formatted key=value log entry with ANSI colors and trailing newline
|
46
20
|
#
|
47
|
-
# @example
|
21
|
+
# @example Formatting a log entry
|
48
22
|
# formatter = CMDx::LogFormatters::PrettyKeyValue.new
|
49
|
-
#
|
50
|
-
# # =>
|
51
|
-
#
|
52
|
-
# @example Error log entry with colored failure indicators
|
53
|
-
# output = formatter.call("ERROR", Time.now, task, error_details)
|
54
|
-
# # => ANSI-colored "severity=ERROR ... status=failed\n" (red highlighting)
|
55
|
-
#
|
56
|
-
# @note ANSI colors are automatically applied to key elements like severity levels,
|
57
|
-
# status values, class names, and metadata for enhanced visual distinction
|
23
|
+
# result = formatter.call("INFO", Time.now, task_object, "Task completed")
|
24
|
+
# # => "severity=INFO pid=12345 timestamp=2024-01-01T12:00:00Z message=Task completed\n"
|
58
25
|
def call(severity, time, task, message)
|
59
26
|
m = LoggerSerializer.call(severity, time, task, message, ansi_colorize: true).merge!(
|
60
27
|
severity:,
|
@@ -2,68 +2,30 @@
|
|
2
2
|
|
3
3
|
module CMDx
|
4
4
|
module LogFormatters
|
5
|
-
# Pretty line log formatter
|
5
|
+
# Pretty line log formatter that outputs human-readable log entries with ANSI colors.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# @example Basic usage with global logger configuration
|
13
|
-
# CMDx.configure do |config|
|
14
|
-
# config.logger = Logger.new(STDOUT, formatter: CMDx::LogFormatters::PrettyLine.new)
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# @example Task-specific formatter configuration
|
18
|
-
# class ProcessOrderTask < CMDx::Task
|
19
|
-
# task_settings!(log_format: CMDx::LogFormatters::PrettyLine.new)
|
20
|
-
#
|
21
|
-
# def call
|
22
|
-
# logger.info "Processing order #{order_id}"
|
23
|
-
# end
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# @example Sample pretty line output (with ANSI colors)
|
27
|
-
# I, [2022-07-17T18:43:15.000000 #1234] INFO -- ProcessOrderTask: state=complete status=success outcome=success runtime=15
|
28
|
-
# # Colors applied: INFO in blue, success in green, class name highlighted
|
29
|
-
#
|
30
|
-
# @example Error pretty line output with colored failure indicators
|
31
|
-
# E, [2022-07-17T18:43:15.000000 #1234] ERROR -- ProcessOrderTask: state=interrupted status=failed outcome=failed
|
32
|
-
# # Colors applied: ERROR in red, failed in red, class name highlighted
|
33
|
-
#
|
34
|
-
# @see CMDx::LogFormatters::Line For plain line formatting without colors
|
35
|
-
# @see CMDx::LoggerAnsi For ANSI color definitions
|
36
|
-
# @see CMDx::LoggerSerializer For details on serialized data structure
|
37
|
-
# @see CMDx::Utils::LogTimestamp For timestamp formatting
|
7
|
+
# This formatter converts log entries into a traditional log line format with
|
8
|
+
# color-coded severity levels, timestamps, and process information. The output
|
9
|
+
# is designed to be easily readable in terminal environments that support ANSI
|
10
|
+
# color codes.
|
38
11
|
class PrettyLine
|
39
12
|
|
40
|
-
# Formats a log entry as
|
13
|
+
# Formats a log entry as a colorized human-readable line.
|
41
14
|
#
|
42
|
-
#
|
43
|
-
#
|
15
|
+
# @param severity [String] the log severity level (e.g., "INFO", "ERROR")
|
16
|
+
# @param time [Time] the timestamp when the log entry was created
|
17
|
+
# @param task [Object] the task object associated with the log entry
|
18
|
+
# @param message [String] the log message content
|
44
19
|
#
|
45
|
-
# @
|
46
|
-
# @param time [Time] Timestamp when the log entry was created
|
47
|
-
# @param task [CMDx::Task] Task instance being logged
|
48
|
-
# @param message [Object] Log message or data to be included
|
20
|
+
# @return [String] the formatted log line with ANSI colors and trailing newline
|
49
21
|
#
|
50
|
-
# @
|
22
|
+
# @raise [NoMethodError] if the task object doesn't respond to class or name methods
|
23
|
+
# @raise [StandardError] if LoggerSerializer, LoggerAnsi, or LogTimestamp fail
|
51
24
|
#
|
52
|
-
# @example
|
25
|
+
# @example Formatting a log entry
|
53
26
|
# formatter = CMDx::LogFormatters::PrettyLine.new
|
54
|
-
#
|
55
|
-
# # =>
|
56
|
-
#
|
57
|
-
# @example Warning log entry with colored indicators
|
58
|
-
# output = formatter.call("WARN", Time.now, task, "Order delayed")
|
59
|
-
# # => ANSI-colored "W, [2022-07-17T18:43:15.000000 #1234] WARN -- ProcessOrderTask: state=interrupted status=skipped\n"
|
60
|
-
#
|
61
|
-
# @example Error log entry with red highlighting
|
62
|
-
# output = formatter.call("ERROR", Time.now, task, error_details)
|
63
|
-
# # => ANSI-colored "E, [2022-07-17T18:43:15.000000 #1234] ERROR -- ProcessOrderTask: status=failed caused_failure={...}\n"
|
64
|
-
#
|
65
|
-
# @note This is the default formatter for CMDx when no specific formatter is configured
|
66
|
-
# @note ANSI colors are automatically applied based on severity levels and status values
|
27
|
+
# result = formatter.call("INFO", Time.now, task_object, "Task completed")
|
28
|
+
# # => "\e[32mI\e[0m, [2024-01-01T12:00:00.000Z #12345] \e[32mINFO\e[0m -- MyTask: Task completed\n"
|
67
29
|
def call(severity, time, task, message)
|
68
30
|
i = LoggerAnsi.call(severity[0])
|
69
31
|
s = LoggerAnsi.call(severity)
|
@@ -2,64 +2,34 @@
|
|
2
2
|
|
3
3
|
module CMDx
|
4
4
|
module LogFormatters
|
5
|
-
# Raw log formatter
|
5
|
+
# Raw log formatter that outputs log messages using inspect format.
|
6
6
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# @example Basic usage with global logger configuration
|
12
|
-
# CMDx.configure do |config|
|
13
|
-
# config.logger = Logger.new($stdout, formatter: CMDx::LogFormatters::Raw.new)
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# @example Task-specific formatter configuration
|
17
|
-
# class ProcessOrderTask < CMDx::Task
|
18
|
-
# task_settings!(log_format: CMDx::LogFormatters::Raw.new)
|
19
|
-
#
|
20
|
-
# def call
|
21
|
-
# logger.info "Processing order #{order_id}"
|
22
|
-
# logger.debug({ order_details: order.attributes })
|
23
|
-
# end
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# @example Sample raw output
|
27
|
-
# "Processing order 12345"
|
28
|
-
# {:order_details=>{:id=>12345, :status=>"pending"}}
|
29
|
-
#
|
30
|
-
# @see CMDx::LogFormatters::Line For structured log formatting with metadata
|
31
|
-
# @see CMDx::LogFormatters::Json For JSON-formatted output
|
7
|
+
# This formatter provides a simple, unstructured log output by calling
|
8
|
+
# inspect on the message content. It ignores severity, time, and task
|
9
|
+
# metadata, focusing only on the raw message content. Each log entry
|
10
|
+
# is output as an inspected string followed by a newline character.
|
32
11
|
class Raw
|
33
12
|
|
34
|
-
# Formats a log entry as
|
35
|
-
#
|
36
|
-
# Outputs only the message parameter using Ruby's inspect method,
|
37
|
-
# ignoring all other log context including severity, timestamp, and task information.
|
38
|
-
# Useful for debugging scenarios where only the message content is relevant.
|
13
|
+
# Formats a log entry as an inspected string.
|
39
14
|
#
|
40
|
-
# @param _severity [String]
|
41
|
-
# @param _time [Time]
|
42
|
-
# @param _task [
|
43
|
-
# @param message [Object]
|
15
|
+
# @param _severity [String] the log severity level (ignored)
|
16
|
+
# @param _time [Time] the timestamp when the log entry was created (ignored)
|
17
|
+
# @param _task [Object] the task object associated with the log entry (ignored)
|
18
|
+
# @param message [Object] the log message content to be inspected
|
44
19
|
#
|
45
|
-
# @return [String]
|
20
|
+
# @return [String] the inspected message with trailing newline
|
46
21
|
#
|
47
|
-
# @example
|
22
|
+
# @example Formatting a log entry
|
48
23
|
# formatter = CMDx::LogFormatters::Raw.new
|
49
|
-
#
|
50
|
-
# # => "\"
|
24
|
+
# result = formatter.call("INFO", Time.now, task_object, "Task completed")
|
25
|
+
# # => "\"Task completed\"\n"
|
51
26
|
#
|
52
|
-
# @example
|
53
|
-
#
|
54
|
-
#
|
55
|
-
# # => "{:
|
56
|
-
#
|
57
|
-
# @example Array message output
|
58
|
-
# items = ["item1", "item2", "item3"]
|
59
|
-
# output = formatter.call("INFO", Time.now, task, items)
|
60
|
-
# # => "[\"item1\", \"item2\", \"item3\"]\n"
|
27
|
+
# @example Formatting a complex object
|
28
|
+
# formatter = CMDx::LogFormatters::Raw.new
|
29
|
+
# result = formatter.call("DEBUG", Time.now, task_object, { status: :success, count: 42 })
|
30
|
+
# # => "{:status=>:success, :count=>42}\n"
|
61
31
|
def call(_severity, _time, _task, message)
|
62
|
-
message.inspect
|
32
|
+
message.inspect + "\n" # rubocop:disable Style/StringConcatenation
|
63
33
|
end
|
64
34
|
|
65
35
|
end
|
data/lib/cmdx/logger.rb
CHANGED
@@ -1,101 +1,39 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module CMDx
|
4
|
-
# Logger configuration and
|
4
|
+
# Logger configuration and retrieval utilities for task execution.
|
5
5
|
#
|
6
|
-
#
|
7
|
-
# for
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# @example Basic logger usage
|
11
|
-
# class ProcessOrderTask < CMDx::Task
|
12
|
-
# def call
|
13
|
-
# logger.info "Processing order #{order_id}"
|
14
|
-
# logger.debug { "Order details: #{order.inspect}" }
|
15
|
-
# end
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# @example Task-specific logger configuration
|
19
|
-
# class ProcessOrderTask < CMDx::Task
|
20
|
-
# task_settings!(
|
21
|
-
# logger: Rails.logger,
|
22
|
-
# log_formatter: CMDx::LogFormatters::Json.new,
|
23
|
-
# log_level: Logger::INFO
|
24
|
-
# )
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# @example Global logger configuration
|
28
|
-
# CMDx.configure do |config|
|
29
|
-
# config.logger = Logger.new($stdout)
|
30
|
-
# config.log_formatter = CMDx::LogFormatters::PrettyLine.new
|
31
|
-
# config.log_level = Logger::WARN
|
32
|
-
# end
|
33
|
-
#
|
34
|
-
# @example Custom logger with specific formatter
|
35
|
-
# logger = Logger.new(STDOUT)
|
36
|
-
# logger.formatter = CMDx::LogFormatters::Logstash.new
|
37
|
-
# logger.level = Logger::DEBUG
|
38
|
-
#
|
39
|
-
# class MyTask < CMDx::Task
|
40
|
-
# task_settings!(logger: logger)
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# @see CMDx::LogFormatters Log formatting options
|
44
|
-
# @see CMDx::ResultLogger Result-specific logging functionality
|
45
|
-
# @see CMDx::Task Task logging integration
|
6
|
+
# This module provides functionality to configure and retrieve logger instances
|
7
|
+
# for task execution, applying task-specific settings such as formatter, level,
|
8
|
+
# and program name when available.
|
46
9
|
module Logger
|
47
10
|
|
48
11
|
module_function
|
49
12
|
|
50
|
-
# Configures and returns a logger instance for
|
13
|
+
# Configures and returns a logger instance for the given task.
|
51
14
|
#
|
52
|
-
#
|
53
|
-
# including formatter,
|
54
|
-
#
|
15
|
+
# This method retrieves the logger from task settings and applies any
|
16
|
+
# available configuration options including formatter, level, and program name.
|
17
|
+
# The task itself is set as the logger's program name for identification.
|
55
18
|
#
|
56
|
-
# @param task [
|
57
|
-
# @return [::Logger, nil] Configured logger instance or nil if not set
|
19
|
+
# @param task [Task] the task instance to configure logging for
|
58
20
|
#
|
59
|
-
# @
|
60
|
-
# task = ProcessOrderTask.new
|
61
|
-
# logger = Logger.call(task)
|
62
|
-
# logger.info "Task started" if logger
|
21
|
+
# @return [Logger, nil] the configured logger instance or nil if no logger is set
|
63
22
|
#
|
64
|
-
# @example
|
65
|
-
#
|
66
|
-
#
|
67
|
-
# logger: Logger.new(STDOUT),
|
68
|
-
# log_formatter: CMDx::LogFormatters::Json.new
|
69
|
-
# )
|
70
|
-
# end
|
71
|
-
#
|
72
|
-
# task = MyTask.new
|
73
|
-
# logger = Logger.call(task) # Logger with JSON formatter applied
|
74
|
-
#
|
75
|
-
# @example Logger with custom level
|
76
|
-
# class MyTask < CMDx::Task
|
77
|
-
# task_settings!(
|
78
|
-
# logger: Logger.new(STDOUT),
|
79
|
-
# log_level: Logger::DEBUG
|
80
|
-
# )
|
81
|
-
# end
|
23
|
+
# @example Configure logger for a task
|
24
|
+
# logger = CMDx::Logger.call(task)
|
25
|
+
# logger.info("Task started")
|
82
26
|
#
|
83
|
-
#
|
84
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
# class MyTask < CMDx::Task
|
88
|
-
# # No logger setting
|
89
|
-
# end
|
90
|
-
#
|
91
|
-
# task = MyTask.new
|
92
|
-
# logger = Logger.call(task) # => nil
|
27
|
+
# @example Logger with custom formatter
|
28
|
+
# task.set_cmd_setting(:log_formatter, custom_formatter)
|
29
|
+
# logger = CMDx::Logger.call(task)
|
30
|
+
# logger.debug("Debug message")
|
93
31
|
def call(task)
|
94
|
-
logger = task.
|
32
|
+
logger = task.cmd_setting(:logger)
|
95
33
|
|
96
34
|
unless logger.nil?
|
97
|
-
logger.formatter = task.
|
98
|
-
logger.level = task.
|
35
|
+
logger.formatter = task.cmd_setting(:log_formatter) if task.cmd_setting?(:log_formatter)
|
36
|
+
logger.level = task.cmd_setting(:log_level) if task.cmd_setting?(:log_level)
|
99
37
|
logger.progname = task
|
100
38
|
end
|
101
39
|
|