class Fluent::Log

Public Class Methods

new(out=STDERR, level=LEVEL_TRACE, opts={}) click to toggle source
# File lib/fluent/log.rb, line 56
def initialize(out=STDERR, level=LEVEL_TRACE, opts={})
  @out = out
  @level = level
  @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')

  if opts.has_key?(:suppress_repeated_stacktrace)
    @suppress_repeated_stacktrace = opts[:suppress_repeated_stacktrace]
  end
end
str_to_level(log_level_str) click to toggle source
# File lib/fluent/log.rb, line 44
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

Public Instance Methods

DEBUG(*args, &block)
Alias for: debug
ERROR(*args, &block)
Alias for: error
FATAL(*args, &block)
Alias for: fatal
INFO(*args, &block)
Alias for: info
TRACE(*args, &block)
Alias for: trace
WARN(*args, &block)
Alias for: warn
debug(*args, &block) click to toggle source
# File lib/fluent/log.rb, line 146
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
Also aliased as: DEBUG
debug_backtrace(backtrace=$!.backtrace) click to toggle source
# File lib/fluent/log.rb, line 155
def debug_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_DEBUG)
end
disable_events(thread) click to toggle source

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 118
def disable_events(thread)
  @threads_exclude_events.push(thread) unless @threads_exclude_events.include?(thread)
end
enable_color(b=true) click to toggle source
# File lib/fluent/log.rb, line 95
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
enable_color?() click to toggle source
# File lib/fluent/log.rb, line 91
def enable_color?
  !@color_reset.empty?
end
enable_debug(b=true) click to toggle source
# File lib/fluent/log.rb, line 81
def enable_debug(b=true)
  @debug_mode = b
  self
end
enable_event(b=true) click to toggle source
# File lib/fluent/log.rb, line 86
def enable_event(b=true)
  @self_event = b
  self
end
error(*args, &block) click to toggle source
# File lib/fluent/log.rb, line 200
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
Also aliased as: ERROR
error_backtrace(backtrace=$!.backtrace) click to toggle source
# File lib/fluent/log.rb, line 209
def error_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_ERROR)
end
fatal(*args, &block) click to toggle source
# File lib/fluent/log.rb, line 218
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
Also aliased as: FATAL
fatal_backtrace(backtrace=$!.backtrace) click to toggle source
# File lib/fluent/log.rb, line 227
def fatal_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_FATAL)
end
flush() click to toggle source
# File lib/fluent/log.rb, line 244
def flush
  @out.flush
end
info(*args, &block) click to toggle source
# File lib/fluent/log.rb, line 164
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
Also aliased as: INFO
info_backtrace(backtrace=$!.backtrace) click to toggle source
# File lib/fluent/log.rb, line 173
def info_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_INFO)
end
on_debug(&block) click to toggle source
# File lib/fluent/log.rb, line 141
def on_debug(&block)
  return if @level > LEVEL_DEBUG
  block.call if block
end
on_error(&block) click to toggle source
# File lib/fluent/log.rb, line 195
def on_error(&block)
  return if @level > LEVEL_ERROR
  block.call if block
end
on_fatal(&block) click to toggle source
# File lib/fluent/log.rb, line 213
def on_fatal(&block)
  return if @level > LEVEL_FATAL
  block.call if block
end
on_info(&block) click to toggle source
# File lib/fluent/log.rb, line 159
def on_info(&block)
  return if @level > LEVEL_INFO
  block.call if block
end
on_trace(&block) click to toggle source
# File lib/fluent/log.rb, line 122
def on_trace(&block)
  return if @level > LEVEL_TRACE
  block.call if block
end
on_warn(&block) click to toggle source
# File lib/fluent/log.rb, line 177
def on_warn(&block)
  return if @level > LEVEL_WARN
  block.call if block
end
puts(msg) click to toggle source
# File lib/fluent/log.rb, line 231
def puts(msg)
  @out.puts(msg)
  @out.flush
  msg
rescue
  # FIXME
  nil
end
trace(*args, &block) click to toggle source
# File lib/fluent/log.rb, line 127
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
Also aliased as: TRACE
trace_backtrace(backtrace=$!.backtrace) click to toggle source
# File lib/fluent/log.rb, line 137
def trace_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_TRACE)
end
warn(*args, &block) click to toggle source
# File lib/fluent/log.rb, line 182
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
Also aliased as: WARN
warn_backtrace(backtrace=$!.backtrace) click to toggle source
# File lib/fluent/log.rb, line 191
def warn_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_WARN)
end
write(data) click to toggle source
# File lib/fluent/log.rb, line 240
def write(data)
  @out.write(data)
end

Private Instance Methods

caller_line(time, depth, level) click to toggle source
# File lib/fluent/log.rb, line 297
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
dump_stacktrace(backtrace, level) click to toggle source
# File lib/fluent/log.rb, line 250
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
event(level, args) click to toggle source
# File lib/fluent/log.rb, line 267
def event(level, args)
  time = Time.now
  message = ''
  map = {}
  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|
    message << " #{k}=#{v.inspect}"
  }

  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