Module: JetstreamBridge::Logging

Defined in:
lib/jetstream_bridge/core/logging.rb

Overview

Logging helpers that route to the configured logger when available, falling back to Rails.logger or STDOUT.

Class Method Summary collapse

Class Method Details

.debug(msg, tag: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • msg (String)

    Message to log

  • tag (String, nil) (defaults to: nil)

    Optional prefix



44
45
46
# File 'lib/jetstream_bridge/core/logging.rb', line 44

def debug(msg, tag: nil)
  log(:debug, msg, tag: tag)
end

.default_loggerLogger

Returns a default STDOUT logger, memoized for reuse.

Returns:

  • (Logger)


26
27
28
# File 'lib/jetstream_bridge/core/logging.rb', line 26

def default_logger
  @default_logger ||= Logger.new($stdout)
end

.error(msg, tag: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • msg (String)

    Message to log

  • tag (String, nil) (defaults to: nil)

    Optional prefix



65
66
67
# File 'lib/jetstream_bridge/core/logging.rb', line 65

def error(msg, tag: nil)
  log(:error, msg, tag: tag)
end

.info(msg, tag: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • msg (String)

    Message to log

  • tag (String, nil) (defaults to: nil)

    Optional prefix



51
52
53
# File 'lib/jetstream_bridge/core/logging.rb', line 51

def info(msg, tag: nil)
  log(:info, msg, tag: tag)
end

.log(level, msg, tag: nil) ⇒ void

This method returns an undefined value.

Log a message at the given level with an optional tag prefix.

Parameters:

  • level (Symbol)

    Log level (:debug, :info, :warn, :error)

  • msg (String)

    Message to log

  • tag (String, nil) (defaults to: nil)

    Optional prefix (e.g. “JetstreamBridge::Consumer”)



36
37
38
39
# File 'lib/jetstream_bridge/core/logging.rb', line 36

def log(level, msg, tag: nil)
  message = tag ? "[#{tag}] #{msg}" : msg
  logger.public_send(level, message)
end

.loggerLogger

Returns the active logger instance.

Resolution order: configured logger, Rails.logger, STDOUT fallback.

Returns:

  • (Logger)

    Active logger



17
18
19
20
21
# File 'lib/jetstream_bridge/core/logging.rb', line 17

def logger
  JetstreamBridge.config.logger ||
    (defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger) ||
    default_logger
end

.sanitize_url(url) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jetstream_bridge/core/logging.rb', line 69

def sanitize_url(url)
  uri = URI.parse(url)
  return url unless uri.user || uri.password

  userinfo =
    if uri.password # user:pass → keep user, mask pass
      "#{uri.user}:***"
    else            # token-only userinfo → mask entirely
      '***'
    end

  host = uri.host || ''
  port = uri.port ? ":#{uri.port}" : ''
  path = uri.path.to_s # omit query on purpose to avoid leaking tokens
  frag = uri.fragment ? "##{uri.fragment}" : ''

  "#{uri.scheme}://#{userinfo}@#{host}#{port}#{path}#{frag}"
rescue URI::InvalidURIError
  # Fallback: redact any userinfo before the '@'
  url.gsub(%r{(nats|tls)://([^@/]+)@}i) do
    scheme = Regexp.last_match(1)
    creds  = Regexp.last_match(2)
    masked = creds&.include?(':') ? "#{creds&.split(':', 2)&.first}:***" : '***'
    "#{scheme}://#{masked}@"
  end
end

.warn(msg, tag: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • msg (String)

    Message to log

  • tag (String, nil) (defaults to: nil)

    Optional prefix



58
59
60
# File 'lib/jetstream_bridge/core/logging.rb', line 58

def warn(msg, tag: nil)
  log(:warn, msg, tag: tag)
end