Class: Loggun::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/loggun/config.rb

Overview

Class for configurations

Defined Under Namespace

Classes: FailureConfiguration

Constant Summary collapse

DEFAULTS =
{
  pattern: '%{time} - %{pid} %{severity} %{type} %{tags_text} %{message}',
  parent_transaction_to_message: true,
  message_format: :json,
  log_format: :plain,
  force_utc: false,
  precision: :milliseconds,
  incoming_http: {
    controllers: %w[ApplicationController],
    success_condition: -> { response.code == '200' },
    error_info: -> { nil }
  },
  active_record: {
    log_subscriber_class_name: '::Loggun::Modifiers::ActiveRecord::LoggunLogSubscriber',
    payload_keys: %i[sql name duration source]
  }
}.freeze
DEFAULT_MODIFIERS =
%i[rails active_record sidekiq clockwork outgoing_http].freeze
MESSAGE_FORMATS =
%i[json key_value].freeze
LOG_FORMATS =
%i[json plain].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/loggun/config.rb', line 43

def initialize
  @formatter = Loggun::Formatter.new
  @precision = DEFAULTS[:precision]
  @pattern = DEFAULTS[:pattern]
  @parent_transaction_to_message = DEFAULTS[:parent_transaction_to_message]
  @message_format = DEFAULTS[:message_format]
  @log_format = DEFAULTS[:log_format]
  @force_utc = DEFAULTS[:force_utc]
  @modifiers = Loggun::OrderedOptions.new
  @custom_modifiers = []
  @exclude_keys = []
  @only_keys = []
  set_default_modifiers
end

Instance Attribute Details

#custom_modifiersObject

Returns the value of attribute custom_modifiers.



29
30
31
# File 'lib/loggun/config.rb', line 29

def custom_modifiers
  @custom_modifiers
end

#exclude_keysObject

Returns the value of attribute exclude_keys.



29
30
31
# File 'lib/loggun/config.rb', line 29

def exclude_keys
  @exclude_keys
end

#force_utcObject

Returns the value of attribute force_utc.



29
30
31
# File 'lib/loggun/config.rb', line 29

def force_utc
  @force_utc
end

#formatterObject

Returns the value of attribute formatter.



29
30
31
# File 'lib/loggun/config.rb', line 29

def formatter
  @formatter
end

#log_formatObject

Returns the value of attribute log_format.



29
30
31
# File 'lib/loggun/config.rb', line 29

def log_format
  @log_format
end

#message_formatObject

Returns the value of attribute message_format.



29
30
31
# File 'lib/loggun/config.rb', line 29

def message_format
  @message_format
end

#modifiersObject

Returns the value of attribute modifiers.



29
30
31
# File 'lib/loggun/config.rb', line 29

def modifiers
  @modifiers
end

#only_keysObject

Returns the value of attribute only_keys.



29
30
31
# File 'lib/loggun/config.rb', line 29

def only_keys
  @only_keys
end

#parent_transaction_to_messageObject

Returns the value of attribute parent_transaction_to_message.



29
30
31
# File 'lib/loggun/config.rb', line 29

def parent_transaction_to_message
  @parent_transaction_to_message
end

#patternObject

Returns the value of attribute pattern.



29
30
31
# File 'lib/loggun/config.rb', line 29

def pattern
  @pattern
end

#precisionObject

Returns the value of attribute precision.



29
30
31
# File 'lib/loggun/config.rb', line 29

def precision
  @precision
end

Class Method Details

.check_configObject



78
79
80
81
82
83
84
85
86
# File 'lib/loggun/config.rb', line 78

def check_config
  unless MESSAGE_FORMATS.include? instance.message_format
    raise FailureConfiguration, 'Unknown value for message_format'
  end

  unless LOG_FORMATS.include? instance.log_format
    raise FailureConfiguration, 'Unknown value for log_format'
  end
end

.configure(&block) ⇒ Object



59
60
61
62
63
64
# File 'lib/loggun/config.rb', line 59

def configure(&block)
  block.call(instance)
  use_modifiers
  check_config
  instance
end

.setup_formatter(app, formatter = nil) ⇒ Object



88
89
90
91
# File 'lib/loggun/config.rb', line 88

def setup_formatter(app, formatter = nil)
  Loggun.logger = app.logger
  Loggun.logger.formatter = formatter || instance.formatter
end

.use_modifiersObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/loggun/config.rb', line 66

def use_modifiers
  DEFAULT_MODIFIERS.each do |modifier|
    next unless instance.modifiers.public_send(modifier)&.enable

    require_relative "modifiers/#{modifier}"
    klass = Loggun::Modifiers.const_get(modifier.to_s.camelize)
    klass.use
  end

  instance.custom_modifiers.each(&:use)
end

Instance Method Details

#add_modifier(modifier) ⇒ Object



104
105
106
107
108
# File 'lib/loggun/config.rb', line 104

def add_modifier(modifier)
  return unless modifier.respond_to? :use

  custom_modifiers << modifier
end

#set_default_modifiersObject



94
95
96
97
98
99
100
101
102
# File 'lib/loggun/config.rb', line 94

def set_default_modifiers
  DEFAULT_MODIFIERS.each do |modifier|
    modifiers[modifier] = Loggun::OrderedOptions.new
    modifiers[modifier].enable = false
    next unless DEFAULTS[modifier].is_a?(Hash)

    modifiers[modifier].merge!(DEFAULTS[modifier])
  end
end

#timestamp_precisionObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/loggun/config.rb', line 110

def timestamp_precision
  case precision
  when :sec, :seconds then 0
  when :millis, :milliseconds, :ms then 3
  when :micros, :microseconds, :us then 6
  when :nanos, :nanoseconds, :ns then 9
  else
    3 # milliseconds
  end
end