Class: ActFluentLoggerRails::FluentLogger

Inherits:
ActiveSupport::Logger
  • Object
show all
Defined in:
lib/act-fluent-logger-rails/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, level, log_tags) ⇒ FluentLogger

Returns a new instance of FluentLogger.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/act-fluent-logger-rails/logger.rb', line 77

def initialize(options, level, log_tags)
  self.level = level
  port    = options[:port]
  host    = options[:host]
  nanosecond_precision = options[:nanosecond_precision]
  @messages_type = (options[:messages_type] || :array).to_sym
  @tag = options[:tag]
  @severity_key = (options[:severity_key] || :severity).to_sym
  @flush_immediately = options[:flush_immediately]
  logger_opts = {host: host, port: port, nanosecond_precision: nanosecond_precision}
  @fluent_logger = ::Fluent::Logger::FluentLogger.new(nil, logger_opts)
  @severity = 0
  @messages = []
  @log_tags = log_tags
  @map = {}
  after_initialize if respond_to? :after_initialize
end

Instance Method Details

#[](key) ⇒ Object



126
127
128
# File 'lib/act-fluent-logger-rails/logger.rb', line 126

def [](key)
  @map[key]
end

#[]=(key, value) ⇒ Object



130
131
132
# File 'lib/act-fluent-logger-rails/logger.rb', line 130

def []=(key, value)
  @map[key] = value
end

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



95
96
97
98
99
100
101
# File 'lib/act-fluent-logger-rails/logger.rb', line 95

def add(severity, message = nil, progname = nil, &block)
  return true if severity < level
  message = (block_given? ? block.call : progname) if message.blank?
  return true if message.blank?
  add_message(severity, message)
  true
end

#add_message(severity, message) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/act-fluent-logger-rails/logger.rb', line 103

def add_message(severity, message)
  @severity = severity if @severity < severity

  message =
    case message
    when ::String
      message
    when ::Exception
      "#{ message.message } (#{ message.class })\n" <<
        (message.backtrace || []).join("\n")
    else
      message.inspect
    end

  if message.encoding == Encoding::UTF_8
    @messages << message
  else
    @messages << message.dup.force_encoding(Encoding::UTF_8)
  end

  flush if @flush_immediately
end

#closeObject



155
156
157
# File 'lib/act-fluent-logger-rails/logger.rb', line 155

def close
  @fluent_logger.close
end

#flushObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/act-fluent-logger-rails/logger.rb', line 134

def flush
  return if @messages.empty?
  messages = if @messages_type == :string
               @messages.join("\n")
             else
               @messages
             end
  @map[:messages] = messages
  @map[@severity_key] = format_severity(@severity)
  if @tags
    @log_tags.keys.zip(@tags).each do |k, v|
      @map[k] = v
    end
  end
  @fluent_logger.post(@tag, @map)
  @severity = 0
  @messages.clear
  @tags = nil
  @map.clear
end

#format_severity(severity) ⇒ Object



167
168
169
# File 'lib/act-fluent-logger-rails/logger.rb', line 167

def format_severity(severity)
  ActFluentLoggerRails::Logger::SEV_LABEL[severity] || 'ANY'
end

#levelObject



159
160
161
# File 'lib/act-fluent-logger-rails/logger.rb', line 159

def level
  @level
end

#level=(l) ⇒ Object



163
164
165
# File 'lib/act-fluent-logger-rails/logger.rb', line 163

def level=(l)
  @level = l
end