Skip to content

Commit 753b677

Browse files
committed
Merge pull request #210 from quixoten/logging
Make logging a pluggable component of Protobuf
2 parents 9486341 + 406ce63 commit 753b677

30 files changed

Lines changed: 170 additions & 330 deletions

lib/protobuf/cli.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
require 'thor'
22
require 'protobuf/version'
3-
require 'protobuf/logger'
3+
require 'protobuf/logging'
44
require 'protobuf/rpc/servers/socket_runner'
55
require 'protobuf/rpc/servers/zmq_runner'
66

77
module Protobuf
88
class CLI < ::Thor
99
include ::Thor::Actions
10+
include ::Protobuf::Logging
1011

1112
attr_accessor :runner, :mode
1213

@@ -21,7 +22,7 @@ class CLI < ::Thor
2122
option :threshold, :type => :numeric, :default => 100, :aliases => %w(-t), :desc => 'Multi-threaded Socket Server cleanup threshold.'
2223
option :threads, :type => :numeric, :default => 5, :aliases => %w(-r), :desc => 'Number of worker threads to run. Only applicable in --zmq mode.'
2324

24-
option :log, :type => :string, :aliases => %w(-l), :desc => 'Log file or device. Default is STDOUT.'
25+
option :log, :type => :string, :default => STDOUT, :aliases => %w(-l), :desc => 'Log file or device. Default is STDOUT.'
2526
option :level, :type => :numeric, :default => ::Logger::INFO, :aliases => %w(-v), :desc => 'Log level to use, 0-5 (see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/)'
2627

2728
option :socket, :type => :boolean, :aliases => %w(-s), :desc => 'Socket Mode for server and client connections.'
@@ -86,12 +87,14 @@ def configure_gc
8687
# Setup the protobuf logger.
8788
def configure_logger
8889
debug_say('Configuring logger')
89-
::Protobuf::Logger.configure({ :file => options.log || STDOUT,
90-
:level => options.debug? ? ::Logger::DEBUG : options.level })
90+
91+
log_level = options.debug? ? ::Logger::DEBUG : options.level
92+
93+
::Protobuf::Logging.initialize_logger(options.log, log_level)
9194

9295
# Debug output the server options to the log file.
93-
::Protobuf::Logger.debug { 'Debugging options:' }
94-
::Protobuf::Logger.debug { options.inspect }
96+
logger.debug { 'Debugging options:' }
97+
logger.debug { options.inspect }
9598
end
9699

97100
# Re-write the $0 var to have a nice process name in ps.
@@ -185,15 +188,16 @@ def runner_options
185188
end
186189

187190
def say_and_exit(message, exception = nil)
188-
message = set_color(message, :red) if ::Protobuf::Logger.file == STDOUT
191+
message = set_color(message, :red) if options.log == STDOUT
192+
193+
logger.error { message }
189194

190-
::Protobuf::Logger.error { message }
191195
if exception
192196
$stderr.puts "[#{exception.class.name}] #{exception.message}"
193197
$stderr.puts exception.backtrace.join("\n")
194198

195-
::Protobuf::Logger.error { "[#{exception.class.name}] #{exception.message}" }
196-
::Protobuf::Logger.debug { exception.backtrace.join("\n") }
199+
logger.error { "[#{exception.class.name}] #{exception.message}" }
200+
logger.debug { exception.backtrace.join("\n") }
197201
end
198202

199203
exit(1)
@@ -212,18 +216,18 @@ def create_zmq_runner
212216
end
213217

214218
def shutdown_server
215-
::Protobuf::Logger.info { 'RPC Server shutting down...' }
219+
logger.info { 'RPC Server shutting down...' }
216220
@runner.try(:stop)
217221
::Protobuf::Rpc::ServiceDirectory.instance.stop
218-
::Protobuf::Logger.info { 'Shutdown complete' }
222+
logger.info { 'Shutdown complete' }
219223
end
220224

221225
# Start the runner and log the relevant options.
222226
def start_server
223227
debug_say('Running server')
224228

225229
@runner.run do
226-
::Protobuf::Logger.info {
230+
logger.info {
227231
"pid #{::Process.pid} -- #{@runner_mode} RPC Server listening at #{options.host}:#{options.port}"
228232
}
229233

lib/protobuf/field/base_field.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
require 'protobuf/wire_type'
21
require 'protobuf/field/field_array'
2+
require 'protobuf/logging'
3+
require 'protobuf/wire_type'
34

45
module Protobuf
56
module Field
67
class BaseField
8+
include ::Protobuf::Logging
79

810
##
911
# Constants

lib/protobuf/field/bytes_field.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def define_setter
6767
raise TypeError, "Unacceptable value #{val} for field #{field.name} of type #{field.type_class}"
6868
end
6969
rescue NoMethodError => ex
70-
::Protobuf::Logger.error { ex.message }
71-
::Protobuf::Logger.error { ex.backtrace.join("\n") }
70+
logger.error { ex.message }
71+
logger.error { ex.backtrace.join("\n") }
7272
raise TypeError, "Got NoMethodError attempting to set #{val} for field #{field.name} of type #{field.type_class}: #{ex.message}"
7373
end
7474
end

lib/protobuf/lifecycle.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Protobuf
22
class Lifecycle
3-
include ::Protobuf::Logger::LogMethods
3+
include ::Protobuf::Logging
44

55
def self.register(event_name, &blk)
66
raise "Lifecycle register must have a block" unless block_given?

lib/protobuf/logger.rb

Lines changed: 0 additions & 86 deletions
This file was deleted.

lib/protobuf/logging.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Protobuf
2+
module Logging
3+
def self.initialize_logger(log_target=$stdout, log_level=::Logger::INFO)
4+
@counter ||= 0
5+
@counter = @counter + 1
6+
old_logger = defined?(@logger) ? @logger : nil
7+
@logger = Logger.new(log_target)
8+
@logger.level = log_level
9+
old_logger.close if old_logger and close_old_logger?
10+
@logger
11+
end
12+
13+
def self.close_old_logger=(boolean)
14+
@close_old_logger = !!boolean
15+
end
16+
17+
def self.close_old_logger?
18+
defined?(@close_old_logger) ? @close_old_logger : true
19+
end
20+
21+
def self.logger
22+
defined?(@logger) ? @logger : initialize_logger
23+
end
24+
25+
def self.logger=(new_logger)
26+
@logger = new_logger
27+
end
28+
29+
def logger
30+
::Protobuf::Logging.logger
31+
end
32+
33+
def log_exception(ex)
34+
logger.error { ex.message }
35+
logger.error { ex.backtrace[0..5].join("\n") }
36+
logger.debug { ex.backtrace.join("\n") }
37+
end
38+
39+
def log_signature
40+
@_log_signature ||= "[#{self.class == Class ? self.name : self.class.name}]"
41+
end
42+
43+
def sign_message(message)
44+
"#{log_signature} #{message}"
45+
end
46+
end
47+
end
48+
49+
# Inspired by [mperham](https://github.com/mperham/sidekiq)

lib/protobuf/rpc/client.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
require 'forwardable'
22
require 'protobuf'
3-
require 'protobuf/logger'
3+
require 'protobuf/logging'
44
require 'protobuf/rpc/error'
55
require 'protobuf/rpc/connector'
66

77
module Protobuf
88
module Rpc
99
class Client
1010
extend Forwardable
11-
include Protobuf::Logger::LogMethods
11+
include Protobuf::Logging
1212

1313
def_delegators :@connector, :options, :complete_cb, :success_cb, :failure_cb
1414
attr_reader :connector
@@ -29,7 +29,7 @@ class Client
2929
def initialize(options = {})
3030
raise "Invalid client configuration. Service must be defined." if options[:service].nil?
3131
@connector = Connector.connector_for_client.new(options)
32-
log_debug { sign_message("Initialized with options: #{options.inspect}") }
32+
logger.debug { sign_message("Initialized with options: #{options.inspect}") }
3333
end
3434

3535
def log_signature
@@ -106,28 +106,28 @@ def on_success=(callable)
106106
def method_missing(method_name, *params)
107107
service = options[:service]
108108
unless service.rpc_method?(method_name)
109-
log_error { sign_message("#{service.name}##{method_name.to_s} not rpc method, passing to super") }
109+
logger.error { sign_message("#{service.name}##{method_name.to_s} not rpc method, passing to super") }
110110
super(method_name, *params)
111111
else
112-
log_debug { sign_message("#{service.name}##{method_name.to_s}") }
112+
logger.debug { sign_message("#{service.name}##{method_name.to_s}") }
113113
rpc = service.rpcs[method_name.to_sym]
114114

115115
options[:request_type] = rpc.request_type
116-
log_debug { sign_message("Request Type: #{options[:request_type].name}") }
116+
logger.debug { sign_message("Request Type: #{options[:request_type].name}") }
117117

118118
options[:response_type] = rpc.response_type
119-
log_debug { sign_message("Response Type: #{options[:response_type].name}") }
119+
logger.debug { sign_message("Response Type: #{options[:response_type].name}") }
120120

121121
options[:method] = method_name.to_s
122122
options[:request] = params[0].is_a?(Hash) ? options[:request_type].new(params[0]) : params[0]
123-
log_debug { sign_message("Request Data: #{options[:request].inspect}") }
123+
logger.debug { sign_message("Request Data: #{options[:request].inspect}") }
124124

125125
# Call client to setup on_success and on_failure event callbacks
126126
if block_given?
127-
log_debug { sign_message("client setup callback given, invoking") }
127+
logger.debug { sign_message("client setup callback given, invoking") }
128128
yield(self)
129129
else
130-
log_debug { sign_message("no block given for callbacks") }
130+
logger.debug { sign_message("no block given for callbacks") }
131131
end
132132

133133
send_request

lib/protobuf/rpc/connectors/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'timeout'
2-
require 'protobuf/logger'
2+
require 'protobuf/logging'
33
require 'protobuf/rpc/rpc.pb'
44
require 'protobuf/rpc/buffer'
55
require 'protobuf/rpc/error'
@@ -23,7 +23,7 @@ module Connectors
2323
}
2424

2525
class Base
26-
include Protobuf::Logger::LogMethods
26+
include Protobuf::Logging
2727

2828
attr_reader :options
2929
attr_accessor :success_cb, :failure_cb, :complete_cb

0 commit comments

Comments
 (0)