betterlog 2.0.6 → 2.1.1

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.
@@ -4,22 +4,61 @@
4
4
  # tools etc.
5
5
 
6
6
  module Betterlog
7
+
8
+ # Thread-local storage for global metadata used to enrich log events.
9
+ #
10
+ # This class provides a thread-safe mechanism to store and manage metadata
11
+ # that should be included with log events. It ensures that metadata is
12
+ # scoped to the current thread and can be easily added, removed, or
13
+ # temporarily applied within a block context.
14
+ #
15
+ # @see Betterlog::Log::Event
16
+ # @see Betterlog.with_meta
7
17
  class GlobalMetadata
8
18
  include Tins::SexySingleton
9
19
 
10
20
  thread_local(:current) { {} }
11
21
 
22
+ # Adds metadata to the current thread-local storage.
23
+ #
24
+ # This method takes a hash of data and merges it with the existing metadata
25
+ # in the current thread's storage. The provided data is symbolized and then
26
+ # combined with the current metadata using a union operation, ensuring that
27
+ # new keys overwrite existing ones with the same name.
28
+ #
29
+ # @param data [ Hash ] A hash containing the metadata to be added
30
+ # @return [ Betterlog::GlobalMetadata ] Returns the GlobalMetadata instance itself
12
31
  def add(data)
13
32
  data = data.symbolize_keys_recursive
14
33
  self.current = data | current
15
34
  self
16
35
  end
17
36
 
37
+ # Removes metadata keys from the current thread-local storage.
38
+ #
39
+ # This method takes a hash or array of keys and deletes the corresponding
40
+ # entries from the global metadata stored in the current thread. It ensures
41
+ # that only the specified keys are removed, leaving other metadata intact.
42
+ #
43
+ # @param data [ Hash, Array ] A hash containing keys to remove or an array
44
+ # of key symbols
45
+ # @return [ void ] Returns nil after removing the specified keys
18
46
  def remove(data)
19
47
  keys = data.ask_and_send_or_self(:keys).map(&:to_sym)
20
48
  keys.each { current.delete(_1) }
21
49
  end
22
50
 
51
+ # Temporarily adds metadata to the current thread-local storage for the
52
+ # duration of a block execution.
53
+ #
54
+ # This method allows for the temporary addition of metadata to the global
55
+ # metadata store within the context of a block. The metadata is
56
+ # automatically removed after the block completes, ensuring that the
57
+ # metadata changes do not persist beyond the intended scope.
58
+ #
59
+ # @param data [ Hash ] A hash containing the metadata key-value pairs to be added
60
+ # @yield [ data ] Executes the provided block with the metadata in place
61
+ # @return [ void ] Returns nil after the block has been executed and metadata removed
23
62
  def with_meta(data = {}, &block)
24
63
  add data
25
64
  block.call
@@ -28,6 +67,16 @@ module Betterlog
28
67
  end
29
68
  end
30
69
 
70
+ # Provides a convenient way to temporarily add metadata to the global
71
+ # metadata store within the scope of a block.
72
+ #
73
+ # This method serves as a shortcut to Betterlog::GlobalMetadata.with_meta,
74
+ # allowing for easy temporary addition of metadata that is automatically
75
+ # removed after the block execution completes.
76
+ #
77
+ # @param data [ Hash ] A hash containing the metadata key-value pairs to be added
78
+ # @yield [ data ] Executes the provided block with the metadata in place
79
+ # @return [ void ] Returns nil after the block has been executed and metadata removed
31
80
  def self.with_meta(data = {}, &block)
32
81
  Betterlog::GlobalMetadata.with_meta(data, &block)
33
82
  end
@@ -1,8 +1,30 @@
1
+ require 'socket'
2
+
1
3
  module Betterlog
2
4
  class Log
5
+ # A structured logging event representation.
6
+ #
7
+ # This class encapsulates log event data with standardized formatting and
8
+ # metadata enrichment. It provides methods for creating, formatting, and
9
+ # serializing log events while ensuring consistent structure and
10
+ # compatibility with various logging systems.
11
+ #
12
+ # @see Betterlog::Log::EventFormatter
13
+ # @see Betterlog::Log::Severity
14
+ # @see Betterlog::GlobalMetadata
3
15
  class Event
4
- require 'socket'
5
-
16
+ # Converts an input argument into a log event object with standardized
17
+ # formatting.
18
+ #
19
+ # This method processes various types of input including exceptions,
20
+ # strings, and hashes, transforming them into structured log events while
21
+ # preserving or enhancing their metadata. It ensures consistent event
22
+ # creation by normalizing keys and applying default values
23
+ # where necessary.
24
+ #
25
+ # @param arg [ Object ] the input argument to be converted into a log event
26
+ # @param rest [ Hash ] additional key-value pairs to be merged into the resulting event
27
+ # @return [ Betterlog::Log::Event ] a new log event instance created from the input argument
6
28
  def self.ify(arg, **rest)
7
29
  rest = rest.symbolize_keys_recursive(circular: :circular)
8
30
  if e = arg.ask_and_send(:exception)
@@ -23,11 +45,32 @@ module Betterlog
23
45
  end
24
46
  end
25
47
 
48
+ # Parses a JSON string into a new log event instance.
49
+ #
50
+ # This method takes a JSON representation of a log event and converts it
51
+ # into a structured event object. It attempts to parse the JSON string
52
+ # and create a new event using the parsed data. If the JSON is malformed
53
+ # or cannot be parsed, the method silently rescues the error and returns
54
+ # nil.
55
+ #
56
+ # @param json [ String ] a JSON-formatted string representing a log event
57
+ # @return [ Betterlog::Log::Event, nil ] a new log event instance if
58
+ # parsing succeeds, nil otherwise
26
59
  def self.parse(json)
27
60
  new(JSON.parse(json))
28
61
  rescue JSON::ParserError
29
62
  end
30
63
 
64
+ # Checks if a JSON string represents a log event with emitter data.
65
+ #
66
+ # This method attempts to parse a JSON string and determine whether it
67
+ # contains a log event structure by checking for the presence of an
68
+ # 'emitter' key in the parsed data. It returns true if the JSON is valid
69
+ # and contains the emitter key, false otherwise.
70
+ #
71
+ # @param json [ String ] A JSON-formatted string to check
72
+ # @return [ Boolean ] true if the JSON represents a log event with
73
+ # emitter data, false if not or if parsing fails
31
74
  def self.is?(json)
32
75
  if json = json.ask_and_send(:to_str)
33
76
  data = JSON.parse(json).ask_and_send(:to_hash)
@@ -37,6 +80,15 @@ module Betterlog
37
80
  false
38
81
  end
39
82
 
83
+ # Initializes a new log event with the provided data.
84
+ #
85
+ # This constructor processes the input data by normalizing keys, computing
86
+ # default values for missing fields, and setting up the event's severity level.
87
+ # It ensures that all log events have consistent structure and required fields
88
+ # such as timestamp, PID, program name, and host information.
89
+ #
90
+ # @param data [ Hash ] a hash containing the initial data for the log event
91
+ # @return [ Betterlog::Log::Event ] a new log event instance with processed data
40
92
  def initialize(data = {})
41
93
  data = compute_data(data.symbolize_keys_recursive(circular: :circular))
42
94
  data[:severity] =
@@ -51,10 +103,31 @@ module Betterlog
51
103
  @data = Hash[data.sort_by(&:first)]
52
104
  end
53
105
 
106
+ # Returns a duplicate of the internal data hash for JSON serialization.
107
+ #
108
+ # This method provides access to the log event's underlying data
109
+ # structure by returning a shallow copy of the internal @data instance
110
+ # variable. It is primarily used to enable JSON serialization of log
111
+ # events while ensuring that modifications to the returned hash do not
112
+ # affect the original event data.
113
+ #
114
+ # @return [ Hash ] a duplicate of the internal data hash containing all
115
+ # log event attributes and metadata
54
116
  def as_json(*a)
55
117
  @data.dup
56
118
  end
57
119
 
120
+ # Converts the log event to a JSON string representation.
121
+ #
122
+ # This method generates a JSON-encoded string of the log event's data,
123
+ # providing a structured format suitable for logging and transmission.
124
+ # In cases where JSON generation fails due to encoding issues or other errors,
125
+ # it falls back to a minimal JSON representation containing only the severity
126
+ # and a cleaned-up message to ensure logging functionality remains intact.
127
+ #
128
+ # @return [ String ] A JSON string representation of the log event
129
+ # @see #as_json
130
+ # @see JSON.generate
58
131
  def to_json(*a)
59
132
  JSON.generate(as_json)
60
133
  rescue
@@ -67,44 +140,130 @@ module Betterlog
67
140
  })
68
141
  end
69
142
 
143
+ # Formats the log event using the specified formatting options.
144
+ #
145
+ # This method delegates to an EventFormatter instance to process the log
146
+ # event according to the provided arguments, enabling flexible output
147
+ # generation including JSON representation and pretty-printed strings
148
+ # with optional colorization.
149
+ #
150
+ # @param args [ Hash ] A hash of formatting options to control the output format
151
+ # @return [ String ] The formatted string representation of the log event
70
152
  def format(**args)
71
153
  Log::EventFormatter.new(self).format(**args)
72
154
  end
73
155
 
74
156
  alias to_s format
75
157
 
158
+ # Sets a value in the event data hash using the provided name as a key.
159
+ #
160
+ # This method allows for direct assignment of a value to a specific key
161
+ # within the log event's internal data structure. The key is converted to a
162
+ # symbol before being used to store the value, ensuring consistency with
163
+ # the internal storage format.
164
+ #
165
+ # @param name [ String, Symbol ] the key to set in the event data
166
+ # @param value [ Object ] the value to associate with the given key
167
+ # @return [ Object ] the assigned value
76
168
  def []=(name, value)
77
169
  @data[name.to_sym] = value
78
170
  end
79
171
 
172
+ # Retrieves a value from the event data hash using the provided name as a key.
173
+ #
174
+ # This method allows for access to specific fields stored within the log
175
+ # event's internal data structure by converting the provided name to a
176
+ # symbol and using it to look up the corresponding value.
177
+ #
178
+ # @param name [ String, Symbol ] the key used to retrieve the value from
179
+ # the event data
180
+ # @return [ Object, nil ] the value associated with the given key, or nil
181
+ # if the key does not exist
80
182
  def [](name)
81
183
  @data[name.to_sym]
82
184
  end
83
185
 
186
+ # Returns the severity level of the log event.
187
+ #
188
+ # This method provides access to the severity attribute that was assigned
189
+ # to the log event during its initialization. The severity indicates the
190
+ # importance or urgency of the log message, such as debug, info, warn,
191
+ # error, or fatal levels.
192
+ #
193
+ # @return [ Betterlog::Log::Severity ] the severity level object associated
194
+ # with this log event
84
195
  def severity
85
196
  @data[:severity]
86
197
  end
87
198
 
199
+ # Returns the emitter identifier associated with the log event.
200
+ #
201
+ # This method provides access to the emitter field stored within the log
202
+ # event's data hash. The emitter typically indicates the source or type
203
+ # of system that generated the log entry, helping to categorize and route
204
+ # log messages appropriately.
205
+ #
206
+ # @return [ String, nil ] the emitter identifier if set, otherwise nil
88
207
  def emitter
89
208
  @data[:emitter]
90
209
  end
91
210
 
211
+ # Returns the notification flag from the log event data.
212
+ #
213
+ # This method retrieves the value associated with the :notify key from
214
+ # the event's internal data hash. The return value indicates whether the
215
+ # log event should trigger notifications to registered notifiers.
216
+ #
217
+ # @return [ Object, nil ] the notification setting from the event data,
218
+ # or nil if not set
92
219
  def notify?
93
220
  @data[:notify]
94
221
  end
95
222
 
223
+ # Checks equality between this log event and another object based on
224
+ # their internal data.
225
+ #
226
+ # This method compares the internal data hash of this log event with that
227
+ # of another object to determine if they contain identical content. It
228
+ # accesses the private @data instance variable from both objects and uses
229
+ # the built-in eql? method for hash comparison to ensure a deep equality
230
+ # check.
231
+ #
232
+ # @param other [ Object ] the object to compare against, expected to be another
233
+ # Betterlog::Log::Event instance
234
+ # @return [ TrueClass, FalseClass ] true if both objects have equal internal data,
235
+ # false otherwise
96
236
  def eql?(other)
97
237
  @data.eql? other.instance_variable_get(:@data)
98
238
  end
99
239
 
100
240
  alias == eql?
101
241
 
242
+ # Returns the hash value corresponding to the event's data.
243
+ #
244
+ # This method provides access to the cached hash representation of the
245
+ # internal data hash, which is used for consistent identification and
246
+ # comparison operations within the logging system. The returned hash
247
+ # value is derived from the event's current data state and remains stable
248
+ # as long as the data does not change.
249
+ #
250
+ # @return [ Integer ] the hash value of the event's internal data structure
102
251
  def hash
103
252
  @data.hash
104
253
  end
105
254
 
106
255
  private
107
256
 
257
+ # Processes input data to compute and enrich log event information.
258
+ #
259
+ # This method takes raw log data and enriches it with default values for
260
+ # standard log fields such as timestamp, process ID, program name, and
261
+ # host information. It also merges in global metadata and Sidekiq context
262
+ # information when available to provide comprehensive context for the log
263
+ # event.
264
+ #
265
+ # @param data [ Hash ] the raw input data to be processed and enriched
266
+ # @return [ Hash ] the processed data hash with default values and metadata merged in
108
267
  def compute_data(data)
109
268
  d = data | {
110
269
  timestamp: Time.now.utc.iso8601(3),
@@ -3,14 +3,48 @@ require 'term/ansicolor'
3
3
 
4
4
  module Betterlog
5
5
  class Log
6
+ # Formats log events for structured logging output.
7
+ #
8
+ # This class provides functionality to format log events into various
9
+ # output formats, including JSON representation and pretty-printed strings
10
+ # with color support. It handles the conversion of log data into
11
+ # human-readable formats while maintaining structured logging capabilities.
12
+ #
13
+ # @see Betterlog::Log::Event
14
+ # @see Betterlog::Log::EventFormatter#format
6
15
  class EventFormatter
7
16
  include Term::ANSIColor
8
17
  include ComplexConfig::Provider::Shortcuts
9
18
 
19
+ # Initializes a new EventFormatter instance with the given log event.
20
+ #
21
+ # This constructor sets up the formatter with a specific log event to be
22
+ # formatted, preparing it for subsequent formatting operations such as
23
+ # JSON generation or pretty printing with color support.
24
+ #
25
+ # @param event [Betterlog::Log::Event] the log event to be formatted by
26
+ # this instance
27
+ # @return [Betterlog::Log::EventFormatter] a new EventFormatter instance
10
28
  def initialize(event)
11
29
  @event = event
12
30
  end
13
31
 
32
+ # Formats the log event according to specified options.
33
+ #
34
+ # This method processes a log event and returns either a JSON
35
+ # representation or a formatted string based on the provided formatting
36
+ # options. It supports pretty printing with custom format patterns and
37
+ # optional colorization.
38
+ #
39
+ # @param pretty [ Boolean, Symbol ] when true, enables pretty printing;
40
+ # when :format, uses a specific format pattern; otherwise uses JSON generation
41
+ # @param color [ Boolean ] whether to apply color formatting to the output
42
+ # @param format [ Symbol ] the format identifier to use for pretty printing
43
+ #
44
+ # @return [ String ] the formatted log event as a string
45
+ #
46
+ # @see Betterlog::Log::EventFormatter#format_pattern
47
+ # @see JSON.generate
14
48
  def format(pretty: false, color: false, format: :default)
15
49
  old_coloring, Term::ANSIColor.coloring = Term::ANSIColor.coloring?, color
16
50
  f = cc.log.formats[format] and format = f
@@ -26,6 +60,20 @@ module Betterlog
26
60
 
27
61
  private
28
62
 
63
+ # Colorizes a string value based on configured styles for a given key and
64
+ # optional value.
65
+ #
66
+ # This method applies visual styling to a string by looking up the
67
+ # appropriate style configuration based on the provided key and value,
68
+ # then applying that style using the internal apply_style method.
69
+ #
70
+ # @param key [ Object ] the lookup key for determining the style
71
+ # configuration
72
+ # @param value [ Object ] the value used to determine which specific
73
+ # style to apply
74
+ # @param string [ Object ] the string to be colorized, defaults to the
75
+ # key if not provided
76
+ # @return [ String ] the colorized string based on the configured styles
29
77
  def colorize(key, value, string = key)
30
78
  case style = cc.log.styles[key]
31
79
  when nil, String, Array
@@ -35,6 +83,19 @@ module Betterlog
35
83
  end
36
84
  end
37
85
 
86
+ # Applies visual styling to a string using the provided style
87
+ # configuration.
88
+ #
89
+ # This method takes a style definition and applies it to a given string,
90
+ # supporting both single styles and arrays of styles. It handles the case
91
+ # where no style is provided by returning the string with reset
92
+ # formatting. When multiple styles are specified, they are applied
93
+ # sequentially to the string.
94
+ #
95
+ # @param style [ Object ] The style configuration to apply, which can be nil,
96
+ # a string, an array of styles, or a ComplexConfig::Settings object
97
+ # @param string [ String ] The input string to which the style will be applied
98
+ # @return [ String ] The styled string with appropriate ANSI color codes inserted
38
99
  def apply_style(style, string)
39
100
  style.nil? and return string + Term::ANSIColor.reset
40
101
  string = Term::ANSIColor.uncolor(string)
@@ -48,6 +109,18 @@ module Betterlog
48
109
  end
49
110
  end
50
111
 
112
+ # Formats a log event using a specified pattern with support for
113
+ # directives and colorization.
114
+ #
115
+ # This method processes a format string by replacing placeholders with
116
+ # actual event data, applying formatting directives for special handling
117
+ # of values like objects or timestamps, and optionally applying color
118
+ # styling based on configured styles.
119
+ #
120
+ # @param format [ String ] the format pattern to apply to the log event
121
+ # @return [ String ] the formatted string representation of the log event
122
+ # @see Betterlog::Log::EventFormatter#format_object
123
+ # @see Betterlog::Log::EventFormatter#colorize
51
124
  def format_pattern(format:)
52
125
  format.
53
126
  gsub('\n', "\n").
@@ -99,6 +172,23 @@ module Betterlog
99
172
  }
100
173
  end
101
174
 
175
+ # Formats complex objects into a readable string representation with
176
+ # nested structure visualization.
177
+ #
178
+ # This method recursively processes arrays and hashes, converting them
179
+ # into indented string representations that show the hierarchical
180
+ # structure of the data. It handles nested arrays and hashes by
181
+ # increasing the indentation level for each nesting level, making it
182
+ # easier to visualize the data structure.
183
+ #
184
+ # @param object [ Object ] the object to be formatted, which can be an
185
+ # array, hash, or any other object
186
+ # @param depth [ Integer ] the current depth level for indentation, used
187
+ # internally during recursion
188
+ # @param nl [ String ] the newline character or string to use for
189
+ # separating elements
190
+ # @return [ String ] a formatted string representation of the object,
191
+ # with proper indentation for nested structures
102
192
  def format_object(object, depth: 0, nl: ?\n)
103
193
  case
104
194
  when a = object.ask_and_send(:to_ary)
@@ -2,14 +2,44 @@ require 'term/ansicolor'
2
2
 
3
3
  module Betterlog
4
4
  class Log
5
+ # Formats log messages for legacy Rails logging compatibility.
6
+ #
7
+ # This class provides a formatter that integrates with Rails' legacy
8
+ # logging system, converting standard log messages into structured JSON
9
+ # events while preserving the original formatting behavior for backward
10
+ # compatibility.
11
+ #
12
+ # @see Betterlog::Log::Event
13
+ # @see ActiveSupport::Logger::Formatter
5
14
  class LegacyEventFormatter < ::ActiveSupport::Logger::Formatter
6
15
  include ActiveSupport::TaggedLogging::Formatter
7
16
  include ComplexConfig::Provider::Shortcuts
8
17
 
18
+ # Returns the emitter identifier string for legacy log events.
19
+ #
20
+ # This method provides a constant string value that represents the
21
+ # emitter type for logs formatted using the legacy event formatter.
22
+ #
23
+ # @return [ String ] the string 'legacy' indicating the legacy emitter type
9
24
  def emitter
10
25
  'legacy'
11
26
  end
12
27
 
28
+ # Processes a log message using the legacy formatting approach.
29
+ #
30
+ # This method handles the conversion of standard log messages into
31
+ # structured JSON events when legacy logging support is enabled. It
32
+ # extracts relevant information from the input message, such as
33
+ # backtraces and location data, and formats it according to the legacy
34
+ # event structure.
35
+ #
36
+ # @param severity [ String ] the severity level of the log message
37
+ # @param timestamp [ Time ] the time when the log entry was created
38
+ # @param program [ String ] the name of the program generating the log
39
+ # @param message [ String ] the raw log message content
40
+ #
41
+ # @return [ String ] the formatted log message, either as a JSON event or
42
+ # the original message if it's not suitable for conversion
13
43
  def call(severity, timestamp, program, message)
14
44
  if cc.log.legacy_supported
15
45
  if message.blank?
@@ -2,13 +2,42 @@ require 'logger'
2
2
 
3
3
  module Betterlog
4
4
  class Log
5
+ # A severity level representation for logging events.
6
+ #
7
+ # This class provides a structured way to handle logging severity levels,
8
+ # ensuring consistent formatting and comparison of severity values. It
9
+ # integrates with Ruby's standard Logger::Severity constants while
10
+ # providing additional functionality for normalization, caching, and
11
+ # comparison operations.
12
+ #
13
+ # @see Betterlog::Log::Event
14
+ # @see Logger::Severity
5
15
  class Severity
6
16
  include ::Logger::Severity
7
17
  include Comparable
8
18
 
9
19
  class << self
20
+
21
+ # Defines a thread-local variable named shared within the class.
22
+ #
23
+ # This method sets up a thread-local storage area called shared,
24
+ # which can be used to store data that is unique to each thread.
25
+ # It is typically used to maintain state or caches that should
26
+ # not be shared across different threads in a multi-threaded environment.
10
27
  thread_local :shared
11
28
 
29
+ # Creates a new Severity instance with the given name, normalizing it
30
+ # to uppercase symbols for consistent lookup.
31
+ #
32
+ # This method converts the input name to a standardized format by
33
+ # converting it to a symbol, uppercasing its string representation, and
34
+ # then attempting to retrieve or create a corresponding Severity
35
+ # constant. It ensures that only one instance exists per unique
36
+ # severity name by using a shared cache.
37
+ #
38
+ # @param name [ String, Symbol, Betterlog::Log::Severity ] the severity
39
+ # name to initialize with
40
+ # @return [ Betterlog::Log::Severity ] a new or cached severity instance
12
41
  def new(name)
13
42
  name = name.to_sym if self.class === name
14
43
  name = name.to_s.upcase.to_sym
@@ -17,6 +46,17 @@ module Betterlog
17
46
  end
18
47
  end
19
48
 
49
+ # Initializes a new Severity instance with the given name.
50
+ #
51
+ # This constructor creates a severity level object by converting the
52
+ # input name to a standardized symbol format and attempting to map it to
53
+ # a corresponding logger severity constant. If the constant is not found,
54
+ # it defaults to the UNKNOWN severity level.
55
+ #
56
+ # @param name [ String, Symbol, Betterlog::Log::Severity ] the severity name
57
+ # to initialize with, which will be converted to uppercase and normalized
58
+ # to a symbol for lookup
59
+ # @return [ Betterlog::Log::Severity ] a new severity instance
20
60
  def initialize(name)
21
61
  name = name.to_sym if self.class === name
22
62
  @name = name.to_s.downcase.to_sym
@@ -28,36 +68,100 @@ module Betterlog
28
68
  end
29
69
  end
30
70
 
71
+ # Returns an array of all Severity constants as initialized objects.
72
+ #
73
+ # This method retrieves all available severity constants defined in the
74
+ # class and creates a new instance of Severity for each one. The results
75
+ # are cached in an instance variable to avoid re-computation on
76
+ # subsequent calls.
77
+ #
78
+ # @return [ Array<Betterlog::Log::Severity> ] an array containing Severity
79
+ # instances for each constant defined in the class
31
80
  def self.all
32
81
  @all_constants ||= constants.map { |c| new(c) }
33
82
  end
34
83
 
84
+ # Converts the severity level to its integer representation.
85
+ #
86
+ # This method returns the underlying integer value that represents the
87
+ # severity level, which corresponds to standard logging severity
88
+ # constants.
89
+ #
90
+ # @return [ Integer ] the integer value of the severity level
35
91
  def to_i
36
92
  @level
37
93
  end
38
94
 
95
+ # Converts the severity name to an uppercase string representation.
96
+ #
97
+ # This method returns a string version of the severity name, converted to
98
+ # uppercase for consistent formatting and display purposes.
99
+ #
100
+ # @return [ String ] the uppercase string representation of the severity name
39
101
  def to_s
40
102
  @name.to_s.upcase
41
103
  end
42
104
 
105
+ # Converts the severity name to a symbol representation.
106
+ #
107
+ # This method returns the internal symbol representation of the severity
108
+ # name, which is used for consistent identification and comparison of
109
+ # severity levels.
110
+ #
111
+ # @return [ Symbol ] the symbol representation of the severity name
43
112
  def to_sym
44
113
  @name
45
114
  end
46
115
 
116
+ # Converts the log event to a JSON-compatible hash representation.
117
+ #
118
+ # This method provides a way to serialize the log event data into a
119
+ # format suitable for JSON generation, ensuring that the event's data can
120
+ # be easily converted to a JSON string while maintaining its structure
121
+ # and content.
122
+ #
123
+ # @return [ Hash ] a hash representation of the log event data
47
124
  def as_json(*)
48
125
  to_s
49
126
  end
50
127
 
128
+ # Compares this severity instance with another value for ordering.
129
+ #
130
+ # This method enables sorting and comparison of severity levels by
131
+ # converting both the current instance and the provided value to their
132
+ # integer representations and performing a standard numeric comparison.
133
+ #
134
+ # @param other [ Object ] the value to compare against, which will be converted
135
+ # to a Severity instance for comparison
136
+ # @return [ Integer ] -1 if this instance is less than the other, 0 if they are
137
+ # equal, 1 if this instance is greater than the other
51
138
  def <=>(other)
52
139
  to_i <=> self.class.new(other).to_i
53
140
  end
54
141
 
142
+ # Checks equality between this severity instance and another object based
143
+ # on their symbol representations.
144
+ #
145
+ # This method compares the symbol representation of the current severity
146
+ # instance with that of another object to determine if they are
147
+ # equivalent.
148
+ #
149
+ # @param other [ Object ] the object to compare against
150
+ # @return [ TrueClass, FalseClass ] true if both objects have equal
151
+ # symbol representations, false otherwise
55
152
  def eql?(other)
56
153
  to_sym == other.to_sym
57
154
  end
58
155
 
59
156
  alias == eql?
60
157
 
158
+ # Returns the hash value corresponding to the severity name symbol.
159
+ #
160
+ # This method provides a hash representation of the internal symbol
161
+ # identifier for the severity level, which is used for consistent
162
+ # identification and comparison operations within the logging system.
163
+ #
164
+ # @return [ Integer ] the hash value of the severity name symbol
61
165
  def hash
62
166
  @name.hash
63
167
  end