Skip to content

Instantly share code, notes, and snippets.

@tzusman
Created July 28, 2019 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzusman/1f618d17e67f72be275b658742b4549f to your computer and use it in GitHub Desktop.
Save tzusman/1f618d17e67f72be275b658742b4549f to your computer and use it in GitHub Desktop.
/*
* Place here: ~/.rvm/gems/ruby-2.4.1/gems/fluentd-1.6.0/lib/fluent/plugin/out_color_stdout.rb
* Run this: gem install awesome_print
*/
require 'fluent/plugin/output'
require 'awesome_print'
module Fluent::Plugin
class StdoutOutput < Output
Fluent::Plugin.register_output('color_stdout', self)
helpers :inject, :formatter, :compat_parameters
DEFAULT_LINE_FORMAT_TYPE = 'stdout'
DEFAULT_FORMAT_TYPE = 'json'
config_section :buffer do
config_set_default :chunk_keys, ['tag']
config_set_default :flush_at_shutdown, true
config_set_default :chunk_limit_size, 10 * 1024
end
config_section :format do
config_set_default :@type, DEFAULT_LINE_FORMAT_TYPE
config_set_default :output_type, DEFAULT_FORMAT_TYPE
end
def prefer_buffered_processing
false
end
def multi_workers_ready?
true
end
attr_accessor :formatter
def configure(conf)
compat_parameters_convert(conf, :inject, :formatter)
super
@formatter = formatter_create
end
def process(tag, es)
es = inject_values_to_event_stream(tag, es)
es.each {|time,record|
$log.write(format(tag, time, record))
}
$log.flush
end
def color(level)
{
"error" => :red,
"warning" => :yellow,
"success" => :green,
"info" => :blue,
"debug" => :gray,
"trace" => :to_s,
}[level] || :to_s
end
def format(tag, time, record)
record = inject_values_to_record(tag, time, record)
col = color(record['level'])
@formatter.format(tag, time, record).chomp.send(col) + "\n"
end
def write(chunk)
chunk.write_to($log)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment