Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append plugin name and id to log #860

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/fluent/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def initialize(out=STDERR, level=LEVEL_TRACE, opts={})

# 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
Expand All @@ -75,6 +77,7 @@ def initialize(out=STDERR, level=LEVEL_TRACE, opts={})
attr_accessor :level
attr_accessor :tag
attr_accessor :time_format
attr_accessor :optional_header, :optional_attrs

def enable_debug(b=true)
@debug_mode = b
Expand Down Expand Up @@ -268,8 +271,8 @@ def dump_stacktrace(backtrace, level)

def event(level, args)
time = Time.now
message = ''
map = {}
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|
Expand Down Expand Up @@ -340,6 +343,18 @@ def enable_color(b = true)
@logger.enable_color b
end

def optional_header=(header)
@logger.optional_header = header
end

def optional_attrs=(attrs)
@logger.optional_attrs = attrs
end

def optional_attrs
@logger.optional_attrs
end

extend Forwardable
def_delegators '@logger', :enable_color?, :enable_debug, :enable_event,
:disable_events, :tag, :tag=, :time_format, :time_format=,
Expand Down Expand Up @@ -371,6 +386,8 @@ def configure(conf)
@log = PluginLogger.new($log)
end
@log.level = @log_level
@log.optional_header = "[#{self.class.name}#{plugin_id_configured? ? "(" + @id + ")" : ""}] "
@log.optional_attrs = {}
end
end
end
Expand Down