Class: Makit::Logging::LogRequest
- Inherits:
-
Object
- Object
- Makit::Logging::LogRequest
- Defined in:
- lib/makit/logging/log_request.rb
Overview
Represents a log request that flows through the logging middleware chain
This class encapsulates all the information needed for a log entry, including the log level, message, context, and metadata. It provides a clean interface for middleware to process and modify log information.
Instance Attribute Summary collapse
-
#context ⇒ Hash
readonly
Additional context information.
-
#level ⇒ Symbol
readonly
The log level (:debug, :info, :warn, :error, :fatal, :success).
-
#message ⇒ String
readonly
The log message.
-
#metadata ⇒ Hash
readonly
Metadata about the log entry.
-
#timestamp ⇒ Time
readonly
When the log request was created.
-
#verbosity ⇒ Symbol
readonly
The verbosity level (:quiet, :normal, :verbose, :debug).
Instance Method Summary collapse
-
#add_metadata(data) ⇒ void
Add metadata to the log request.
-
#error? ⇒ Boolean
Check if this is an error level log.
-
#info? ⇒ Boolean
Check if this is an info level log.
-
#initialize(level, message, context = {}, verbosity: :normal) ⇒ LogRequest
constructor
Initialize a new log request.
-
#success? ⇒ Boolean
Check if this is a success level log.
-
#to_h ⇒ Hash
Convert the log request to a hash representation.
-
#to_json(*_args) ⇒ String
Convert the log request to a JSON string.
-
#to_s ⇒ String
Get a formatted string representation.
-
#warn? ⇒ Boolean
Check if this is a warning level log.
Constructor Details
#initialize(level, message, context = {}, verbosity: :normal) ⇒ LogRequest
Initialize a new log request
45 46 47 48 49 50 51 52 |
# File 'lib/makit/logging/log_request.rb', line 45 def initialize(level, , context = {}, verbosity: :normal) @level = level = @context = context.dup = {} = Time.now @verbosity = verbosity end |
Instance Attribute Details
#context ⇒ Hash (readonly)
Returns additional context information.
29 30 31 |
# File 'lib/makit/logging/log_request.rb', line 29 def context @context end |
#level ⇒ Symbol (readonly)
Returns the log level (:debug, :info, :warn, :error, :fatal, :success).
23 24 25 |
# File 'lib/makit/logging/log_request.rb', line 23 def level @level end |
#message ⇒ String (readonly)
Returns the log message.
26 27 28 |
# File 'lib/makit/logging/log_request.rb', line 26 def end |
#metadata ⇒ Hash (readonly)
Returns metadata about the log entry.
32 33 34 |
# File 'lib/makit/logging/log_request.rb', line 32 def end |
#timestamp ⇒ Time (readonly)
Returns when the log request was created.
35 36 37 |
# File 'lib/makit/logging/log_request.rb', line 35 def end |
#verbosity ⇒ Symbol (readonly)
Returns the verbosity level (:quiet, :normal, :verbose, :debug).
37 38 39 |
# File 'lib/makit/logging/log_request.rb', line 37 def verbosity @verbosity end |
Instance Method Details
#add_metadata(data) ⇒ void
This method returns an undefined value.
Add metadata to the log request
58 59 60 |
# File 'lib/makit/logging/log_request.rb', line 58 def (data) .merge!(data) end |
#error? ⇒ Boolean
Check if this is an error level log
93 94 95 |
# File 'lib/makit/logging/log_request.rb', line 93 def error? @level == :error || @level == :fatal end |
#info? ⇒ Boolean
Check if this is an info level log
107 108 109 |
# File 'lib/makit/logging/log_request.rb', line 107 def info? @level == :info end |
#success? ⇒ Boolean
Check if this is a success level log
100 101 102 |
# File 'lib/makit/logging/log_request.rb', line 100 def success? @level == :success end |
#to_h ⇒ Hash
Convert the log request to a hash representation
65 66 67 68 69 70 71 72 73 |
# File 'lib/makit/logging/log_request.rb', line 65 def to_h { level: @level, message: , context: @context, metadata: , timestamp: .iso8601, } end |
#to_json(*_args) ⇒ String
Convert the log request to a JSON string
78 79 80 81 |
# File 'lib/makit/logging/log_request.rb', line 78 def to_json(*_args) require "json" JSON.generate(to_h) end |
#to_s ⇒ String
Get a formatted string representation
86 87 88 |
# File 'lib/makit/logging/log_request.rb', line 86 def to_s "[#{@timestamp.strftime("%Y-%m-%d %H:%M:%S")}] #{@level.to_s.upcase}: #{@message}" end |
#warn? ⇒ Boolean
Check if this is a warning level log
114 115 116 |
# File 'lib/makit/logging/log_request.rb', line 114 def warn? @level == :warn end |