Class: AwesomeFluentLogger::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/awesome_fluent_logger/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fluent: nil, level: DEBUG, progname: nil, formatter: nil, datetime_format: nil) ⇒ Logger

Returns a new instance of Logger.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/awesome_fluent_logger/logger.rb', line 9

def initialize(fluent: nil, level: DEBUG, progname: nil, formatter: nil, datetime_format: nil)
  super(nil, 0, 0, level: level, progname: progname, formatter: formatter, datetime_format: datetime_format)

  if fluent.is_a?(Hash)
    tag_prefix = fluent.fetch(:tag_prefix, nil)
    @logger = ::Fluent::Logger::FluentLogger.new(tag_prefix, **fluent)
  elsif fluent.respond_to?(:post)
    @logger = fluent
  else
    raise ArgumentError
  end

  @default_formatter = Formatter.new
  @default_formatter.datetime_format = datetime_format
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/awesome_fluent_logger/logger.rb', line 8

def logger
  @logger
end

Instance Method Details

#<<(msg) ⇒ Object



58
59
60
# File 'lib/awesome_fluent_logger/logger.rb', line 58

def <<(msg)
  @logger&.post(nil, msg)
end

#add(severity, message = nil, progname = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/awesome_fluent_logger/logger.rb', line 25

def add(severity, message = nil, progname = nil)
  severity ||= UNKNOWN

  if @logger.nil? or severity < level
    return true
  end

  if progname.nil?
    progname = @progname
  end
  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      progname = @progname
    end
  end

  formatted_severity = format_severity(severity)

  data = format_message(formatted_severity, Time.now, progname, message)

  unless data.is_a?(Hash)
    data = {data: data}
  end

  tag = [progname&.+('.'), formatted_severity.downcase].join

  @logger.post(tag, data)
  true
end

#closeObject



62
63
64
# File 'lib/awesome_fluent_logger/logger.rb', line 62

def close
  @logger&.close
end