Class: Servus::Support::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/servus/support/logger.rb

Overview

Logger class for logging service calls and results

Class Method Summary collapse

Class Method Details

.log_call(service_class, args) ⇒ Object

Logs a call to a service.

When Config#log_filter_parameters is configured, matching argument values are replaced with [FILTERED] before logging. With the default empty list, arguments are logged verbatim.

Parameters:

  • service_class (Class) —

    The service class

  • args (Hash) —

    The arguments passed to the service



28
29
30
31
# File 'lib/servus/support/logger.rb', line 28

def self.log_call(service_class, args)
  rendered = log_parameters(args)
  logger.info("Calling #{service_class.name} with args: #{rendered.inspect}")
end

.log_event(event_name, payload, event_id:, duration_ms:) ⇒ Object

Logs an event emission with correlation ID and duration.

Parameters:

  • event_name (Symbol) —

    The event name

  • payload (Hash) —

    The event payload

  • event_id (String) —

    The unique event correlation ID

  • duration_ms (Float) —

    The dispatch duration in milliseconds



77
78
79
# File 'lib/servus/support/logger.rb', line 77

def self.log_event(event_name, payload, event_id:, duration_ms:)
  logger.info("[#{event_id}] Event :#{event_name} (#{duration_ms.round(1)}ms) #{payload.inspect}")
end

.log_exception(service_class, exception) ⇒ Object

Logs an uncaught exception from a service

Parameters:

  • service_class (Class) —

    The service class

  • exception (Exception) —

    The uncaught exception



93
94
95
# File 'lib/servus/support/logger.rb', line 93

def self.log_exception(service_class, exception)
  logger.error("#{service_class.name} uncaught exception: #{exception.class} - #{exception.message}")
end

.log_failure(service_class, error, duration) ⇒ Object

Logs a failed result from a service

Parameters:



59
60
61
# File 'lib/servus/support/logger.rb', line 59

def self.log_failure(service_class, error, duration)
  logger.warn("#{service_class.name} failed in #{duration.round(3)}s with error: #{error}")
end

.log_guard_failure(service_class, error) ⇒ Object

Logs a guard failure from a service

Parameters:



67
68
69
# File 'lib/servus/support/logger.rb', line 67

def self.log_guard_failure(service_class, error)
  logger.warn("#{service_class.name} guard failed: #{error.message}")
end

.log_job_class_conflict(service_class, const_name) ⇒ Object

Logs that a service could not publish its generated job class because the constant is already taken by the application.

The application's class is left alone. The service still runs, but its generated job has no name, so call_async on it cannot be serialized.

Parameters:

  • service_class (Class<Servus::Base>) —

    The service whose job was not published

  • const_name (String) —

    The constant the application already owns



115
116
117
118
119
120
121
# File 'lib/servus/support/logger.rb', line 115

def self.log_job_class_conflict(service_class, const_name)
  logger.warn(
    "#{service_class.name} did not publish its generated job as #{const_name} — that constant " \
    'is already defined by the application. The application class is unchanged; ' \
    "#{service_class.name}.call_async cannot be enqueued until the service or the job is renamed."
  )
end

.log_parameters(params) ⇒ Hash

Filters parameters for logging based on the configured filter list.

Parameters:

  • params (Hash) —

    The parameters to filter

Returns:

  • (Hash) —

    The filtered parameters



127
128
129
130
131
132
133
# File 'lib/servus/support/logger.rb', line 127

def self.log_parameters(params)
  if Servus.config.log_filter_parameters.empty?
    params
  else
    Servus.config.parameter_filter.filter(params)
  end
end

.log_result(service_class, result, duration) ⇒ Object

Logs a result from a service

Parameters:

  • service_class (Class) —

    The service class

  • result (Servus::Support::Response) —

    The result from the service

  • duration (Float) —

    The duration of the service call



38
39
40
41
42
43
44
# File 'lib/servus/support/logger.rb', line 38

def self.log_result(service_class, result, duration)
  if result.success?
    log_success(service_class, duration)
  else
    log_failure(service_class, result.error, duration)
  end
end

.log_schema_override(key) ⇒ Object

Logs that a registered schema fragment was replaced with a different value.

Expected during development reloads. Outside of that it usually means two libraries are claiming the same fragment key.

Parameters:

  • key (String) —

    The schema fragment key being overridden



103
104
105
# File 'lib/servus/support/logger.rb', line 103

def self.log_schema_override(key)
  logger.warn("Schema fragment #{key.inspect} was already registered with a different value; replacing it.")
end

.log_success(service_class, duration) ⇒ Object

Logs a successful result from a service

Parameters:

  • service_class (Class) —

    The service class

  • duration (Float) —

    The duration of the service call



50
51
52
# File 'lib/servus/support/logger.rb', line 50

def self.log_success(service_class, duration)
  logger.info("#{service_class.name} succeeded in #{duration.round(3)}s")
end

.log_validation_error(service_class, error) ⇒ Object

Logs a validation error from a service

Parameters:



85
86
87
# File 'lib/servus/support/logger.rb', line 85

def self.log_validation_error(service_class, error)
  logger.error("#{service_class.name} validation error: #{error.message}")
end

.logger ⇒ Logger

Returns the logger instance depending on the environment

Returns:

  • (Logger) —

    The logger instance



12
13
14
15
16
17
18
# File 'lib/servus/support/logger.rb', line 12

def self.logger
  if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
    Rails.logger
  else
    @logger ||= ::Logger.new($stdout)
  end
end