cmdx 1.0.1 → 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.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.cursor/prompts/rspec.md +20 -0
  3. data/.cursor/prompts/yardoc.md +8 -0
  4. data/.rubocop.yml +2 -0
  5. data/CHANGELOG.md +17 -2
  6. data/README.md +1 -1
  7. data/docs/basics/call.md +2 -2
  8. data/docs/basics/chain.md +1 -1
  9. data/docs/callbacks.md +3 -36
  10. data/docs/configuration.md +58 -12
  11. data/docs/interruptions/exceptions.md +1 -1
  12. data/docs/interruptions/faults.md +2 -2
  13. data/docs/logging.md +4 -4
  14. data/docs/middlewares.md +43 -43
  15. data/docs/parameters/coercions.md +49 -38
  16. data/docs/parameters/defaults.md +1 -1
  17. data/docs/parameters/validations.md +0 -39
  18. data/docs/testing.md +11 -12
  19. data/docs/workflows.md +4 -4
  20. data/lib/cmdx/.DS_Store +0 -0
  21. data/lib/cmdx/callback.rb +36 -56
  22. data/lib/cmdx/callback_registry.rb +82 -73
  23. data/lib/cmdx/chain.rb +65 -122
  24. data/lib/cmdx/chain_inspector.rb +22 -115
  25. data/lib/cmdx/chain_serializer.rb +17 -148
  26. data/lib/cmdx/coercion.rb +49 -0
  27. data/lib/cmdx/coercion_registry.rb +94 -0
  28. data/lib/cmdx/coercions/array.rb +18 -36
  29. data/lib/cmdx/coercions/big_decimal.rb +21 -33
  30. data/lib/cmdx/coercions/boolean.rb +21 -40
  31. data/lib/cmdx/coercions/complex.rb +18 -31
  32. data/lib/cmdx/coercions/date.rb +20 -39
  33. data/lib/cmdx/coercions/date_time.rb +22 -39
  34. data/lib/cmdx/coercions/float.rb +19 -32
  35. data/lib/cmdx/coercions/hash.rb +22 -41
  36. data/lib/cmdx/coercions/integer.rb +20 -33
  37. data/lib/cmdx/coercions/rational.rb +20 -32
  38. data/lib/cmdx/coercions/string.rb +23 -31
  39. data/lib/cmdx/coercions/time.rb +24 -40
  40. data/lib/cmdx/coercions/virtual.rb +14 -31
  41. data/lib/cmdx/configuration.rb +57 -171
  42. data/lib/cmdx/context.rb +22 -165
  43. data/lib/cmdx/core_ext/hash.rb +42 -67
  44. data/lib/cmdx/core_ext/module.rb +35 -79
  45. data/lib/cmdx/core_ext/object.rb +63 -98
  46. data/lib/cmdx/correlator.rb +40 -156
  47. data/lib/cmdx/error.rb +37 -202
  48. data/lib/cmdx/errors.rb +165 -202
  49. data/lib/cmdx/fault.rb +55 -158
  50. data/lib/cmdx/faults.rb +26 -137
  51. data/lib/cmdx/immutator.rb +22 -109
  52. data/lib/cmdx/lazy_struct.rb +103 -187
  53. data/lib/cmdx/log_formatters/json.rb +14 -40
  54. data/lib/cmdx/log_formatters/key_value.rb +14 -40
  55. data/lib/cmdx/log_formatters/line.rb +14 -48
  56. data/lib/cmdx/log_formatters/logstash.rb +14 -57
  57. data/lib/cmdx/log_formatters/pretty_json.rb +14 -50
  58. data/lib/cmdx/log_formatters/pretty_key_value.rb +13 -46
  59. data/lib/cmdx/log_formatters/pretty_line.rb +16 -54
  60. data/lib/cmdx/log_formatters/raw.rb +19 -49
  61. data/lib/cmdx/logger.rb +20 -82
  62. data/lib/cmdx/logger_ansi.rb +18 -75
  63. data/lib/cmdx/logger_serializer.rb +24 -114
  64. data/lib/cmdx/middleware.rb +38 -60
  65. data/lib/cmdx/middleware_registry.rb +81 -77
  66. data/lib/cmdx/middlewares/correlate.rb +41 -226
  67. data/lib/cmdx/middlewares/timeout.rb +46 -185
  68. data/lib/cmdx/parameter.rb +120 -198
  69. data/lib/cmdx/parameter_evaluator.rb +231 -0
  70. data/lib/cmdx/parameter_inspector.rb +25 -56
  71. data/lib/cmdx/parameter_registry.rb +59 -84
  72. data/lib/cmdx/parameter_serializer.rb +23 -74
  73. data/lib/cmdx/railtie.rb +24 -107
  74. data/lib/cmdx/result.rb +254 -260
  75. data/lib/cmdx/result_ansi.rb +19 -85
  76. data/lib/cmdx/result_inspector.rb +27 -68
  77. data/lib/cmdx/result_logger.rb +18 -81
  78. data/lib/cmdx/result_serializer.rb +28 -132
  79. data/lib/cmdx/rspec/matchers.rb +28 -0
  80. data/lib/cmdx/rspec/result_matchers/be_executed.rb +42 -0
  81. data/lib/cmdx/rspec/result_matchers/be_failed_task.rb +94 -0
  82. data/lib/cmdx/rspec/result_matchers/be_skipped_task.rb +94 -0
  83. data/lib/cmdx/rspec/result_matchers/be_state_matchers.rb +59 -0
  84. data/lib/cmdx/rspec/result_matchers/be_status_matchers.rb +57 -0
  85. data/lib/cmdx/rspec/result_matchers/be_successful_task.rb +87 -0
  86. data/lib/cmdx/rspec/result_matchers/have_bad_outcome.rb +51 -0
  87. data/lib/cmdx/rspec/result_matchers/have_caused_failure.rb +58 -0
  88. data/lib/cmdx/rspec/result_matchers/have_chain_index.rb +59 -0
  89. data/lib/cmdx/rspec/result_matchers/have_context.rb +86 -0
  90. data/lib/cmdx/rspec/result_matchers/have_empty_metadata.rb +54 -0
  91. data/lib/cmdx/rspec/result_matchers/have_good_outcome.rb +52 -0
  92. data/lib/cmdx/rspec/result_matchers/have_metadata.rb +114 -0
  93. data/lib/cmdx/rspec/result_matchers/have_preserved_context.rb +66 -0
  94. data/lib/cmdx/rspec/result_matchers/have_received_thrown_failure.rb +64 -0
  95. data/lib/cmdx/rspec/result_matchers/have_runtime.rb +78 -0
  96. data/lib/cmdx/rspec/result_matchers/have_thrown_failure.rb +76 -0
  97. data/lib/cmdx/rspec/task_matchers/be_well_formed_task.rb +62 -0
  98. data/lib/cmdx/rspec/task_matchers/have_callback.rb +85 -0
  99. data/lib/cmdx/rspec/task_matchers/have_cmd_setting.rb +68 -0
  100. data/lib/cmdx/rspec/task_matchers/have_executed_callbacks.rb +92 -0
  101. data/lib/cmdx/rspec/task_matchers/have_middleware.rb +46 -0
  102. data/lib/cmdx/rspec/task_matchers/have_parameter.rb +181 -0
  103. data/lib/cmdx/task.rb +213 -425
  104. data/lib/cmdx/task_deprecator.rb +55 -0
  105. data/lib/cmdx/task_processor.rb +245 -0
  106. data/lib/cmdx/task_serializer.rb +22 -70
  107. data/lib/cmdx/utils/ansi_color.rb +13 -89
  108. data/lib/cmdx/utils/log_timestamp.rb +13 -42
  109. data/lib/cmdx/utils/monotonic_runtime.rb +13 -63
  110. data/lib/cmdx/utils/name_affix.rb +21 -71
  111. data/lib/cmdx/validator.rb +48 -0
  112. data/lib/cmdx/validator_registry.rb +86 -0
  113. data/lib/cmdx/validators/exclusion.rb +55 -94
  114. data/lib/cmdx/validators/format.rb +31 -85
  115. data/lib/cmdx/validators/inclusion.rb +65 -110
  116. data/lib/cmdx/validators/length.rb +117 -133
  117. data/lib/cmdx/validators/numeric.rb +123 -130
  118. data/lib/cmdx/validators/presence.rb +38 -79
  119. data/lib/cmdx/version.rb +1 -7
  120. data/lib/cmdx/workflow.rb +46 -339
  121. data/lib/cmdx.rb +1 -1
  122. data/lib/generators/cmdx/install_generator.rb +14 -31
  123. data/lib/generators/cmdx/task_generator.rb +39 -55
  124. data/lib/generators/cmdx/templates/install.rb +24 -6
  125. data/lib/generators/cmdx/workflow_generator.rb +41 -66
  126. data/lib/locales/ar.yml +0 -1
  127. data/lib/locales/cs.yml +0 -1
  128. data/lib/locales/da.yml +0 -1
  129. data/lib/locales/de.yml +0 -1
  130. data/lib/locales/el.yml +0 -1
  131. data/lib/locales/en.yml +0 -1
  132. data/lib/locales/es.yml +0 -1
  133. data/lib/locales/fi.yml +0 -1
  134. data/lib/locales/fr.yml +0 -1
  135. data/lib/locales/he.yml +0 -1
  136. data/lib/locales/hi.yml +0 -1
  137. data/lib/locales/it.yml +0 -1
  138. data/lib/locales/ja.yml +0 -1
  139. data/lib/locales/ko.yml +0 -1
  140. data/lib/locales/nl.yml +0 -1
  141. data/lib/locales/no.yml +0 -1
  142. data/lib/locales/pl.yml +0 -1
  143. data/lib/locales/pt.yml +0 -1
  144. data/lib/locales/ru.yml +0 -1
  145. data/lib/locales/sv.yml +0 -1
  146. data/lib/locales/th.yml +0 -1
  147. data/lib/locales/tr.yml +0 -1
  148. data/lib/locales/vi.yml +0 -1
  149. data/lib/locales/zh.yml +0 -1
  150. metadata +34 -8
  151. data/lib/cmdx/parameter_validator.rb +0 -81
  152. data/lib/cmdx/parameter_value.rb +0 -244
  153. data/lib/cmdx/parameters_inspector.rb +0 -72
  154. data/lib/cmdx/parameters_serializer.rb +0 -115
  155. data/lib/cmdx/rspec/result_matchers.rb +0 -917
  156. data/lib/cmdx/rspec/task_matchers.rb +0 -570
  157. 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 for CMDx logging system.
5
+ # Logstash log formatter that outputs structured log entries in Logstash JSON format.
6
6
  #
7
- # Formats log entries as JSON objects compatible with Logstash and the ELK stack
8
- # (Elasticsearch, Logstash, Kibana). Includes Logstash-specific fields like
9
- # @version and @timestamp for seamless integration with log aggregation pipelines.
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
- # Creates a JSON object with Logstash-specific metadata fields (@version, @timestamp)
49
- # combined with task execution data, severity, and process information for
50
- # seamless integration with ELK stack log processing pipelines.
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
- # @param severity [String] Log severity level (DEBUG, INFO, WARN, ERROR, FATAL)
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
- # @return [String] Single-line Logstash-compatible JSON string with newline terminator
22
+ # @raise [JSON::GeneratorError] if the log data cannot be serialized to JSON
58
23
  #
59
- # @example Success log entry for Logstash
24
+ # @example Formatting a log entry
60
25
  # formatter = CMDx::LogFormatters::Logstash.new
61
- # output = formatter.call("INFO", Time.now, task, "Order processed")
62
- # # => {"@version":"1","@timestamp":"2022-07-17T18:43:15.000000","severity":"INFO",...}\n
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 for CMDx logging system.
5
+ # Pretty JSON log formatter that outputs structured log entries as formatted JSON.
6
6
  #
7
- # Formats log entries as human-readable, multi-line JSON objects with proper
8
- # indentation and formatting. Contains the same structured data as the JSON
9
- # formatter but optimized for development environments and manual inspection.
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
- # Combines task execution metadata with severity, process ID, and timestamp
46
- # information to create a human-readable JSON log entry with proper
47
- # indentation and formatting for development and debugging purposes.
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
- # @param severity [String] Log severity level (DEBUG, INFO, WARN, ERROR, FATAL)
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
- # @return [String] Multi-line pretty-formatted JSON string with newline terminator
22
+ # @raise [JSON::GeneratorError] if the log data cannot be serialized to JSON
55
23
  #
56
- # @example Success log entry
24
+ # @example Formatting a log entry
57
25
  # formatter = CMDx::LogFormatters::PrettyJson.new
58
- # output = formatter.call("INFO", Time.now, task, "Order processed")
59
- # # => Multi-line formatted JSON output
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 for CMDx logging system.
5
+ # Pretty key-value log formatter that outputs structured log entries as human-readable key=value pairs.
6
6
  #
7
- # Formats log entries as space-separated key=value pairs with ANSI color
8
- # highlighting for improved readability in terminal environments. Provides
9
- # the same structured data as KeyValue formatter with enhanced visual presentation.
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 ANSI-colorized key=value pairs.
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] Log severity level (DEBUG, INFO, WARN, ERROR, FATAL)
41
- # @param time [Time] Timestamp when the log entry was created
42
- # @param task [CMDx::Task] Task instance being logged
43
- # @param message [Object] Log message or data to be included
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] Single-line ANSI-colorized key=value formatted string with newline terminator
19
+ # @return [String] the formatted key=value log entry with ANSI colors and trailing newline
46
20
  #
47
- # @example Success log entry with colors
21
+ # @example Formatting a log entry
48
22
  # formatter = CMDx::LogFormatters::PrettyKeyValue.new
49
- # output = formatter.call("INFO", Time.now, task, "Order processed")
50
- # # => ANSI-colored "severity=INFO pid=1234 ... status=success\n"
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 for CMDx logging system.
5
+ # Pretty line log formatter that outputs human-readable log entries with ANSI colors.
6
6
  #
7
- # Formats log entries in a traditional single-line format enhanced with ANSI color
8
- # highlighting for improved readability in terminal environments. Provides the same
9
- # structured format as Line formatter with visual enhancements for severity levels,
10
- # status indicators, and task information.
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 an ANSI-colorized single-line log entry.
13
+ # Formats a log entry as a colorized human-readable line.
41
14
  #
42
- # Creates a log entry in the format: "[COLORED_SEVERITY_INITIAL], [TIMESTAMP #PID] [COLORED_SEVERITY] -- CLASS: [COLORED_MESSAGE]"
43
- # where colors are applied to severity indicators, status values, and other key elements.
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
- # @param severity [String] Log severity level (DEBUG, INFO, WARN, ERROR, FATAL)
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
- # @return [String] Single-line ANSI-colorized log entry with newline terminator
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 Success log entry with colors
25
+ # @example Formatting a log entry
53
26
  # formatter = CMDx::LogFormatters::PrettyLine.new
54
- # output = formatter.call("INFO", Time.now, task, "Order processed")
55
- # # => ANSI-colored "I, [2022-07-17T18:43:15.000000 #1234] INFO -- ProcessOrderTask: state=complete status=success\n"
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 for CMDx logging system.
5
+ # Raw log formatter that outputs log messages using inspect format.
6
6
  #
7
- # Outputs only the raw message content without any additional formatting,
8
- # timestamps, severity indicators, or task metadata. Provides the simplest
9
- # possible log output containing just the inspected message content.
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 raw message content only.
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] Log severity level (ignored)
41
- # @param _time [Time] Timestamp when the log entry was created (ignored)
42
- # @param _task [CMDx::Task] Task instance being logged (ignored)
43
- # @param message [Object] Log message or data to be output
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] Raw message content with inspect formatting and newline terminator
20
+ # @return [String] the inspected message with trailing newline
46
21
  #
47
- # @example String message output
22
+ # @example Formatting a log entry
48
23
  # formatter = CMDx::LogFormatters::Raw.new
49
- # output = formatter.call("INFO", Time.now, task, "Order processed")
50
- # # => "\"Order processed\"\n"
24
+ # result = formatter.call("INFO", Time.now, task_object, "Task completed")
25
+ # # => "\"Task completed\"\n"
51
26
  #
52
- # @example Hash message output
53
- # data = { order_id: 12345, status: "completed" }
54
- # output = formatter.call("DEBUG", Time.now, task, data)
55
- # # => "{:order_id=>12345, :status=>\"completed\"}\n"
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 << "\n"
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 setup module for CMDx tasks.
4
+ # Logger configuration and retrieval utilities for task execution.
5
5
  #
6
- # The Logger module provides centralized logger configuration and initialization
7
- # for CMDx tasks. It handles logger setup, formatter assignment, log level
8
- # configuration, and progname assignment based on task settings.
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 a task.
13
+ # Configures and returns a logger instance for the given task.
51
14
  #
52
- # Retrieves the logger from task settings and applies additional configuration
53
- # including formatter, log level, and progname if specified in task settings.
54
- # Returns nil if no logger is configured.
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 [CMDx::Task] The task instance to configure logging for
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
- # @example Basic logger retrieval
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 Logger with custom formatter
65
- # class MyTask < CMDx::Task
66
- # task_settings!(
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
- # task = MyTask.new
84
- # logger = Logger.call(task) # Logger with DEBUG level applied
85
- #
86
- # @example No logger configured
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.task_setting(:logger)
32
+ logger = task.cmd_setting(:logger)
95
33
 
96
34
  unless logger.nil?
97
- logger.formatter = task.task_setting(:log_formatter) if task.task_setting?(:log_formatter)
98
- logger.level = task.task_setting(:log_level) if task.task_setting?(:log_level)
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