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 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rospatent/logger.rb', line 24 def initialize(output: $stdout, level: :info, formatter: :text) @level = level # Handle different types of output @logger = if output.respond_to?(:debug) && output.respond_to?(:info) && output.respond_to?(:error) # If it's already a logger instance (like Rails.logger), use it directly output else # If it's an IO object or file path, create a new Logger new_logger = ::Logger.new(output) new_logger.formatter = case formatter when :json method(:json_formatter) else method(:text_formatter) end new_logger end # Set the log level @logger.level = LEVELS[level] || ::Logger::INFO 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
137 138 139 |
# File 'lib/rospatent/logger.rb', line 137 def debug(, data = {}) log_structured(:debug, , data) end |
#error(message, data = {}) ⇒ Object
Log error message
158 159 160 |
# File 'lib/rospatent/logger.rb', line 158 def error(, data = {}) log_structured(:error, , data) end |
#fatal(message, data = {}) ⇒ Object
Log fatal error
165 166 167 |
# File 'lib/rospatent/logger.rb', line 165 def fatal(, data = {}) log_structured(:fatal, , data) end |
#info(message, data = {}) ⇒ Object
Log info message
144 145 146 |
# File 'lib/rospatent/logger.rb', line 144 def info(, data = {}) log_structured(:info, , data) end |
#log_cache(operation, key, ttl: nil) ⇒ Object
Log cache operations
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rospatent/logger.rb', line 108 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
94 95 96 97 98 99 100 101 102 |
# File 'lib/rospatent/logger.rb', line 94 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
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rospatent/logger.rb', line 123 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rospatent/logger.rb', line 52 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rospatent/logger.rb', line 75 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
151 152 153 |
# File 'lib/rospatent/logger.rb', line 151 def warn(, data = {}) log_structured(:warn, , data) end |