Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #15 from cosmo0920/use-log-method-instead-of-log-g…
Browse files Browse the repository at this point in the history
…lobal-var

Use log method instead of $log global variable

Patch by Hiroshi Hatake. Thanks!!!
  • Loading branch information
kou committed Apr 13, 2017
2 parents 7e67b6a + e4c31bc commit 027b202
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
41 changes: 21 additions & 20 deletions lib/fluent/plugin/in_groonga.rb
Expand Up @@ -173,9 +173,9 @@ def emit(command, params)
def run
@loop.run
rescue
$log.error("[input][groonga][error] unexpected error",
:error => "#{$!.class}: #{$!}")
$log.error_backtrace
@input_plugin.log.error("[input][groonga][error] unexpected error",
:error => "#{$!.class}: #{$!}")
@input_plugin.log.error_backtrace
end

def emit_command?(command)
Expand Down Expand Up @@ -205,30 +205,30 @@ def initialize(socket, input)
def on_connect
@repeater = @input.create_repeater(self)
@repeater.on_connect_failed do
$log.error("[input][groonga][connect][error] " +
@input.log.error("[input][groonga][connect][error] " +
"failed to connect to Groonga:",
:real_host => @input.real_host,
:real_port => @input.real_port)
close
end
@request_handler = RequestHandler.new(@input, @repeater)
@response_handler = ResponseHandler.new(self)
@response_handler = ResponseHandler.new(self, @input)
end

def on_read(data)
begin
@request_handler << data
rescue HTTP::Parser::Error, URI::InvalidURIError
$log.error("[input][groonga][request][error] " +
@input.log.error("[input][groonga][request][error] " +
"failed to parse HTTP request:",
:error => "#{$!.class}: #{$!}")
$log.error_backtrace
@input.log.error_backtrace
reply_error_response("400 Bad Request")
rescue
$log.error("[input][groonga][request][error] " +
@input.log.error("[input][groonga][request][error] " +
"failed to handle HTTP request:",
:error => "#{$!.class}: #{$!}")
$log.error_backtrace
@input.log.error_backtrace
reply_error_response("500 Internal Server Error")
end
end
Expand All @@ -237,10 +237,10 @@ def write_back(data)
begin
@response_handler << data
rescue
$log.error("[input][groonga][response][error] " +
@input.log.error("[input][groonga][response][error] " +
"failed to handle HTTP response from Groonga:",
:error => "#{$!.class}: #{$!}")
$log.error_backtrace
@input.log.error_backtrace
reply_error_response("500 Internal Server Error")
return
end
Expand Down Expand Up @@ -345,8 +345,9 @@ def on_message_complete
end

class ResponseHandler
def initialize(handler)
def initialize(handler, input)
@handler = handler
@input = input
@parser = Http::Parser.new(self)
end

Expand Down Expand Up @@ -381,19 +382,19 @@ def on_message_complete
begin
response = JSON.parse(@body)
rescue JSON::ParserError
$log.warn("[input][groonga][response][warn] " +
"failed to parse response JSON:",
:error => "#{$!.class}: #{$!}",
:json => @body)
@input.log.warn("[input][groonga][response][warn] " +
"failed to parse response JSON:",
:error => "#{$!.class}: #{$!}",
:json => @body)
end
when /\Aapplication\/x-msgpack\z/i
begin
response = MessagePack.unpack(@body)
rescue MessagePack::UnpackError, EOFError
$log.warn("[input][groonga][response][warn] " +
"failed to parse response MessagePack",
:error => "#{$!.class}: #{$!}",
:msgpack => @body)
@input.log.warn("[input][groonga][response][warn] " +
"failed to parse response MessagePack",
:error => "#{$!.class}: #{$!}",
:msgpack => @body)
end
when /\Atext\/x-groonga-command-list\z/i
response = @body
Expand Down
40 changes: 21 additions & 19 deletions lib/fluent/plugin/out_groonga.rb
Expand Up @@ -109,9 +109,9 @@ def write(chunk)
def create_client(protocol)
case protocol
when :http, :gqtp
NetworkClient.new(protocol)
NetworkClient.new(protocol, self)
when :command
CommandClient.new
CommandClient.new(self)
end
end

Expand Down Expand Up @@ -610,9 +610,10 @@ class NetworkClient < BaseClient
config_param :host, :string, :default => nil
config_param :port, :integer, :default => nil

def initialize(protocol)
def initialize(protocol, output_plugin)
super()
@protocol = protocol
@output_plugin = output_plugin
end

def start
Expand All @@ -634,17 +635,17 @@ def execute(name, arguments={})
begin
response = @client.execute(command)
rescue Groonga::Client::Error
$log.error("[output][groonga][error]",
:protocol => @protocol,
:host => @host,
:port => @port,
:command_name => name)
@output_plugin.log.error("[output][groonga][error]",
:protocol => @protocol,
:host => @host,
:port => @port,
:command_name => name)
raise
end
unless response.success?
$log.error("[output][groonga][error]",
:status_code => response.status_code,
:message => response.message)
@output_plugin.log.error("[output][groonga][error]",
:status_code => response.status_code,
:message => response.message)
end
response
end
Expand All @@ -659,8 +660,9 @@ class CommandClient < BaseClient
Shellwords.split(value)
end

def initialize
super
def initialize(output_plugin)
super()
@output_plugin = output_plugin
end

def configure(conf)
Expand Down Expand Up @@ -747,14 +749,14 @@ def read_output(context)
end

unless output_message.empty?
$log.debug("[output][groonga][output]",
:context => context,
:message => output_message)
@output_plugin.log.debug("[output][groonga][output]",
:context => context,
:message => output_message)
end
unless error_message.empty?
$log.error("[output][groonga][error]",
:context => context,
:message => error_message)
@output_plugin.log.error("[output][groonga][error]",
:context => context,
:message => error_message)
end
end
end
Expand Down

0 comments on commit 027b202

Please sign in to comment.