lib/uu/logger_fluent.rb in uu-0.2.0 vs lib/uu/logger_fluent.rb in uu-0.2.1

- old
+ new

@@ -3,23 +3,28 @@ require 'fluent-logger' module UU class LoggerFluent class Formatter - def initialize(context, tag) + DEFAULT_TAG = 'fluentd' + @tag = ENV['FLUENTD_TAG'] || DEFAULT_TAG + class << self + attr_accessor :tag + end + + def initialize(context) @context = context - @tag = tag end attr_reader :context def call(severity, msg) - [@tag, { + [self.class.tag, { log_level: severity, **metadata, - message: msg, **@context.context, + **(msg.is_a?(Hash) ? msg : { message: msg }), }] end def metadata location = find_location @@ -49,32 +54,22 @@ def post(*); end end NIL = Nil.new - @cached_logger = Hash.new do |hash, key| - hash[key] = - Fluent::Logger::FluentLogger.new( - nil, - nanosecond_precision: true, - **key, - ) + def initialize(cxt, host: ENV['FLUENTD_HOST'], port: ENV['FLUENTD_PORT']) + @logger = create_logger(host, port) + @formatter = Formatter.new(cxt) end - class << self - attr_reader :cached_logger - end + def create_logger(host, port) + return NIL unless host - def initialize(cxt, host: ENV['FLUENTD_HOST'], port: ENV['FLUENTD_PORT']) - @logger = - if host - self.class.cached_logger[ - { host: host, **(port ? { port: port.to_i } : {}) } - ] - else - NIL - end - @formatter = Formatter.new(cxt, 'tag') + Fluent::Logger::FluentLogger.new( + nil, + nanosecond_precision: true, + host: host, **(port ? { port: port.to_i } : {}) + ) end attr_reader :logger attr_accessor :formatter