Sha256: 2c9d492012c3d1c689879d9bbbf7f7ad618cb96e975817b3b3d825dffcacdd2c
Contents?: true
Size: 1.82 KB
Versions: 2
Compression:
Stored size: 1.82 KB
Contents
# frozen_string_literal: true require 'fluent-logger' module UU class LoggerFluent class Formatter DEFAULT_TAG = 'fluentd' @tag = ENV['FLUENTD_TAG'] || DEFAULT_TAG class << self attr_accessor :tag end def initialize(context) @context = context end attr_reader :context def call(severity, msg) [self.class.tag, { log_level: severity, **metadata, **@context.context, **(msg.is_a?(Hash) ? msg : { message: msg }), }] end def metadata location = find_location { filename: File.basename(location.path), method: location.label, lineno: location.lineno, } end PATHS = %w[ /log.rb /logger.rb /loggable.rb /forwardable.rb ].freeze def find_location caller_locations.find do |location_| location_.path != __FILE__ && PATHS.none? { |path| location_.path.end_with?(path) } end end end class Nil def post(*); end end NIL = Nil.new def initialize(cxt, host: ENV['FLUENTD_HOST'], port: ENV['FLUENTD_PORT']) @logger = create_logger(host, port) @formatter = Formatter.new(cxt) end def create_logger(host, port) return NIL unless host Fluent::Logger::FluentLogger.new( nil, nanosecond_precision: true, host: host, **(port ? { port: port.to_i } : {}) ) end attr_reader :logger attr_accessor :formatter def format_message(severity, message) @formatter.call(severity, message) end %i[debug info warn error fatal].each do |severity| define_method(severity) do |&block| @logger.post(*format_message(severity, block.call)) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
uu-0.2.2 | lib/uu/logger_fluent.rb |
uu-0.2.1 | lib/uu/logger_fluent.rb |