Class: Rospatent::Logger
- Inherits:
-
Object
- Object
- Rospatent::Logger
- Defined in:
- lib/rospatent/logger.rb
Overview
Structured logger for API requests, responses, and events
Constant Summary collapse
- LEVELS =
{ debug: ::Logger::DEBUG, info: ::Logger::INFO, warn: ::Logger::WARN, error: ::Logger::ERROR, fatal: ::Logger::FATAL }.freeze
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#debug(message, data = {}) ⇒ Object
Log debug information.
-
#error(message, data = {}) ⇒ Object
Log error message.
-
#fatal(message, data = {}) ⇒ Object
Log fatal error.
-
#info(message, data = {}) ⇒ Object
Log info message.
-
#initialize(output: $stdout, level: :info, formatter: :text) ⇒ Logger
constructor
Initialize a new logger.
-
#log_cache(operation, key, ttl: nil) ⇒ Object
Log cache operations.
-
#log_error(error, context = {}) ⇒ Object
Log an error with context.
-
#log_performance(operation, duration, metadata = {}) ⇒ Object
Log performance metrics.
-
#log_request(method, endpoint, params = {}, headers = {}) ⇒ Object
Log an API request.
-
#log_response(method, endpoint, status, duration, response_size: nil, request_id: nil) ⇒ Object
Log an API response.
-
#warn(message, data = {}) ⇒ Object
Log warning.
Constructor Details
#initialize(output: $stdout, level: :info, formatter: :text) ⇒ Logger
Initialize a new logger
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rospatent/logger.rb', line 24 def initialize(output: $stdout, level: :info, formatter: :text) @logger = ::Logger.new(output) @level = level @logger.level = LEVELS[level] || ::Logger::INFO @logger.formatter = case formatter when :json method(:json_formatter) else method(:text_formatter) end end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
18 19 20 |
# File 'lib/rospatent/logger.rb', line 18 def level @level end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
18 19 20 |
# File 'lib/rospatent/logger.rb', line 18 def logger @logger end |
Instance Method Details
#debug(message, data = {}) ⇒ Object
Log debug information
127 128 129 |
# File 'lib/rospatent/logger.rb', line 127 def debug(, data = {}) log_structured(:debug, , data) end |
#error(message, data = {}) ⇒ Object
Log error message
148 149 150 |
# File 'lib/rospatent/logger.rb', line 148 def error(, data = {}) log_structured(:error, , data) end |
#fatal(message, data = {}) ⇒ Object
Log fatal error
155 156 157 |
# File 'lib/rospatent/logger.rb', line 155 def fatal(, data = {}) log_structured(:fatal, , data) end |
#info(message, data = {}) ⇒ Object
Log info message
134 135 136 |
# File 'lib/rospatent/logger.rb', line 134 def info(, data = {}) log_structured(:info, , data) end |
#log_cache(operation, key, ttl: nil) ⇒ Object
Log cache operations
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rospatent/logger.rb', line 98 def log_cache(operation, key, ttl: nil) return unless should_log?(:debug) log_structured(:debug, "Cache operation", { operation: operation, cache_key: key, ttl_seconds: ttl, timestamp: Time.now.iso8601 }) end |
#log_error(error, context = {}) ⇒ Object
Log an error with context
84 85 86 87 88 89 90 91 92 |
# File 'lib/rospatent/logger.rb', line 84 def log_error(error, context = {}) log_structured(:error, "Error occurred", { error_class: error.class.name, error_message: error., error_backtrace: error.backtrace&.first(10), context: context, timestamp: Time.now.iso8601 }) end |
#log_performance(operation, duration, metadata = {}) ⇒ Object
Log performance metrics
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rospatent/logger.rb', line 113 def log_performance(operation, duration, = {}) return unless should_log?(:info) log_structured(:info, "Performance metric", { operation: operation, duration_ms: (duration * 1000).round(2), metadata: , timestamp: Time.now.iso8601 }) end |
#log_request(method, endpoint, params = {}, headers = {}) ⇒ Object
Log an API request
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rospatent/logger.rb', line 42 def log_request(method, endpoint, params = {}, headers = {}) return unless should_log?(:info) safe_params = sanitize_params(params) safe_headers = sanitize_headers(headers) log_structured(:info, "API Request", { http_method: method.upcase, endpoint: endpoint, params: safe_params, headers: safe_headers, timestamp: Time.now.iso8601, request_id: generate_request_id }) end |
#log_response(method, endpoint, status, duration, response_size: nil, request_id: nil) ⇒ Object
Log an API response
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rospatent/logger.rb', line 65 def log_response(method, endpoint, status, duration, response_size: nil, request_id: nil) return unless should_log?(:info) level = status >= 400 ? :warn : :info log_structured(level, "API Response", { http_method: method.upcase, endpoint: endpoint, status_code: status, duration_ms: (duration * 1000).round(2), response_size_bytes: response_size, timestamp: Time.now.iso8601, request_id: request_id }) end |
#warn(message, data = {}) ⇒ Object
Log warning
141 142 143 |
# File 'lib/rospatent/logger.rb', line 141 def warn(, data = {}) log_structured(:warn, , data) end |