Class: Quonfig::SemanticLoggerFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/quonfig/semantic_logger_filter.rb

Overview

SemanticLogger filter that gates log output by a single Quonfig config whose rules target the logger via the quonfig.logger-name context property.

Usage:

filter = client.semantic_logger_filter(config_key: 'log-levels.my-app')
SemanticLogger.add_appender(io: $stdout, filter: filter)

The filter normalizes the SemanticLogger logger name to dotted snake_case (e.g. MyApp::Foo::Barmy_app.foo.bar) and exposes it to the evaluator under quonfig.logger-name so the customer’s Quonfig config can discriminate per-logger via PROP_STARTS_WITH_ONE_OF / PROP_IS_ONE_OF rules. Lookup is O(1): one client.get call per log line.

Constant Summary collapse

LEVELS =
{
  trace: 0,
  debug: 1,
  info:  2,
  warn:  3,
  error: 4,
  fatal: 5
}.freeze
LOGGER_NAME_CONTEXT_KEY =
'quonfig.logger-name'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, config_key:) ⇒ SemanticLoggerFilter

Returns a new instance of SemanticLoggerFilter.



33
34
35
36
37
38
39
40
# File 'lib/quonfig/semantic_logger_filter.rb', line 33

def initialize(client, config_key:)
  unless self.class.semantic_logger_loaded?
    raise LoadError, "semantic_logger gem is required for Quonfig::SemanticLoggerFilter. Add `gem 'semantic_logger'` to your Gemfile."
  end

  @client = client
  @config_key = config_key
end

Class Method Details

.semantic_logger_loaded?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/quonfig/semantic_logger_filter.rb', line 29

def self.semantic_logger_loaded?
  defined?(SemanticLogger)
end

Instance Method Details

#call(log) ⇒ Object

SemanticLogger filter contract: return true to emit, false to suppress. Missing config key → return true so SemanticLogger’s static level decides.



44
45
46
47
48
49
50
51
# File 'lib/quonfig/semantic_logger_filter.rb', line 44

def call(log)
  configured = @client.get(@config_key, nil, context_for(log))
  return true if configured.nil?

  log_severity = LEVELS[log.level] || LEVELS[:debug]
  min_severity = LEVELS[normalize_level(configured)] || LEVELS[:debug]
  log_severity >= min_severity
end

#normalize(name) ⇒ Object

Normalize a SemanticLogger logger name to the dotted snake_case form the customer writes targeting rules against.

MyApp::Foo::Bar  my_app.foo.bar
HTMLParser       html_parser


57
58
59
60
61
62
63
# File 'lib/quonfig/semantic_logger_filter.rb', line 57

def normalize(name)
  name.to_s
      .gsub('::', '.')
      .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
      .gsub(/([a-z\d])([A-Z])/, '\1_\2')
      .downcase
end