# File lib/fluent/log.rb, line 57 def initialize(logger, opts={}) # overwrites logger.level= so that config reloading resets level of Fluentd::Log orig_logger_level_setter = logger.class.public_instance_method(:level=).bind(logger) me = self # The original ruby logger sets the number as each log level like below. # DEBUG = 0 # INFO = 1 # WARN = 2 # ERROR = 3 # FATAL = 4 # Serverengine use this original log number. In addition to this, serverengine sets -1 as TRACE level. # TRACE = -1 # # On the other hand, in fluentd side, it sets the number like below. # TRACE = 0 # DEBUG = 1 # INFO = 2 # WARN = 3 # ERROR = 4 # FATAL = 5 # # Then fluentd's level is set as serverengine's level + 1. # So if serverengine's logger level is changed, fluentd's log level will be changed to that + 1. logger.define_singleton_method(:level=) {|level| orig_logger_level_setter.call(level); me.level = self.level + 1 } @logger = logger @out = logger.instance_variable_get(:@logdev) @level = logger.level + 1 @debug_mode = false @self_event = false @tag = 'fluent' @time_format = '%Y-%m-%d %H:%M:%S %z ' @depth_offset = 1 enable_color out.tty? # TODO: This variable name is unclear so we should change to better name. @threads_exclude_events = [] # Fluent::Engine requires Fluent::Log, so we must take that object lazily @engine = Fluent.const_get('Engine') @optional_header = nil @optional_attrs = nil @suppress_repeated_stacktrace = opts[:suppress_repeated_stacktrace] end
# File lib/fluent/log.rb, line 45 def self.str_to_level(log_level_str) case log_level_str.downcase when "trace" then LEVEL_TRACE when "debug" then LEVEL_DEBUG when "info" then LEVEL_INFO when "warn" then LEVEL_WARN when "error" then LEVEL_ERROR when "fatal" then LEVEL_FATAL else raise "Unknown log level: level = #{log_level_str}" end end
# File lib/fluent/log.rb, line 196 def debug(*args, &block) return if @level > LEVEL_DEBUG args << block.call if block time, msg = event(:debug, args) puts [@color_debug, caller_line(time, @depth_offset, LEVEL_DEBUG), msg, @color_reset].join rescue end
# File lib/fluent/log.rb, line 205 def debug_backtrace(backtrace=$!.backtrace) dump_stacktrace(backtrace, LEVEL_DEBUG) end
If you want to suppress event emitting in specific thread, please use this method. Events in passed thread are never emitted.
# File lib/fluent/log.rb, line 168 def disable_events(thread) @threads_exclude_events.push(thread) unless @threads_exclude_events.include?(thread) end
# File lib/fluent/log.rb, line 102 def dup dl_opts = {} dl_opts[:log_level] = @level - 1 logger = ServerEngine::DaemonLogger.new(@out, dl_opts) clone = self.class.new(logger, suppress_repeated_stacktrace: @suppress_repeated_stacktrace) clone.tag = @tag clone.time_format = @time_format # optional headers/attrs are not copied, because new PluginLogger should have another one of it clone end
# File lib/fluent/log.rb, line 145 def enable_color(b=true) if b @color_trace = TTYColor::BLUE @color_debug = TTYColor::WHITE @color_info = TTYColor::GREEN @color_warn = TTYColor::YELLOW @color_error = TTYColor::MAGENTA @color_fatal = TTYColor::RED @color_reset = TTYColor::NORMAL else @color_trace = '' @color_debug = '' @color_info = '' @color_warn = '' @color_error = '' @color_fatal = '' @color_reset = '' end self end
# File lib/fluent/log.rb, line 141 def enable_color? !@color_reset.empty? end
# File lib/fluent/log.rb, line 131 def enable_debug(b=true) @debug_mode = b self end
# File lib/fluent/log.rb, line 136 def enable_event(b=true) @self_event = b self end
# File lib/fluent/log.rb, line 250 def error(*args, &block) return if @level > LEVEL_ERROR args << block.call if block time, msg = event(:error, args) puts [@color_error, caller_line(time, @depth_offset, LEVEL_ERROR), msg, @color_reset].join rescue end
# File lib/fluent/log.rb, line 259 def error_backtrace(backtrace=$!.backtrace) dump_stacktrace(backtrace, LEVEL_ERROR) end
# File lib/fluent/log.rb, line 268 def fatal(*args, &block) return if @level > LEVEL_FATAL args << block.call if block time, msg = event(:fatal, args) puts [@color_fatal, caller_line(time, @depth_offset, LEVEL_FATAL), msg, @color_reset].join rescue end
# File lib/fluent/log.rb, line 277 def fatal_backtrace(backtrace=$!.backtrace) dump_stacktrace(backtrace, LEVEL_FATAL) end
# File lib/fluent/log.rb, line 294 def flush @out.flush end
# File lib/fluent/log.rb, line 214 def info(*args, &block) return if @level > LEVEL_INFO args << block.call if block time, msg = event(:info, args) puts [@color_info, caller_line(time, @depth_offset, LEVEL_INFO), msg, @color_reset].join rescue end
# File lib/fluent/log.rb, line 223 def info_backtrace(backtrace=$!.backtrace) dump_stacktrace(backtrace, LEVEL_INFO) end
# File lib/fluent/log.rb, line 119 def logdev=(logdev) @out = logdev @logger.instance_variable_set(:@logdev, logdev) nil end
# File lib/fluent/log.rb, line 191 def on_debug(&block) return if @level > LEVEL_DEBUG block.call if block end
# File lib/fluent/log.rb, line 245 def on_error(&block) return if @level > LEVEL_ERROR block.call if block end
# File lib/fluent/log.rb, line 263 def on_fatal(&block) return if @level > LEVEL_FATAL block.call if block end
# File lib/fluent/log.rb, line 209 def on_info(&block) return if @level > LEVEL_INFO block.call if block end
# File lib/fluent/log.rb, line 172 def on_trace(&block) return if @level > LEVEL_TRACE block.call if block end
# File lib/fluent/log.rb, line 227 def on_warn(&block) return if @level > LEVEL_WARN block.call if block end
# File lib/fluent/log.rb, line 281 def puts(msg) @logger << msg + "\n" @out.flush msg rescue # FIXME nil end
# File lib/fluent/log.rb, line 125 def reopen! # do nothing in @logger.reopen! because it's already reopened in Supervisor.load_config @logger.reopen! if @logger nil end
# File lib/fluent/log.rb, line 298 def reset @out.reset if @out.respond_to?(:reset) end
# File lib/fluent/log.rb, line 177 def trace(*args, &block) return if @level > LEVEL_TRACE args << block.call if block time, msg = event(:trace, args) puts [@color_trace, caller_line(time, @depth_offset, LEVEL_TRACE), msg, @color_reset].join rescue # logger should not raise an exception. This rescue prevents unexpected behaviour. end
# File lib/fluent/log.rb, line 187 def trace_backtrace(backtrace=$!.backtrace) dump_stacktrace(backtrace, LEVEL_TRACE) end
# File lib/fluent/log.rb, line 232 def warn(*args, &block) return if @level > LEVEL_WARN args << block.call if block time, msg = event(:warn, args) puts [@color_warn, caller_line(time, @depth_offset, LEVEL_WARN), msg, @color_reset].join rescue end
# File lib/fluent/log.rb, line 241 def warn_backtrace(backtrace=$!.backtrace) dump_stacktrace(backtrace, LEVEL_WARN) end
# File lib/fluent/log.rb, line 290 def write(data) @out.write(data) end
# File lib/fluent/log.rb, line 355 def caller_line(time, depth, level) log_msg = "#{time.strftime(@time_format)}[#{LEVEL_TEXT[level]}]: " if @debug_mode line = caller(depth+1)[0] if match = /^(.+?):(\d+)(?::in `(.*)')?/.match(line) file = match[1].split('/')[-2,2].join('/') line = match[2] method = match[3] return "#{log_msg}#{file}:#{line}:#{method}: " end end return log_msg end
# File lib/fluent/log.rb, line 304 def dump_stacktrace(backtrace, level) return if @level > level time = Time.now line = caller_line(time, 5, level) if @suppress_repeated_stacktrace && (Thread.current[:last_repeated_stacktrace] == backtrace) puts [" ", line, 'suppressed same stacktrace'].join else backtrace.each { |msg| puts [" ", line, msg].join } Thread.current[:last_repeated_stacktrace] = backtrace if @suppress_repeated_stacktrace end nil end
# File lib/fluent/log.rb, line 321 def event(level, args) time = Time.now message = @optional_header ? @optional_header.dup : '' map = @optional_attrs ? @optional_attrs.dup : {} args.each {|a| if a.is_a?(Hash) a.each_pair {|k,v| map[k.to_s] = v } else message << a.to_s end } map.each_pair {|k,v| if k == "error".freeze && v.is_a?(Exception) && !map.has_key?("error_class") message << " error_class=#{v.class.to_s} error=#{v.to_s.inspect}" else message << " #{k}=#{v.inspect}" end } unless @threads_exclude_events.include?(Thread.current) record = map.dup record.keys.each {|key| record[key] = record[key].inspect unless record[key].respond_to?(:to_msgpack) } record['message'] = message.dup @engine.push_log_event("#{@tag}.#{level}", time.to_i, record) end return time, message end