Class: UU::LoggerFluent::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/uu/logger_fluent.rb

Constant Summary collapse

DEFAULT_TAG =
'fluentd'
PATHS =
%w[
  /log.rb
  /logger.rb
  /loggable.rb
  /forwardable.rb
].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Formatter

Returns a new instance of Formatter.



14
15
16
# File 'lib/uu/logger_fluent.rb', line 14

def initialize(context)
  @context = context
end

Class Attribute Details

.tagObject

Returns the value of attribute tag.



11
12
13
# File 'lib/uu/logger_fluent.rb', line 11

def tag
  @tag
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



18
19
20
# File 'lib/uu/logger_fluent.rb', line 18

def context
  @context
end

Instance Method Details

#call(severity, msg) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/uu/logger_fluent.rb', line 20

def call(severity, msg)
  [self.class.tag, {
    log_level: severity,
    **,
    **@context.context,
    **(msg.is_a?(Hash) ? msg : { message: msg }),
  }]
end

#find_locationObject



45
46
47
48
49
50
# File 'lib/uu/logger_fluent.rb', line 45

def find_location
  caller_locations.find do |location_|
    location_.path != __FILE__ &&
      PATHS.none? { |path| location_.path.end_with?(path) }
  end
end

#metadataObject



29
30
31
32
33
34
35
36
# File 'lib/uu/logger_fluent.rb', line 29

def 
  location = find_location
  {
    filename: File.basename(location.path),
    method: location.label,
    lineno: location.lineno,
  }
end